0
from Tkinter import *
window = Tk()
CheckVar1 = IntVar()
cbttn = Checkbutton(text="Caps?", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height=5, \
width = 20)
cbttn.grid(row=3, column=0)
while True:
    if CheckVar1 == 0:
        a = Button(text="a").grid(column=1)
        b = Button(text="b").grid(column=1)
        c = Button(text="c").grid(column=1)
        d = Button(text="d").grid(column=1)
        e = Button(text="e").grid(column=1)
        f = Button(text="f").grid(column=1)
        g = Button(text="g").grid(column=1)
    if CheckVar1 == 1:
        A = Button(text="A").grid(column=1)
    window.mainloop()#makes everything work

I want to find out how to make different buttons appear depending on what the value of CheckVar1 is. I have no errors, but the code I want to work are the if CheckVar1 == 0: and if CheckVar1 == 1.

1 Answers1

1

Use the trace method on your IntVar.

from Tkinter import *
window = Tk()
CheckVar1 = IntVar()
cbttn = Checkbutton(text="Caps?", variable = CheckVar1, onvalue = 1, offvalue = 0, height=5,width = 20)
cbttn.grid(row=3, column=0)

def trace_method(*args):
    print ("Changed")
    if CheckVar1.get() == 0: #note that you need to call `get` method on the IntVar
        a = Button(text="a").grid(column=1)
        b = Button(text="b").grid(column=1)
        c = Button(text="c").grid(column=1)
        d = Button(text="d").grid(column=1)
        e = Button(text="e").grid(column=1)
        f = Button(text="f").grid(column=1)
        g = Button(text="g").grid(column=1)
    elif CheckVar1.get() == 1:
        A = Button(text="A").grid(column=1)

CheckVar1.trace("w",trace_method)

window.mainloop()
Henry Yik
  • 22,275
  • 4
  • 18
  • 40