Python Tkinter:有没有办法默认选中复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37595078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:36:52  来源:igfitidea点击:

Tkinter: is there a way to check checkboxes by default?

pythoncheckboxtkinter

提问by Marvin Lerousseau

I have this piece of code that will create a simple checkbox :

我有这段代码可以创建一个简单的复选框:

from Tkinter import *

CheckVar = IntVar()
self.checkbutton = Checkbutton(self.root, text = "Test", variable = CheckVar)

However this checkbox in unchecked by default and I'm searching for a way to check it.

但是,默认情况下未选中此复选框,我正在寻找一种方法来检查它。

So far I have tried to insert

到目前为止,我已经尝试插入

CheckVar.set(1)

right after CheckVar but it didn't work.

在 CheckVar 之后,但它没有用。

Thanks for your help

谢谢你的帮助

Edit : here is my full piece of code. When I run it, the box is still unchecked

编辑:这是我的完整代码。当我运行它时,该框仍未选中

from Tkinter import *

class App():
    def __init__(self, root):   
        self.root = root
        CheckVar = IntVar()
        CheckVar.set(1)
        self.checkbutton = Checkbutton(self.root, text = "Test", variable = CheckVar)
        self.checkbutton.grid(row=0, column=0,)


root = Tk()
app = App(root)
root.mainloop()

回答by Bryan Oakley

Your CheckVaris a local variable. It's getting garbage collected. Save it as an object attribute. Also, you can create the variable and initialize it all in one step:

CheckVar是一个局部变量。它正在收集垃圾。将其另存为对象属性。此外,您可以一步创建变量并对其进行初始化:

self.CheckVar = IntVar(value=1)
self.checkbutton = Checkbutton(..., variable = self.CheckVar)

回答by Gunner Stone

I think the function you are looking for is .select()

我认为你正在寻找的功能是 .select()

This function selects the checkbutton (as can be assumed from the function name)

此函数选择复选按钮(从函数名称可以推测)

Try calling this function after your widget is defined:

定义小部件后尝试调用此函数:

from Tkinter import *

CheckVar = IntVar()
self.checkbutton = Checkbutton(self.root, text = "Test", variable = CheckVar)
self.checkbutton.select()

By calling the function right after the widget is created, it looks as though it's selected by default.

通过在小部件创建后立即调用该函数,它看起来好像是默认选中的。

回答by 98Ed

Just adding onto GunnerStone's answer - Because I was looking for something where I can reset my values/checkboxes.

只是添加到 GunnerStone 的答案 - 因为我正在寻找可以重置我的值/复选框的东西。

If you'd like to de-selectthe checkbox value for whatever reason, use deselect():

如果您de-select出于任何原因想要复选框值,请使用deselect()

from Tkinter import *

CheckVar = IntVar()
self.checkbutton = Checkbutton(self.root, text = "Test", variable = CheckVar)
self.checkbutton.deselect()

or use toggleto switch between the two:

或用于toggle在两者之间切换:

self.checkbutton.toggle()

self.checkbutton.toggle()

回答by Paul Trovato

var4 = IntVar(value=1)
    Checkbutton(root, text="New", variable=var4, bg="light green").grid(row=12, column=0)

This code will check all the boxes when you run the program.

当您运行程序时,此代码将选中所有框。