Export to GitHub

python-ttk - issue #10

No way to deselect radiobuttons and checkbuttons


Posted on Jul 20, 2009 by Swift Hippo

What steps will reproduce the problem? 1. radio1 = ttk.Radiobutton(student, text = "Above Average", variable=verbal, value='Above Average') radio1.grid(row=33, column=0, sticky=W) 2. check1 = ttk.Checkbutton(student, text=name, variable=var, onvalue=onv, offvalue=0) 3. radio1.deselect() check1.deselect()

What is the expected output? What do you see instead? In Tkinter, checks and radios can be cleared in a form with the simple deselect() command, but Ttk doesn't seem to support that...or any other method of clearing them, for that matter.

What version of the product are you using? On what operating system? Windows XP Pro Python 2.6.2 Latest download of Ttk from here

Please provide any additional information below.

Comment #1

Posted on Jul 21, 2009 by Quick Horse

Ttk radiobutton and Ttk checkbutton do not implement deselect, as you noted.

Instead, you should change the variable's value in order to deselect checkbutton and radiobuttons. The following example should help:

import ttk import Tkinter

root = Tkinter.Tk()

testvar = Tkinter.IntVar(value=2) testvar2 = Tkinter.StringVar(value='a')

rb = ttk.Radiobutton(text='Radiobutton 1', variable=testvar, value=1) rb1 = ttk.Radiobutton(text='Radiobutton 2', variable=testvar, value=2) cb = ttk.Checkbutton(text='Checkbutton 1', variable=testvar2, onvalue='a', offvalue=0) rb.pack() rb1.pack() cb.pack()

testvar.set(None) testvar2.set(str(cb['offvalue']))

root.mainloop()

Thanks for noticing that, it could be a start of a document that describes differences when porting from Tk to Ttk.

Comment #2

Posted on Jul 21, 2009 by Swift Hippo

Thanks for the fix, my form is back to clear again. P.S. A friend of mine actually found an easier one: button.state('!selected') seems to turn them off too.

Comment #3

Posted on Jul 21, 2009 by Quick Horse

I'm not sure if I would call that method easier. Changing button's state requires knowing which button is currently selected, or you could do that for every button. On the other hand, resetting the variable's value is a one-time operation and doesn't require knowing which button is selected (so I consider it much better).

Comment #4

Posted on Jul 21, 2009 by Swift Hippo

It was easier for me because I only had a few radio buttons and I had all my checkbuttons in an array, so I just looped through them, but I see your point. Either way, problem solved.

Status: Fixed

Labels:
Type-Defect Priority-Medium