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

6

33

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.

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()
Rostrum answered 2/6, 2016 at 14:43 Comment(2)
Just tried it on my machine and it works perfectly. There must be a problem somewhere else in your code.Brynnbrynna
It should work. If not, then provide your full code related to this.Haile
A
46

Your CheckVar is 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:

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

You can also use the select function of the checkbutton:

self.checkbutton.select()
Andrus answered 2/6, 2016 at 15:15 Comment(3)
I am getting 'Checkbutton' has no attribute 'select' with ttk 0.3.1Perspiration
@xjcl: then you must be using the Checkbutton from ttk instead of tkinter.Andrus
Thank you @BryanOakley! What a frustration when looking at identical code occurring at different locations in the program and not getting the same result.Crooked
S
19

I think the function you are looking for is .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.

Sherlock answered 2/6, 2016 at 14:59 Comment(0)
S
5

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

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

from Tkinter import *

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

or use toggle to switch between the two:

self.checkbutton.toggle()

Silky answered 3/10, 2019 at 15:26 Comment(0)
P
1

I was using ttk and the only thing that worked was

self.checkbutton.state(["selected"])

Check the state via

"selected" in self.checkbutton.state()
Perspiration answered 13/1, 2022 at 16:14 Comment(0)
T
1

Thought I'd just post the sample code that worked for me, this creates a checked checkbox.

window1 = tkinter.Tk()
check1 = tkinter.IntVar()

checkbox1 = tkinter.Checkbutton(window1, variable=check1, text="")
checkbox1.grid(row=0, column=0)
checkbox1.select()
Trigeminal answered 4/4, 2023 at 13:50 Comment(0)
S
-1
var4 = IntVar(value=1)
Checkbutton(root, text="New", variable=var4, bg="light green").grid(row=12, column=0)

This will check all the check boxs.

Sniffy answered 3/1, 2020 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.