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

我想了解如何根据 的值来显示不同的按钮CheckVar1。我没有错误,但我想要工作的代码是if CheckVar1 == 0:and if CheckVar1 == 1

4

1 回答 1

1

使用trace你的方法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()
于 2019-04-17T13:58:30.227 回答