Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法设置tkinter复选框的默认值_Python_Tkinter - Fatal编程技术网

Python 无法设置tkinter复选框的默认值

Python 无法设置tkinter复选框的默认值,python,tkinter,Python,Tkinter,我正在尝试创建默认值为True的复选框,但它不起作用,我尝试了很多答案,但都不起作用 import tkinter as tk class App(tk.Tk): def __init__(self): super().__init__() self.title("Test v1") self.geometry("400x250") self.build_init() def build_init(self):

我正在尝试创建默认值为True的复选框,但它不起作用,我尝试了很多答案,但都不起作用

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Test v1")
        self.geometry("400x250")
        self.build_init()
    def build_init(self):
        #CheckVar = tk.BooleanVar(self,)
        CheckVar = tk.IntVar(value=1)
        checkbutton = tk.Checkbutton(self, text = "Test", variable = CheckVar,onvalue=1, offvalue=0)
        #checkbutton.select()
        checkbutton.place(x=20,y=80)
App().mainloop()

除了选择不起作用之外,我在文档中找不到太多关于它的内容,也在这个问题上

CheckVar.get?@JoshuaNixon你是什么意思?CheckVar作为一个局部变量,在函数的末尾不再存在-使Checkbutton无处存储其状态。@jasonharper ok,但它应该会显示出来,因为我还没有正确地使用这个函数?Tkinter中几乎没有任何事情会立即发生-对小部件状态的更新会由Main循环处理,因此您必须在Checkbutton出现之前退出该函数并丢失Var。不过,它对我来说是有效的。尝试复制粘贴整个代码。你的代码已经运行了,只是没有在checkvar和checkbutton中添加self
import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Test v1")
        self.geometry("400x250")
        self.build_init()
    def build_init(self):
        #CheckVar = tk.BooleanVar(self,)
        self.CheckVar = tk.IntVar(value=1)
        self.checkbutton = tk.Checkbutton(self, text = "Test", variable = self.CheckVar,onvalue=1, offvalue=0)
        #checkbutton.select()
        self.checkbutton.place(x=20,y=80)
App().mainloop()