首页 > 解决方案 > 无法为 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):
        #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()

select除了哪些不起作用之外,我在文档中找不到太多关于它的内容,还有关于这个问题Tkinter: 有没有办法默认检查复选框?

标签: pythontkinter

解决方案


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()

推荐阅读