Radio Buttons and Variables

Gordon Williams g_will at cyberus.ca
Fri Jul 16 12:44:17 EDT 1999


I'm having trouble getting radio buttons and variables working properly. 
The code works properly the first time it is run from Pythonwin but the
second time the variable does not update properly.  This means that
foovar.get()=0 regardless of the radio button pressed.

Each time the script is run, print foovar indicates a new variable (eg
PY_VAR0 then PY_VAR1 etc).  Could this be the problem?  

The script below prints the value of the radio button variable when the
button at the bottom is pushed.

I'm sure this is a common problem but could not find any reference to it.

Any ideas to solve this?

Thanks,
Gordon Williams

"""
 
Contents:
---------

HorizRadioGroup:
  horizontal and vertical radiobutton groups,

HorizButtonSet:
  horizontal and vertical sets of buttons

MenuBar:
  quick and easy menubar management using too complex data structures,
  but it works.

"""

from Tkinter import *
from types import IntType
import sys

class MenuBar(Frame):
    def __init__(self, menus, master=None):
	Frame.__init__(self, master, relief=RAISED, bd=2)
	for menu in menus:
	    title = menu['title']
	    items = menu['items']
	    del menu['items']
	    del menu['title']
	    button = Menubutton(self, text = title)
	    button.pack( side = LEFT, padx = '1m')

	    themenu = Menu(button, menu, tearoff=0)  # should make tearoff be
configureable

	    for item in items:
		try:
		    mtype = item['type']
		    del item['type']
		except: mtype = 'command'
		try:
		    mlabel = item['label']
		    del item['label']
		except: mlabel = None
		try: 
		    mcommand = item['command']
		    del item['command']
		except: mcommand = None
		cnf = item
		if mlabel: cnf['label'] = mlabel
		if mcommand: cnf['command'] = mcommand
		
		themenu.add(mtype, cnf)

	    button['menu'] = themenu
	

class RadioGroup(Frame):
    def __init__(self, elements, default, theanchor = W, master = None):
	Frame.__init__(self, master)
	self.elements = []
	for i in range(len(elements)):
	    e = elements[i]
	    thetext = e['text']
	    thevar = e['variable']
	    print thetext, thevar, i, thevar.get()   #diagonstic only
	    b = Radiobutton(self, text=thetext, variable=thevar,
			    value=`i`, anchor=theanchor)
	    self.elements.append(b)
	    if i == default:
		self.elements[i].select()

	    if theanchor == W:
		theside = LEFT
	    else: 
		theside = TOP
		
	    self.elements[i].pack(side=theside, expand=1, fill=BOTH)


class HorizRadioGroup(RadioGroup):
    def __init__(self, elements, thedefault, master = None):
	RadioGroup.__init__(self, elements, thedefault, W, master)


class ButtonGroup(Frame):
    def __init__(self, elements, theanchor = W, master = None):
	Frame.__init__(self, master)
	self.elements = []
	for i in range(len(elements)):
	    e = elements[i]
	    thetext = e['text']
	    thecommand = e['command']
	    
	    b = Button(self, text=thetext, command=thecommand)
	    self.elements.append(b)
	    if theanchor == W:
		theside = LEFT
	    else: 
		theside = TOP
		
	    self.elements[i].pack(side=theside, expand=1,
				  anchor = S, fill=BOTH)


class HorizButtonGroup(ButtonGroup):
    def __init__(self, elements, master = None):
	ButtonGroup.__init__(self, elements, W, master)




if __name__ == '__main__':
    root=Tk()
    def newfile():
	print 'New File...'
    def openfile():
	print 'Open File...'
    def spam1():
	print 'Spam! 1'
	print 'the variable is: Choice', foovar.get()+1
    def spam2():
	print 'Spam! 2'
	print 'the variable is: Choice', foovar.get()+1
    def spam3():
	print 'Spam! 3'
	print 'the variable is: Choice', foovar.get()+1

    mdef = [{'title':'File',
	     'items':[{'command':newfile, 'label':'New'},
		      {'command':openfile, 'label':'Open'},
		      {'type':'separator'},
		      {'command':sys.exit, 'label':'Quit'},
		      ],
	     },
	    {'title':'Edit',
	     'items':[{'label':'Cut'},
		      {'label':'Copy'},
		      {'label':'Paste', 'state':'disabled'}
		      ]
	     },
	    ]
    m = MenuBar(mdef,master=root)
    m.pack(side=TOP, fill = BOTH)

    foovar = IntVar()
    
    elements = [
	{'text' : 'Choice 1',
	 'variable' : foovar},
	{'text' : 'Choice 2',
	 'variable' : foovar},
	{'text' : 'Choice 3',
	 'variable' : foovar},
	]

    elements2 = [
	{'text' : 'Choice 1',
	 'command' : spam1},
	{'text' : 'Choice 2',
	 'command' : spam2},
	{'text' : 'Choice 3',
	 'command' : spam3},
	]

    a = HorizRadioGroup(elements, 0,master=root)
    a.pack(side=TOP, fill=BOTH)

    c = HorizButtonGroup(elements2,master=root)
    c.pack(side=BOTTOM, fill=X)
    
    root.mainloop()






More information about the Python-list mailing list