0

I am trying to create checkbox with default value True,but it's not working, I tried plenty of the answers but didn't work

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

I coouln't find much on it in the documentaion other than select which didn't work, Also on this question Tkinter: is there a way to check checkboxes by default?

stackover
  • 35
  • 3

1 Answers1

1
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()
  • It works for me though. try copy pasting the whole code. Your code already works, u just didn't add self in the checkvar and checkbutton – Ryan Kenneth Cabrera Mar 15 '20 at 18:34
  • 1
    This does not work for me, I already have the variable assigned to self so it is not being garbage collected. The button still renders initially unchecked even though the IntVar has a value of 1 and it will update to 0 and back to 1 as I interact with it. Python 3.9 – user2665773 Sep 03 '21 at 19:11