Search code examples
pythontkinterttktkinter.checkbuttonttkwidgets

ttk checkbutton is not deselected by default


Please kick here to see the current output and expected output

I have a simple python program where i want to deselect the checkbutton by default. I want to see it the same way as when a user unchecks a tick box. Please let me know how to achieve it.

from tkinter import *
from tkinter import ttk

def urgentReq():
    global box
    state = box.state()
    if(box.instate(['selected'])):        
        print ("--> Urgent: ",state)
    else:
        print ("--> Not Urgent:",state)

gui = Tk()
gui.title("GUI")
gui.geometry('200x150')

box = ttk.Checkbutton(gui, text ='Urgent Request', command=lambda: urgentReq())
box.grid(column=1, row=4, pady=40, sticky="N")
#write something here to unselect the box by default

Solution

  • Somehow the initial state of the CheckButton = ('alternate',).

    There is a workaround I found here: tkk checkbutton appears when loaded up with black box in it. If you apply it to your code like this, it seems to work:

    checkVar = IntVar()
    box = ttk.Checkbutton(gui, text ='Urgent Request', command=lambda: urgentReq(), variable=checkVar)