从Tkinter获取文本框输入数据



我正在使用TKinter制作一个接口,该接口将接受用户名、密码和web链接。

我在将输入框变量输入全局变量时遇到问题。我得到名称错误:名称"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])

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

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()

最新更新