- #1
Eclair_de_XII
- 1,083
- 91
- TL;DR Summary
- I'm trying to create a frame of Radiobutton widgets, but for some odd reason, the widgets are already filled before the user even selects them. I've tried tinkering with the type of the variable linked to the widgets, but the problem still persists.
Whenever I use a BooleanVar, the side_dish attribute keeps evaluating to False even if I set it to None. So I tried setting it to a StringVar. This time, both the Radiobutton seem to be auto-filled, even though nothing is even selected. If anyone could shed some light on why this happens, I would very much appreciate it. I am not sure if tkinter is still commonly used, but I am intent on sticking to it, either way.
Python:
from tkinter import *
class Restaurant:
def __init__(self):
self.side_dish=None
self.root=Tk()
self.root.title('Waiter')
self.mainFrame=Frame(self.root)
self.mainFrame.grid()
def endProcess(self,*args):
self.root.destroy()
def initializeParams(self,varType):
if varType == 'str':
self.side_dish=StringVar()
self.soupVal='Soup'
self.saladVal='Salad'
elif varType == 'bool':
self.side_dish=BooleanVar()
self.soupVal=False
self.saladVal=True
def chooseSide(self):
question='Would you like the salad or the soup?'
# To create white-space so that title shows
question+=' '*5
Label(self.mainFrame,text=question).grid(row=0,columnspan=2)
d={}
d['varType']='bool'
self.initializeParams(**d)
kw={
'master':self.mainFrame,\
'variable':self.side_dish,\
'command':self.printSelection
}
soupButton=Radiobutton(text='Soup',value=self.soupVal,**kw)
saladButton=Radiobutton(text='Salad',value=self.saladVal,**kw)
buttonPos={
'column':0,\
'sticky':W
}
soupButton.grid(row=1,**buttonPos)
saladButton.grid(row=2,**buttonPos)
soupButton.focus()
def printSelection(self,*args):
chose_salad=self.side_dish.get()
kw={
'master':self.mainFrame
}
Label(text='User chose salad:',**kw).grid(row=1,column=1)
Label(text='%s'%chose_salad,**kw).grid(row=2,column=1)
self.root.bind_all('<Return>',self.endProcess)
def __call__(self):
self.root.bind_all('<Escape>',self.endProcess)
self.chooseSide()
if __name__ == '__main__':
Restaurant()()