检查按钮返回值

时间:2016-10-12 08:13:57

标签: python tkinter

在这段代码中,我看不到值是如何变化的,它总是由于某种原因显示PY_VAR1。我在网上研究了一下,我似乎无法找到一个直截了当的答案,如果按下它就能获得价值。

from Tkinter import *
import tkMessageBox
import Tkinter


top = Tkinter.Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
def test():
    print(CheckVar2)
C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \
             onvalue = 1, offvalue = 0, height=5, \
             width = 20)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \
                  height=5, onvalue = 1, offvalue = 0,\
                 width = 20, command = test)
C1.pack()

C2.pack()
top.mainloop()

将来我需要使用if语句和一切,如果值为1等

1 个答案:

答案 0 :(得分:1)

.get()将解决您的问题:)

from Tkinter import *
import tkMessageBox
import Tkinter


top = Tkinter.Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
def test():
    print(CheckVar2.get()) # Notice the .get()
C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \
             onvalue = 1, offvalue = 0, height=5, \
             width = 20)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \
                  height=5, onvalue = 1, offvalue = 0,\
                 width = 20, command = test)
C1.pack()

C2.pack()
top.mainloop()

打印

1
0
1
0

点击第二个框