简体   繁体   中英

All of my Radiobuttons are selected in Python's tkinter

I'm trying to make a simple pizza ordering GUI, but when I load the radioButtons for the pizza sizes, all of the buttons are being selected at once.

EDIT: After fixing it up, the buttons are no longer all selected on startup. Now, none of them are selected on startup but when you hover over the buttons they select themselves, and it is possible to have multiple options selected.

Here's the code so far:

from tkinter import *

class Pizza(Frame):
    """Initialize the Pizza program"""

    def __init__(self, master):
        """This sets up the Pizza program"""
        super(Pizza, self).__init__(master)
        self.grid()  # This is absolutely vital for future GUI projects!
        self.create_widgets()
        self.name = ""
        self.size = StringVar()
        self.size.set(value="small")

    def create_widgets(self):
        """This creates the input types"""
        # Adds text field for username entry

        # Not important for the question.

        # Adds Radiobuttons for users to interact with
        self.size_label = Label(self, text="Size:")
        self.size_label.grid(row=1, column=0)

        self.size_sml = Radiobutton(self, variable=self.size, value="small", text="Small")
        self.size_sml.grid(row=1, column=1)

        self.size_med = Radiobutton(self, variable=self.size, value="medium", text="Medium")
        self.size_med.grid(row=1, column=2)

        self.size_lrg = Radiobutton(self, variable=self.size, value="large", text="Large")
        self.size_lrg.grid(row=1, column=3)


root = Tk()
root.title("Order Pizza")
root.resizable(width=False, height=False)
app = Pizza(root)

root.mainloop()

The function call given in command attribute shouldn't include parentheses ()

This is because when the function is mentioned with parentheses, it gets called when the create_widgets function is called

You shall change it to become self.size_sml = Radiobutton(self, command=self.small, text="Small")

Your problem is solved. You cannot put self.create_widgets() anywhere inside def __init__(self, master):

You must put at the end of def __init__(self, master):

Change this in line 10:

def __init__(self, master):
    """This sets up the Pizza program"""
    super(Pizza, self).__init__(master)
    self.grid()  # This is absolutely vital for future GUI projects!
    self.create_widgets() #<=== line 10
    self.name = ""
    self.size = StringVar()
    self.size.set(value="small")

to move line 10 to line 14:

def __init__(self, master):
    """This sets up the Pizza program"""
    super(Pizza, self).__init__(master)
    self.grid()  # This is absolutely vital for future GUI projects!
    #self.create_widgets()  #comment out
    self.name = ""
    self.size = StringVar()
    self.size.set(value="small")
    self.create_widgets() #<====line 14

I'm unsure what the problem is. This section of the code seems to be fine. That being said i'd recommend that for the way you store what size people order, you'd be better off using numbers, eg small = 0 medium = 1 large = 2. You can then replace that later in the code because now it is just a little unusual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM