Previous topic: Python 3.4 tkinter checkbutton variable handling not working / responding
After a long thread, the problem seems to be related to the IntVar declaration before running root.mainloop (). Apparently, the code from the previous thread works with Python 2.6.6 (thanks to PM 2Ring) after the syntax changes are made. You can see the full code in the previous thread. The following is an example of (approximately) minimal, complete, and verified:
import tkinter as tk
class Thing(tk.Frame):
def Switch(self):
if self.anyVar.get():
state = "disabled"
else:
state = "normal"
print(state)
self.entry.configure(state=state)
def createWidgets(self):
print("testbefore")
self.anyVar = tk.IntVar()
print("testafter")
tk.Label(self,text="Test",font=("Times New Roman",15)).grid(row=0,column=0,sticky="W",padx=5,pady=5)
self.box = tk.Checkbutton(self,variable=self.anyVar,command=self.Switch)
self.box.grid(row=1,column=1,sticky="W",padx=0,pady=5)
self.entry = tk.Entry(self,width=2)
self.entry.grid(row=2,column=1,sticky="W",padx=2,pady=5)
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.pack()
self.parent = parent
self.createWidgets()
class Framework(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.instances = []
self.parent = parent
thing = Thing(self)
self.instances.append(thing)
def Activity(self):
self.Clear()
self.instances[0].pack()
def Initialise(window):
window.master = tk.Frame(window)
window.master.grid()
window.instances = Framework(window.master)
window.instances.grid()
root = tk.Tk()
Initialise(root)
root.mainloop()
root.destroy()
The code will be executed until self.anyVar = tk.IntVar () is reached, after which the program freezes, but no error message is indicated. "testafter" is never printed. Any idea why this is so? Thanks.