tkinter如何获得选择框状态?

Susurrant 2015-10-06 07:18:26
如单选框Checkbutton,如何判断选中和未选中状态呢,我输出:
from tkinter import *

def p():
print(chk['variable'])

root = Tk()
var = IntVar()
chk = Checkbutton(root, text = 'checkbutton', variable = var, command = p)
chk.pack()

root.mainloop()


variable不应该默认为0或者1表示未勾选或者勾选吗?
为什么不管勾没勾选,就是一直输出PY_VARO,应该怎么判断呢?
...全文
471 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
panghuhu250 2015-10-06
  • 打赏
  • 举报
回复
这里面(http://effbot.org/tkinterbook/checkbutton.htm)提到:
引用
variable= Associates a Tkinter variable to the button. When the button is pressed, the variable is toggled between offvalue and onvalue. Explicit changes to the variable are automatically reflected by the buttons. (variable/Variable)
所以chk的状态体现在var的值里面。如何存取var的值可以看IntVar的文档:http://effbot.org/tkinterbook/variable.htm。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧