获取 Checkbutton 状态 [英] Getting Checkbutton state

查看:71
本文介绍了获取 Checkbutton 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得 Tkinter Checkbutton'state'?通过 'state' 我的意思是了解它是否有复选标记.

解决方案

当您创建它时,它需要一个 variable 关键字参数.从 Tkinter 传递一个 IntVar.选中或取消选中该框会将 var 包含的值设置为相应的布尔状态.这可以作为 var.get() 访问:

检查 => var.get()

未检查 => not var.get()

<预><代码>>>>根 = Tkinter.Tk()>>>var = Tkinter.IntVar()>>>chk = Tkinter.Checkbutton(root, text='foo', variable=var)>>>chk.pack(side=Tkinter.LEFT)>>>var.get() #unchecked0>>>var.get() #checked1

How do I get the 'state' of a Tkinter Checkbutton? By 'state' I mean get whether or not it has a check mark in it or not.

解决方案

When you're creating it, it takes a variable keyword argument. Pass it an IntVar from Tkinter. Checking or unchecking the box will set that value contained by var to the corresponding boolean state. This can be accessed as var.get():

checked => var.get()

not checked => not var.get()

>>> root = Tkinter.Tk()
>>> var = Tkinter.IntVar()
>>> chk = Tkinter.Checkbutton(root, text='foo', variable=var)
>>> chk.pack(side=Tkinter.LEFT)
>>> var.get()  #unchecked
0
>>> var.get()  #checked
1

这篇关于获取 Checkbutton 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆