[Tkinter-discuss] initial state of radiobutton

Iain Day iain at day-online.org.uk.invalid
Sun Aug 17 15:50:34 CEST 2008


Fredrik Lundh wrote:
> Iain Day wrote:
> 
>> def edit_preferences(event=None):
>>     prefs = Preferences(master, title="Preferences")
>>
>> class Preferences(tkSimpleDialog.Dialog):
>>     def body(self, master):
>>         settings = tk.LabelFrame(master, text="Settings")
>>         settings.grid(row=0, column=0)
>>         tk.Label(settings, text="Angular Units: ").grid(row=0, 
>> column=0, sticky=tk.E)
>>         angles = tk.StringVar(value='radians')
> 
>           self.angles = angles # keep a reference to the variable
> 
>>         tk.Radiobutton(settings, text="Degrees", 
>> variable=angles,value='degrees').grid(row=0, column=1)
>>         tk.Radiobutton(settings, text="Radians", 
>> variable=angles,value='radians').grid(row=0, column=2)
> 
> Widgets don't keep references to their option values, so unless you 
> explicitly keep a reference to the variable yourself (e.g. via an 
> instance attribute, as above), it'll be removed at the end of the method.

Okay, thanks. Not quite sure I follow though, later in my code I have

tk.Label(files, text="Working dir: ").grid(row=0, column=0, sticky=tk.E)
tk.Entry(files, textvariable=workdir).grid(row=0, column=1, sticky=tk.W)

and this worked okay, without the self.var = var bit. Is there a 
difference between the two?

Thanks,

Iain



More information about the Tkinter-discuss mailing list