'Tkinter menu screen select

Hey I was wondering if there is a way to switch between the two pages that are defined as "Breakfast" and "Burgers" while still remembering the already entered amounts and not just stacking the widgets ontop of eachother. This will be much clearer when you run the program. Sorry for the size of the code and thankyou to anyone who took the time to have a crack, its much appreciated.

from tkinter import *
import tkinter as tk
import webbrowser
from tkinter.ttk import Combobox
from tkinter import ttk
window = Tk()

#Remembers product values
global Mighty_McMuffin
global Bacon_Egg_McMuffin
global Sausage_Egg_McMuffin
global Big_Mac
global Big_Mac_Bacon
global Quarter_Pounder

#Prices for products
Mighty_McMuffin = 1
Bacon_Egg_McMuffin = 1
Sausage_Egg_McMuffin = 1
Big_Mac = 1
Big_Mac_Bacon = 1
Quarter_Pounder = 1

#Sets maximum number
global numbers
numbers = list(range(11))

#Define the style for combobox widget
style= ttk.Style()
style.theme_use("clam") #clam,alt,default,classic
window.option_add("*TCombobox*Listbox*selectBackground", "grey")
window.option_add("*TCombobox*Listbox*Background", "gold")


def breakfast():
    #Remembers variables
    global option1
    global option2
    global option3
 
    #Breakfast row 1 column 1 set
    name1 = Label(window, text="Mighty McMuffin", fg='black', font=("Helvetica", 10))
    name1.place(x = 50, y = 10)
    option1 = ttk.Combobox(window, width = 23,values = numbers)
    option1.place(x = 50, y = 35)
    option1['state'] = 'readonly'
    option1.set(0)
    
    #Breakfast row 1 column 2 set
    name2 = Label(window, text="Bacon and Egg McMuffin", fg='black', font=("Helvetica", 10))
    name2.place(x = 250, y = 10)
    option2 = Combobox(window, width = 23, values = numbers)
    option2.place(x = 250, y = 35)
    option2['state'] = 'readonly'
    option2.set(0)

    #Breakfast row 1 column 3 set
    name3 = Label(window, text="Sausage and Egg McMuffin", fg='black', font=("Helvetica", 10))
    name3.place(x = 450, y = 10)
    option3 = Combobox(window, width = 23, values = numbers)
    option3.place(x = 450, y = 35)
    option3['state'] = 'readonly'
    option3.set(0)

    
def burgers():

    #Remembers variables
    global option4
    global option5
    global option6
    
    #Burgers row 1 column 1 set
    name4 = Label(window, text="Big Mac", fg='black', font=("Helvetica", 10))
    name4.place(x = 50, y = 10)
    option4 = ttk.Combobox(window, width = 23,values = numbers)
    option4.place(x = 50, y = 35)
    option4['state'] = 'readonly'
    option4.set(0)

    #Burgers row 1 column 2 set
    name5 = Label(window, text="Big Mac and Bacon", fg='black', font=("Helvetica", 10))
    name5.place(x = 250, y = 10)
    option5 = Combobox(window, width = 23, values = numbers)
    option5.place(x = 250, y = 35)
    option5['state'] = 'readonly'
    option5.set(0)

    #Burgers row 1 column 3 set
    name6 = Label(window, text="Quarter Pounder", fg='black', font=("Helvetica", 10))
    name6.place(x = 450, y = 10)
    option6 = Combobox(window, width = 23, values = numbers)
    option6.place(x = 450, y = 35)
    option6['state'] = 'readonly'
    option6.set(0)
    
def complete_order():

    #Prices for number of products
    product1 = int(option1.get()) * Mighty_McMuffin
    product2 = int(option2.get()) * Bacon_Egg_McMuffin
    product3 = int(option3.get()) * Sausage_Egg_McMuffin
    product4 = int(option4.get()) * Big_Mac
    product5 = int(option5.get()) * Big_Mac_Bacon
    product6 = int(option6.get()) * Quarter_Pounder
    
    #Calculates prices for breakfast
    breakfast_price = product1 + product2 + product3
    print("The Total price of your order is $", breakfast_price)
  
def window_setup():
    #Window config
    menu = Menu(window, background = "black", activebackground = "gold")
    window.geometry("700x500")
    window.config(menu = menu)
    window.title("McDonalds")

    #Finish menue bar
    finishmenu = Menu(menu, background = "gold", activebackground = "grey")
    menu.add_cascade(label='Finish', menu = finishmenu)
    finishmenu.add_command(label='Complete Order', command = lambda: complete_order())
    finishmenu.add_separator()
    finishmenu.add_command(label='Quit', command=quit)

    #Help menue bar
    helpmenu = Menu(menu, background = "gold", activebackground = "grey")
    menu.add_cascade(label='Help', menu = helpmenu)
    helpmenu.add_command(label='Visit Website', command = lambda: webbrowser.open('https://mcdonalds.com.au/'))
    helpmenu.add_command(label='Contact us', command = lambda: webbrowser.open('https://mcdonalds.com.au/contact-and-help'))
    helpmenu.add_command(label='FAQs', command = lambda: webbrowser.open('https://mcdonalds.com.au/faqs'))
    helpmenu.add_separator()
    helpmenu.add_command(label='Exit', command = window.quit)

    #Food menue bar
    foodmenu = Menu(menu, background = "gold", activebackground = "grey")
    menu.add_cascade(label='Food', menu = foodmenu)
    foodmenu.add_command(label='Breakfast', command = lambda: breakfast())
    foodmenu.add_command(label='Burgers', command = lambda: burgers())
    foodmenu.add_separator()
    foodmenu.add_command(label='Exit', command = window.quit)
    
window_setup()


Solution 1:[1]

Instead of creating Combobox as global variables, create IntVar() global variables for each Combobox.

Just add those global variables as textvariable to each Combobox and after creating each Combobox widget, set its value using .set() method.

option1 = IntVar()
option2 = IntVar()
option3 = IntVar()

def breakfast():
    #Remembers variables
    global option1
    global option2
    global option3
 
    #Breakfast row 1 column 1 set
    name1 = Label(window, text="Mighty McMuffin", fg='black', font=("Helvetica", 10))
    name1.place(x = 50, y = 10)
    optionBox1 = ttk.Combobox(window, width = 23,values = numbers, textvariable=option1)
    optionBox1.place(x = 50, y = 35)
    optionBox1['state'] = 'readonly'
    optionBox1.set(option1.get())

As a suggestion, do not just create new widgets every time for each breakfast() and burgers(), as you may look closely - the widgets are overlapping. First, remove/forget those widgets already created that you don't want to display.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kartikeya