Tkinter Radiobutton Menu: how to select programmatically (newbie)

Mark C Favas mark at chem.uwa.edu.au
Sun Oct 3 20:35:01 EDT 1999


"Charles Medcoff" <cmedcoff at sprynet.com> writes:

>How does one programmatically select the active entry a radio button menu?
>For example in a drawing application with a tool menu with entries for
>"Selector", "Line", "Rectangle", etc I want to automatically change the
>active drawing task to "Selector" and I need to have the radiobutton menu
>reflect this.

You need to set a variable to be a Tkinter StringVar(), and use the 
variable=variable_name construct when building your radiobutton widget. You
can then set the value by doing variable_name.set('Selector') to select the
required button. A few lines of code follow to give you a flavour.
/F's www.pythonware.com site has excellent reference material for Tkinter. 

import Tkinter; Tk = Tkinter
<snip>
radvar = Tk.StringVar()
<snip>
for text, value in radiobuttons:
   wid = Tk.Radiobutton(parent, text=text, variable=radvar, value=value)
<snip>
radvar.set(val) # val is one of the "value"s in the above loop

Mark
--
Email  - mark at chem.uwa.edu.au      ,-_|\                           Mark C Favas
Phone  - +61 9 380 3482           /     \               Department of Chemistry
Fax    - +61 9 380 1005      ---> *_,-._/   The University of Western Australia
                                       v                               Nedlands
Loc    - 31.97 S, 115.81 E                               Western Australia 6009




More information about the Python-list mailing list