为什么 .get() 不会迭代并在 Tkinter 中工作?
Why won't .get() iterate and work in Tkinter?
我正在为我的网络制作此客户端 class,它应该连接到我们教授设置的 MUD 服务器,但我在 Tkinter 的入口小部件中遇到问题,当 "Enter" 按钮被按下时,条目小部件中的文本应该从 submit_value()
函数打印到 shell,但是我收到一条错误消息,指出当 .get()
时条目小部件不存在是从它调用的,谁能帮我找出这个错误?
from tkinter import *
from sys import exit
def button_func():
print("Test")
def submit_value():
print("Entered Value: %s" % (userEntry.get()))
class TestClient(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
for n in range(3):
self.grid_rowconfigure(n, weight=1)
for n in range(8):
self.grid_columnconfigure(n, weight=1)
lb1 = Listbox(self, width=20,height=24)
lb1.insert(1,"WoW")
lb1.grid(row=0, column=0, columnspan=2, sticky='news')
t1 = Text(self, width=60)
t1.grid(row=0, column=3, columnspan=3)
lb2 = Listbox(self, width=20,height=24)
lb2.insert(1,"Hi")
lb2.grid(row=0, column=6, columnspan=2, sticky='news')
la1 = Label(self, text="Value entry:")
la1.grid(row=1, column=0)
userEntry = StringVar()
e1 = Entry(self, width=40, textvariable=userEntry)
e1.grid(row=1, column=1, columnspan=6)
e2 = Button(self, text="Enter", command=submit_value)
e2.grid(row=1, column=5, columnspan=10)
b1 = Button(self, text="Start", width=10,padx=10,pady=10, command=button_func)
b1.grid(row=2, column=0)
b2 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func)
b2.grid(row=2, column=3)
b3 = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=button_func)
b3.grid(row=2, column=4)
b4 = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=button_func)
b4.grid(row=2, column=5)
b5 = Button(self, text="Loot", width=10,padx=10,pady=10, command=button_func)
b5.grid(row=2, column=6)
b6 = Button(self, text="Leave", width=10,padx=10,pady=10, command=button_func)
b6.grid(row=2, column=7)
stats = Listbox(self, width= 20)
stats.insert(1,"health:")
stats.grid(row=3, column=0, columnspan=8, sticky='news')
root = Tk()
root.title = "Test program"
tw = TestClient(root)
root.mainloop()
userEntry
是 TestClient.__init__
的局部变量。它不能在函数范围之外使用。您可以使用全局变量来解决这个问题:
from tkinter import *
from sys import exit
userEntry = object
def button_func():
print("Test")
def submit_value():
global userEntry
print("Entered Value: %s" % (userEntry.get()))
class TestClient(Frame):
def __init__(self, master):
global userEntry
Frame.__init__(self, master)
self.pack()
for n in range(3):
self.grid_rowconfigure(n, weight=1)
for n in range(8):
self.grid_columnconfigure(n, weight=1)
lb1 = Listbox(self, width=20,height=24)
lb1.insert(1,"WoW")
lb1.grid(row=0, column=0, columnspan=2, sticky='news')
t1 = Text(self, width=60)
t1.grid(row=0, column=3, columnspan=3)
lb2 = Listbox(self, width=20,height=24)
lb2.insert(1,"Hi")
lb2.grid(row=0, column=6, columnspan=2, sticky='news')
la1 = Label(self, text="Value entry:")
la1.grid(row=1, column=0)
userEntry = StringVar()
e1 = Entry(self, width=40, textvariable=userEntry)
e1.grid(row=1, column=1, columnspan=6)
e2 = Button(self, text="Enter", command=submit_value)
e2.grid(row=1, column=5, columnspan=10)
b1 = Button(self, text="Start", width=10,padx=10,pady=10, command=button_func)
b1.grid(row=2, column=0)
b2 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func)
b2.grid(row=2, column=3)
b3 = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=button_func)
b3.grid(row=2, column=4)
b4 = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=button_func)
b4.grid(row=2, column=5)
b5 = Button(self, text="Loot", width=10,padx=10,pady=10, command=button_func)
b5.grid(row=2, column=6)
b6 = Button(self, text="Leave", width=10,padx=10,pady=10, command=button_func)
b6.grid(row=2, column=7)
stats = Listbox(self, width= 20)
stats.insert(1,"health:")
stats.grid(row=3, column=0, columnspan=8, sticky='news')
root = Tk()
root.title = "Test program"
tw = TestClient(root)
root.mainloop()
以上代码将 userEntry
创建为全局变量,因此它可以在整个程序的任何地方使用。
我正在为我的网络制作此客户端 class,它应该连接到我们教授设置的 MUD 服务器,但我在 Tkinter 的入口小部件中遇到问题,当 "Enter" 按钮被按下时,条目小部件中的文本应该从 submit_value()
函数打印到 shell,但是我收到一条错误消息,指出当 .get()
时条目小部件不存在是从它调用的,谁能帮我找出这个错误?
from tkinter import *
from sys import exit
def button_func():
print("Test")
def submit_value():
print("Entered Value: %s" % (userEntry.get()))
class TestClient(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
for n in range(3):
self.grid_rowconfigure(n, weight=1)
for n in range(8):
self.grid_columnconfigure(n, weight=1)
lb1 = Listbox(self, width=20,height=24)
lb1.insert(1,"WoW")
lb1.grid(row=0, column=0, columnspan=2, sticky='news')
t1 = Text(self, width=60)
t1.grid(row=0, column=3, columnspan=3)
lb2 = Listbox(self, width=20,height=24)
lb2.insert(1,"Hi")
lb2.grid(row=0, column=6, columnspan=2, sticky='news')
la1 = Label(self, text="Value entry:")
la1.grid(row=1, column=0)
userEntry = StringVar()
e1 = Entry(self, width=40, textvariable=userEntry)
e1.grid(row=1, column=1, columnspan=6)
e2 = Button(self, text="Enter", command=submit_value)
e2.grid(row=1, column=5, columnspan=10)
b1 = Button(self, text="Start", width=10,padx=10,pady=10, command=button_func)
b1.grid(row=2, column=0)
b2 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func)
b2.grid(row=2, column=3)
b3 = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=button_func)
b3.grid(row=2, column=4)
b4 = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=button_func)
b4.grid(row=2, column=5)
b5 = Button(self, text="Loot", width=10,padx=10,pady=10, command=button_func)
b5.grid(row=2, column=6)
b6 = Button(self, text="Leave", width=10,padx=10,pady=10, command=button_func)
b6.grid(row=2, column=7)
stats = Listbox(self, width= 20)
stats.insert(1,"health:")
stats.grid(row=3, column=0, columnspan=8, sticky='news')
root = Tk()
root.title = "Test program"
tw = TestClient(root)
root.mainloop()
userEntry
是 TestClient.__init__
的局部变量。它不能在函数范围之外使用。您可以使用全局变量来解决这个问题:
from tkinter import *
from sys import exit
userEntry = object
def button_func():
print("Test")
def submit_value():
global userEntry
print("Entered Value: %s" % (userEntry.get()))
class TestClient(Frame):
def __init__(self, master):
global userEntry
Frame.__init__(self, master)
self.pack()
for n in range(3):
self.grid_rowconfigure(n, weight=1)
for n in range(8):
self.grid_columnconfigure(n, weight=1)
lb1 = Listbox(self, width=20,height=24)
lb1.insert(1,"WoW")
lb1.grid(row=0, column=0, columnspan=2, sticky='news')
t1 = Text(self, width=60)
t1.grid(row=0, column=3, columnspan=3)
lb2 = Listbox(self, width=20,height=24)
lb2.insert(1,"Hi")
lb2.grid(row=0, column=6, columnspan=2, sticky='news')
la1 = Label(self, text="Value entry:")
la1.grid(row=1, column=0)
userEntry = StringVar()
e1 = Entry(self, width=40, textvariable=userEntry)
e1.grid(row=1, column=1, columnspan=6)
e2 = Button(self, text="Enter", command=submit_value)
e2.grid(row=1, column=5, columnspan=10)
b1 = Button(self, text="Start", width=10,padx=10,pady=10, command=button_func)
b1.grid(row=2, column=0)
b2 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func)
b2.grid(row=2, column=3)
b3 = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=button_func)
b3.grid(row=2, column=4)
b4 = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=button_func)
b4.grid(row=2, column=5)
b5 = Button(self, text="Loot", width=10,padx=10,pady=10, command=button_func)
b5.grid(row=2, column=6)
b6 = Button(self, text="Leave", width=10,padx=10,pady=10, command=button_func)
b6.grid(row=2, column=7)
stats = Listbox(self, width= 20)
stats.insert(1,"health:")
stats.grid(row=3, column=0, columnspan=8, sticky='news')
root = Tk()
root.title = "Test program"
tw = TestClient(root)
root.mainloop()
以上代码将 userEntry
创建为全局变量,因此它可以在整个程序的任何地方使用。