1

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
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
codehunt
  • 43
  • 4

3 Answers3

3
box.state(['!alternate'])  #box appear unchecked
box.state(['selected'])  #box appear checked
pkanda
  • 151
  • 3
  • 10
1

Use the .invoke() method, but from what I read elsewhere is will also call the command, if one is associated. The instance I was trying to use this, my check button didn't have a command as a parameter, so this worked perfectly for me.

Hope this helps and good luck!

0

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)
Ronald
  • 2,930
  • 2
  • 7
  • 18