【发布时间】:2016-02-16 03:21:55
【问题描述】:
EDIT 现在已移至Python 3.4.3 tkinter - Program freezes on declaration of IntVar or any other tkinter data type,因为这是问题的根源,现已解决。基本上,永远不要使用 tkinter 在 Python 3.x 中命名任何“大师”,它会导致无限循环:(。我的完整代码现在已被删除,因为它是我的课程作业,不希望人们对它进行刻痕:D 编辑
我对 tkinter 比较陌生,看不出哪里出错了。我尝试使用 StringVar() 和常规字符串来获取多个输入字段以在启用 Checkbutton 时禁用。这是问题所在的框架:
class CActivity(tk.Frame):
def Clear(self):
pass # To be completed
def Today(self):
if self.todayVar == "ON":
self.day.configure(state="disabled")
self.month.configure(state="disabled")
self.year.configure(state="disabled")
else:
self.day.configure(state="normal")
self.month.configure(state="normal")
self.year.configure(state="normal")
def createWidgets(self):
self.title = tk.Label(self)
self.title["text"] = "Add an Activity"
self.title["font"] = ("Times New Roman",30)
self.title["fg"] = "purple"
self.title.grid(row=0,column=0,sticky="W",padx=5,pady=5)
self.todayVar = ""
tk.Label(self,text="Activity Name:",font=("Times New Roman",15)).grid(row=1,column=0,sticky="W",padx=5,pady=5)
name = tk.Entry(self).grid(row=1,column=1,columnspan=3,sticky="E",padx=5,pady=5)
tk.Label(self,text="Priority:",font=("Times New Roman",15)).grid(row=2,column=0,sticky="W",padx=5,pady=5)
priority = tk.Checkbutton(self).grid(row=2,column=1,sticky="W",padx=0,pady=5)
tk.Label(self,text="Today?",font=("Times New Roman",15)).grid(row=3,column=0,sticky="W",padx=5,pady=5)
today = tk.Checkbutton(self,onvalue="ON",offvalue="OFF",variable=self.todayVar,command=self.Today).grid(row=3,column=1,sticky="W",padx=0,pady=5) #problem possibly on this line
tk.Label(self,text="Date (DD/MM/YYYY):",font=("Times New Roman",15)).grid(row=4,column=0,sticky="W",padx=5,pady=5)
day = tk.Entry(self,width=2).grid(row=4,column=1,sticky="W",padx=2,pady=5)
month = tk.Entry(self,width=2).grid(row=4,column=2,sticky="W",padx=2,pady=5)
year = tk.Entry(self,width=4).grid(row=4,column=3,sticky="W",padx=2,pady=5)
self.clear = tk.Button(self, command=self.Clear)
self.clear["text"] = "Clear"
self.clear["font"] = ("Times New Roman",15)
self.clear["fg"] = "red"
self.clear.grid(row=7,column=4,sticky="WE",padx=5,pady=5)
self.back = tk.Button(self)
self.back["text"] = "Back"
self.back["font"] = ("Times New Roman",15)
self.back["fg"] = "red"
self.back["command"] = self.parent.Menu
self.back.grid(row=8,column=4,sticky="WE",padx=5,pady=5)
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.pack()
self.parent = parent
self.createWidgets()
这里是使用 StringVar() 而不是标准的 Python 字符串:
class CActivity(tk.Frame):
def Clear(self):
pass # To be completed
def Today(self):
if self.todayVar.get() == "ON":
self.day.configure(state="disabled")
self.month.configure(state="disabled")
self.year.configure(state="disabled")
else:
self.day.configure(state="normal")
self.month.configure(state="normal")
self.year.configure(state="normal")
def createWidgets(self):
self.title = tk.Label(self)
self.title["text"] = "Add an Activity"
self.title["font"] = ("Times New Roman",30)
self.title["fg"] = "purple"
self.title.grid(row=0,column=0,sticky="W",padx=5,pady=5)
self.todayVar = tk.StringVar()
tk.Label(self,text="Activity Name:",font=("Times New Roman",15)).grid(row=1,column=0,sticky="W",padx=5,pady=5)
name = tk.Entry(self).grid(row=1,column=1,columnspan=3,sticky="E",padx=5,pady=5)
tk.Label(self,text="Priority:",font=("Times New Roman",15)).grid(row=2,column=0,sticky="W",padx=5,pady=5)
priority = tk.Checkbutton(self).grid(row=2,column=1,sticky="W",padx=0,pady=5)
tk.Label(self,text="Today?",font=("Times New Roman",15)).grid(row=3,column=0,sticky="W",padx=5,pady=5)
today = tk.Checkbutton(self,onvalue="ON",offvalue="OFF",variable=self.todayVar,command=self.Today).grid(row=3,column=1,sticky="W",padx=0,pady=5)
tk.Label(self,text="Date (DD/MM/YYYY):",font=("Times New Roman",15)).grid(row=4,column=0,sticky="W",padx=5,pady=5)
day = tk.Entry(self,width=2).grid(row=4,column=1,sticky="W",padx=2,pady=5)
month = tk.Entry(self,width=2).grid(row=4,column=2,sticky="W",padx=2,pady=5)
year = tk.Entry(self,width=4).grid(row=4,column=3,sticky="W",padx=2,pady=5)
self.clear = tk.Button(self, command=self.Clear)
self.clear["text"] = "Clear"
self.clear["font"] = ("Times New Roman",15)
self.clear["fg"] = "red"
self.clear.grid(row=7,column=4,sticky="WE",padx=5,pady=5)
self.back = tk.Button(self)
self.back["text"] = "Back"
self.back["font"] = ("Times New Roman",15)
self.back["fg"] = "red"
self.back["command"] = self.parent.Menu
self.back.grid(row=8,column=4,sticky="WE",padx=5,pady=5)
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.pack()
self.parent = parent
self.createWidgets()
在使用标准字符串的情况下,程序运行良好,直到您单击 Checkbutton,此时 Checkbutton 变为灰色,然后程序停止响应。在使用 StringVar() 的情况下,tk 窗口根本不会加载,因为该框架在窗口初始化期间被初始化。感谢您的帮助,如果您想要完整的代码来帮助找到问题,请告诉我。
【问题讨论】:
-
self.todayVar = tk.StringVar() 是示例中错误所在的行,因为程序在到达该行时会冻结(使用调试打印)。我认为这可能与在初始化 tk 窗口期间引用 StringVar() 时有关?
-
@PM2Ring 太好了,谢谢。
标签: python python-3.x tkinter