JosephTLyons
JosephTLyons

Reputation: 2133

Python TKinter Checkbutton Half States

I notice that when clicking the unchecked checkbox, the box switches over to a "half-state," before flipping to a fully checked. It looks like this:

enter image description here

Is there any way to prevent this? Checking / unchecking takes longer than it should because of this.

#!/usr/bin/python
from random import randint
from Tkinter import *

# Set up main window settings
master = Tk()
master.title ("Window")
master.resizable(width = FALSE, height = FALSE)

def femaleOption():
    maleCheckbox.deselect()

def maleOption():
    femaleCheckbox.deselect()

#Create female checkbox
femaleIsChecked = IntVar()
femaleCheckbox = Checkbutton(master, text = "Female", command = femaleOption, variable = femaleIsChecked)
femaleCheckbox.select()

#Create male checkbox
maleCheckbox = Checkbutton(master, text = "Male", command = maleOption)

femaleCheckbox.pack()
maleCheckbox.pack()

master.mainloop()

Upvotes: 0

Views: 1540

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386362

This "half state" represents the tristate value. Depending on how you configure this, you'll usually see this when the value of the associated variable is the empty string. That is because the default tristatevalue is the empty string.

From the canonical tcl/tk documentation:

If a checkbutton is selected then the indicator is normally drawn with a selected appearance, and a Tcl variable associated with the checkbutton is set to a particular value (normally 1). The indicator is drawn with a check mark inside. If the checkbutton is not selected, then the indicator is drawn with a deselected appearance, and the associated variable is set to a different value (typically 0). The indicator is drawn without a check mark inside. In the special case where the variable (if specified) has a value that matches the tristatevalue, the indicator is drawn with a tri-state appearance and is in the tri-state mode indicating mixed or multiple values.

What this means is that somewhere in your code you're likely setting the variable to an empty string, either explicitly or implicitly.

The way to prevent this is to make sure that the associated variables are properly defined (ie: not local variables), and that they are always set to either the onvalue (default 1) or offvalue (default 0).

Upvotes: 4

Related Questions