Radiobutton choices auto-filled before selection

  • Python
  • Thread starter Eclair_de_XII
  • Start date
  • Tags
    Choices
In summary: If I wanted to add another choice, I would simply add another radiobutton with a new value and update the corresponding variable in the initializeParams function. The solution works because I am using the IntVar to map to the integer values of the radiobuttons.
  • #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()()
 
Technology news on Phys.org
  • #2
Boolean is a bad choice for radio buttons - what happens when you want to add another choice? If you want this to be a boolean value then use a checkbox.

Although you initially set side_dish to None you override it in initializeParams to a tk.StringVar. I am not an expert on tkinter but you could try self.side_dish.set('') after line 16.
 
  • #3
Eclair_de_XII said:
Whenever I use a BooleanVar, the side_dish attribute keeps evaluating to False even if I set it to None.

Of course, because None is not a boolean value in Python; only False and True are. Whenever you hand an object to a BooleanVar, it converts it to a Python boolean value (using the built-in function bool, AFAIK, which evaluates to False if called with None as its argument).

Eclair_de_XII said:
So I tried setting it to a StringVar.

Normally, AFAIK, radio buttons are expected to map to zero-based integer values.
 
  • #4
PeterDonis said:
zero-based integer values

It baffles me how simple the solution I came up with was, based on this remark alone. Thanks.

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
        elif varType == 'int':
            self.side_dish=IntVar(value=-1)
            self.saladVal=1
            self.soupVal=0

    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']='int'

        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()()

pbuk said:
what happens when you want to add another choice

In my case, I only have the user choose from two options.
 
Last edited:

Related to Radiobutton choices auto-filled before selection

1. How do radiobutton choices get auto-filled before selection?

Radiobutton choices can be auto-filled through the use of a script or program that pre-selects a specific option before the user even clicks on it. This can be used for convenience or to guide the user towards a certain choice.

2. Can auto-filled radiobutton choices be changed by the user?

Yes, auto-filled radiobutton choices can still be changed by the user. The pre-selected option is simply a default, but the user can click on a different option to make a different selection.

3. How does auto-filling radiobutton choices impact user experience?

Auto-filling radiobutton choices can make the user experience more efficient and streamlined. It eliminates the need for the user to manually select an option, potentially saving time and reducing the risk of error.

4. Are there any potential downsides to using auto-filled radiobutton choices?

One potential downside is that it takes away some control from the user. They may feel restricted or frustrated if they are unable to change the auto-filled option. Additionally, if the auto-filled option is incorrect or not what the user intended, it could lead to confusion or mistakes.

5. Is there a way to disable the auto-fill feature for radiobutton choices?

Yes, the auto-fill feature for radiobutton choices can be disabled by removing the script or code that is responsible for pre-selecting an option. This can also be done by setting a default value to null or blank, so that no option is automatically selected.

Similar threads

  • Programming and Computer Science
Replies
4
Views
928
Back
Top