5. Add Radio Buttons for pizza sizes and OptionMenu for quantity

../_images/pizza_4.png
  • Objective: Add radio buttons for selecting pizza sizes and the OptionMenu for quantity.

  • Content:

    • Creating and positioning radio buttons for Pizza sizes.

    • Introduction to OptionMenu.

    • Creating and positioning the OptionMenu for selecting the quantity of pizzas.


5.1. Creating and Positioning Radio Buttons for Pizza Sizes

Place the following code below the other widget code in Section "4. TKINTER WIDGETS".
This code sets up a GUI for selecting a pizza size using radio buttons.
The selected pizza size is stored in the size_var variable.
The pack method ensures that the radio buttons are neatly aligned within the size_frame, making the GUI intuitive and easy to use.
# Pizza Size (Radio buttons)
size_label = tk.Label(root, text="Pizza Size:")
size_label.grid(row=2, column=0, padx=10, pady=5, sticky="ne")

size_var = tk.StringVar(root)
size_var.set("Small")

size_frame = tk.Frame(root)
size_frame.grid(row=2, column=1, padx=10, pady=5, sticky="w")

for size in ["Small", "Medium", "Large"]:
    radio_button = tk.Radiobutton(size_frame, text=size, variable=size_var, value=size)
    radio_button.pack(anchor="w")
  1. Label Creation:

    pizza_label = tk.Label(root, text="Pizza Size:")
    pizza_label.grid(row=2, column=0, padx=10, pady=5, sticky="ne")
    
    • This line creates a label widget with the text "Pizza Size:".

    • The grid method places the label in the third row (row=2), first column (column=0) of the grid layout.

    • padx and pady add padding around the label for better spacing.

    • sticky="ne" aligns the label to the northeast (top-right) of its grid cell.

  2. StringVar Initialization:

    size_var = tk.StringVar(root)
    size_var.set("Small")
    
    • size_var is a StringVar object that holds the value of the selected pizza size.

    • size_var.set("Small") sets the default value to "Small".

  3. Frame Creation:

    size_frame = tk.Frame(root)
    size_frame.grid(row=2, column=1, padx=10, pady=5, sticky="w")
    
    • This creates a frame widget to contain the radio buttons.

    • The grid method places the frame in the third row (row=2), second column (column=1) of the grid layout.

    • sticky="w" aligns the frame to the west (left side) of its grid cell.

  4. Radio Buttons Creation:

    for size in ["Small", "Medium", "Large"]:
            radio_button = tk.Radiobutton(size_frame, text=size, variable=size_var, value=size)
            radio_button.pack(anchor="w")
    
    • This loop creates a radio button for each pizza size in the list.

    • Each Radiobutton is placed inside size_frame.

    • text=size sets the label of the radio button.

    • variable=size_var links the radio button to the size_var variable.

    • value=size sets the value of size_var when the radio button is selected.

    • pack(anchor="w") arranges the radio buttons vertically, aligned to the left.


5.2. Creating and Positioning the OptionMenu

Place the following code below the other widget code in Section "4. TKINTER WIDGETS".
This code sets up a GUI for selecting a quantity using an OptionMenu.
OptionMenu is a dropdown menu that allows users to select one option from a list of quantities of pizzas.
The selected quantity is stored in the quantity_var variable.
The grid method ensures that the label and the OptionMenu are neatly aligned within the grid layout.
# Quantity (Dropdown Menu)
quanitity_label = tk.Label(root, text="Quantity:")
quanitity_label.grid(row=3, column=0, padx=10, pady=5, sticky="e")
quantity_var = tk.IntVar(root)
quantity_var.set(1)

quantity_menu = tk.OptionMenu(root, quantity_var, 1, 2, 3, 4, 5, 6)
quantity_menu.grid(row=3, column=1, padx=10, pady=5, sticky="w")
  1. Label Creation:

    quanitity_label = tk.Label(root, text="Quantity:")
    quanitity_label.grid(row=3, column=0, padx=10, pady=5, sticky="e")
    
    • This line creates a label widget with the text "Quantity:".

    • The grid method places the label in the fourth row (row=3), first column (column=0) of the grid layout.

    • padx and pady add padding around the label for better spacing.

    • sticky="e" aligns the label to the east (right side) of its grid cell.

  2. IntVar Initialization:

    See: https://python-course.eu/tkinter/variable-classes-in-tkinter.php

    quantity_var = tk.IntVar(root)
    quantity_var.set(1)
    
    • quantity_var is a IntVar object that holds the value of the selected quantity, as an integer.

    • quantity_var.set(1) sets the default value to 1.

  3. OptionMenu Creation:

    quantity_menu = tk.OptionMenu(root, quantity_var, 1, 2, 3, 4, 5, 6)
    quantity_menu.grid(row=3, column=1, padx=10, pady=5, sticky="w")
    
    • This creates an OptionMenu widget for selecting a quantity.

    • The OptionMenu is associated with the root window and linked to the quantity_var variable.

    • The options available in the menu are 1, 2, 3, 4, 5 and 6.

    • The grid method places the OptionMenu in the fourth row (row=3), second column (column=1) of the grid layout.

    • padx and pady add padding around the menu for better spacing.

    • sticky="w" aligns the menu to the west (left side) of its grid cell.