从Tkinter获取文本框条目数据

时间:2018-11-04 00:24:58

标签: python tkinter

我正在使用TKinter创建一个界面,该界面将使用用户名,密码和Web链接。

我在让输入框变量输入全局变量时遇到问题。 我收到NameError:名称'userEntry'未定义

我曾尝试称呼自己。以及init_window和append。但是我真的在黑暗中摸索着,这是我第一次在tkinter中使用Window(Frame)类,因此我不确定自己搞砸了什么。

----------------------编辑------------------------ ----------------------------------

我想从init_window(self)中获取文本数据,并将runProgram(self)中的数据用作硒的变量。但是我无法传输数据。

data = []
userName= []
password=[]
link=[]

class Window(Frame):

     def __init__(self, master=None):
         Frame.__init__(self, master)                 
         self.master = master
         self.init_window()

    #Creation of init_window
     def init_window(self):

        self.master.title("Comment uploader") # changing the title of our master widget         

        userEntry= Entry(root, textvariable=userName)
        passEntry = Entry(root, textvariable=password, show='*')
        linkEntry= Entry(root, textvariable=link)
        openButton = Button(root, text="Open CSV", command=self.fileOpen) # creating a button instance
        goButton = Button(root, text="Run program", command=self.runProgram)

        userEntry.grid(row=0, column=2,sticky=W)
        passEntry.grid(row=1, column=2,sticky=W)
        linkEntry.grid(row=2, column=2,sticky=W) 
        openButton.grid(row=3, column=2, sticky=W)  
        goButton.grid(row=4, column=2, sticky=W)

        label_1= Label(root,text="User Name:" ,font="Times 14")
        label_2= Label(root,text="Password:" ,font="Times 14")
        label_3= Label(root,text="input link" ,font="Times 14")
        label_4= Label(root,text="Open CSV" ,font="Times 14")
        label_5= Label(root,text="Sart the program" ,font="Times 14")

        label_1.grid(row=0 , column=1, sticky=W)
        label_2.grid(row=1 , column=1, sticky=W)
        label_3.grid(row=2 , column=1, sticky=W)
        label_4.grid(row=3 , column=1, sticky=W)
        label_5.grid(row=4 , column=1, sticky=W) 

     def runProgram(self):
          userName = userEntry.get()

          print(userName)
          print(data[1])

1 个答案:

答案 0 :(得分:1)

要访问未创建变量的函数中的变量,应将该变量设为实例变量:

self.userEntry = Entry(self.master, textvariable=userName)

这适用于类中共享的所有对象。例如;您不需要共享条目userEntry,因为您有与该条目相关联的文本变量,但是您必须共享该文本变量。

这里是使用实例变量重写程序的示例变量,我认为您的目标是

from tkinter import *

class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)                 
        self.master = master
        self.data = []
        self.userName = StringVar() # StringVar to hold userEntry content
        self.password = StringVar() # etc, etc.
        self.link = StringVar()
        self.init_window()          # Build GUI

    #Creation of init_window
    def init_window(self):
        self.master.title("Comment uploader") # changing the title of our master widget         

        userEntry= Entry(self.master, textvariable=self.userName)
        passEntry = Entry(self.master, textvariable=self.password, show='*')
        linkEntry= Entry(self.master, textvariable=self.link)
        openButton = Button(self.master, text="Open CSV", command=self.fileOpen) # creating a button instance
        goButton = Button(self.master, text="Run program", command=self.runProgram)

        userEntry.grid(row=0, column=2,sticky=W)
        passEntry.grid(row=1, column=2,sticky=W)
        linkEntry.grid(row=2, column=2,sticky=W) 
        openButton.grid(row=3, column=2, sticky=W)  
        goButton.grid(row=4, column=2, sticky=W)

        label_1= Label(root,text="User Name:" ,font="Times 14")
        label_2= Label(root,text="Password:" ,font="Times 14")
        label_3= Label(root,text="input link" ,font="Times 14")
        label_4= Label(root,text="Open CSV" ,font="Times 14")
        label_5= Label(root,text="Sart the program" ,font="Times 14")

        label_1.grid(row=0 , column=1, sticky=W)
        label_2.grid(row=1 , column=1, sticky=W)
        label_3.grid(row=2 , column=1, sticky=W)
        label_4.grid(row=3 , column=1, sticky=W)
        label_5.grid(row=4 , column=1, sticky=W)

    def fileOpen(self): # Placeholder for button callback
        pass

    def runProgram(self):
        userName = self.userName.get()
        print(userName)
        #print(self.data[1])    # The list self.data is empty...

root = Tk()
app = Window(root)
root.mainloop()