Python中的“选中所有框"复选框 [英] 'Check all boxes' Check box in Python

查看:38
本文介绍了Python中的“选中所有框"复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 python 和 tkinter 我有一个应用程序可以动态创建许多复选框(大约 40 个),我想在它们之前添加另一个可以检查所有复选框

Using python and tkinter I have an app that dynamically creates lots of check boxes (around 40) and i would like to add another one that preceeds them that can check all of the checkboxes

这方面的一个例子是电子邮件...当您想删除大量电子邮件时,您可以选中全选"复选框.

An example of this is in email... when you want to delete large quanitities of email you can check the 'Select all' checkboxs.

我的问题是如何将此功能添加到我的 Python GUI 应用程序中.我认为这与 chk_btn.config(state="") 但我不确定

My question is how can I add this functionality to my python GUI app. I think it's to do with chk_btn.config(state="") but im not sure

感谢所有帮助!

推荐答案

动态创建复选框通常是个坏主意.它可能会导致诸如丢失按钮跟踪之类的问题.但是,我写的这个小脚本应该演示如何做你想做的事:

Creating Checkboxes dynamically is usually a bad idea. It can lead to problems such as losing track of the buttons. However, this mini script I wrote should demonstrate how to do what you want:

from Tkinter import *

root = Tk()

# Create a dictionary where the keys are the checkbuttons
# and the values are their BooleanVars
buttons = dict()
for _ in xrange(10):
    buttons[Checkbutton(root)] = BooleanVar()

for button in buttons:
    # Place each button on the window
    button.grid()
    # Hook each button up to its BooleanVar
    button.config(variable=buttons[button])
    # Set each BooleanVar to True
    buttons[button].set(True)

# I printed this just to show what is going on
print buttons

root.mainloop()

让我免于忘记按钮的是我制作的用于存储它们和它们的 BooleanVars 的字典.如果您想访问按钮或其变量,您可以在该字典中找到它.

What saves me from losing track of the buttons is the dictionary I made to store them and their BooleanVars. If you want to access either a button or its variable, you can find it in that dictionary.

这篇关于Python中的“选中所有框"复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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