-1

how can I make the order again button work when pressed, it goes back to the menu page. I tried everything possible but still can't figure out how I could make it work. I can only make it close the window but not go back to the main page which is where I can select the orders that I want to. pls help or at least guide me on how I can improve and make this code work

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

class MainWindow(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title('Hamburger Ordering')
        self.frames = {}
        self.frames['MenuPage'] = MenuPage(self)

        self.change_window('MenuPage')
    
    def change_window(self, page_name):
        for frame in self.frames.values():
            frame.grid_remove()
        frame = self.frames[page_name]
        frame.grid(row=0, column=0)

class MenuPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.parent = parent

        # Select Patty Label
        self.label1 = tk.Label(self, text='Select Patty: ')
        self.label1.grid(row=0, column=0)

        # Set Value = ' ' in order for the radio buttons appear empty when the program runs
        self.patty = tk.StringVar(value=' ')

        # Radio Boxes
        self.radiobutton1 = tk.Radiobutton(self, text='Pork', variable=self.patty, value='Pork')
        self.radiobutton1.grid(row=1, column=0)

        self.radiobutton2 = tk.Radiobutton(self, text='Beef', variable=self.patty, value='Beef')
        self.radiobutton2.grid(row=1, column=1)

        self.radiobutton3 = tk.Radiobutton(self, text='Chicken', variable=self.patty, value='Chicken')
        self.radiobutton3.grid(row=1, column=2)

        self.radiobutton4 = tk.Radiobutton(self, text='Vegan', variable=self.patty, value='Vegan')
        self.radiobutton4.grid(row=1, column=3)

        self.radiobutton5 = tk.Radiobutton(self, text='No Patty', variable=self.patty, value='No Patty')
        self.radiobutton5.grid(row=1, column=4)

        # Select Condiments Label
        self.label2 = tk.Label(self, text='Condiment: ')
        self.label2.grid(row=3, column=0)

        # Check Boxes for Condiments
        self.var1 = tk.IntVar()
        self.var2 = tk.IntVar()
        self.var3 = tk.IntVar()
        self.var4 = tk.IntVar()

        self.checkbox1 = tk.Checkbutton(self, text='Ketchup', variable=self.var1)
        self.checkbox1.grid(row=4, column=0, padx=4, pady=5, sticky="w")

        self.checkbox2 = tk.Checkbutton(self, text='Mayo', variable=self.var2)
        self.checkbox2.grid(row=5, column=0, padx=4, pady=5, sticky="w")

        self.checkbox3 = tk.Checkbutton(self, text='Mustard', variable=self.var3)
        self.checkbox3.grid(row=6, column=0, padx=5, pady=5, sticky="w")

        self.checkbox4 = tk.Checkbutton(self, text='Hot Sauce', variable=self.var4)
        self.checkbox4.grid(row=7, column=0, padx=6, pady=5, sticky="w")

        # Select Add - ons Label
        self.label3 = tk.Label(self, text='Add - ons: ')
        self.label3.grid(row=3, column=1)

        # Check Boxes for Add - ons
        self.var5 = tk.IntVar()
        self.var6 = tk.IntVar()
        self.var7 = tk.IntVar()
        self.var8 = tk.IntVar()
        self.var9 = tk.IntVar()

        self.checkbox5 = tk.Checkbutton(self, text='Lettuce', variable=self.var5)
        self.checkbox5.grid(row=4, column=1, padx=6, pady=5, sticky="w")

        self.checkbox6 = tk.Checkbutton(self, text='Cheese', variable=self.var6)
        self.checkbox6.grid(row=5, column=1, padx=6, pady=5, sticky="w")

        self.checkbox7 = tk.Checkbutton(self, text='Pickles', variable=self.var7)
        self.checkbox7.grid(row=6, column=1, padx=6, pady=5, sticky="w")

        self.checkbox8 = tk.Checkbutton(self, text='Tomato', variable=self.var8)
        self.checkbox8.grid(row=7, column=1, padx=6, pady=5, sticky="w")

        self.checkbox9 = tk.Checkbutton(self, text='Cucumber', variable=self.var9)
        self.checkbox9.grid(row=8, column=1, padx=6, pady=5, sticky="w")

        # Quantity Picker
        self.label4 = tk.Label(self, text='Quantity: ')
        self.label4.grid(row=3, column=2)

        self.spinbox1 = tk.Spinbox(self, from_=1, to=10)
        self.spinbox1.grid(row=3, column=3)

        # Drinks Picker
        self.label5 = tk.Label(self, text='Drink: ')
        self.label5.grid(row=5, column=2)

        drink_list = ['Coke', 'Sprite', 'Pineapple', 'Iced Tea', 'Alfonso Light', 'Water','No Drinks']
        self.combobox = ttk.Combobox(self, values=drink_list)
        self.combobox.grid(row=5, column=3)

        # Confirm Order Button
        self.button1 = tk.Button(text = 'Confirm Order', command = self.confirm_order)
        self.button1.grid(row = 4, column = 3, padx=10, pady=10, sticky="e" )
        
    # Retrieves all the selected items and displays it in a messagebox    
    def confirm_order(self):
        patty = self.patty.get()
        condiments = [var.get() for var in [self.var1, self.var2, self.var3, self.var4]]
        add_ons = [var.get() for var in [self.var5, self.var6, self.var7, self.var8, self.var9]]
        quantity = self.spinbox1.get()
        drink = self.combobox.get()

        message = f'Patty: {patty}\n'
        message += 'Condiments: ' + ', '.join([condiment for i, condiment in enumerate(['Ketchup', 'Mayo', 'Mustard', 'Hot Sauce']) if condiments[i]]) + '\n'
        message += 'Add-ons: ' + ', '.join([add_on for i, add_on in enumerate(['Lettuce', 'Cheese', 'Pickles', 'Tomato', 'Cucumber']) if add_ons[i]]) + '\n'
        message += f'Quantity: {quantity}\n'
        message += f'Drink: {drink}'

        confirm_order = messagebox.askokcancel(title='Confirm Order', message=message)

        if confirm_order == True:
            self.parent.withdraw()
            self.ConfirmPage(self.parent, patty, condiments, add_ons, quantity, drink)

    def ConfirmPage(self, patty, condiments, add_ons, quantity, drink):
        new_window = tk.Toplevel(root)
        new_window.geometry("600x300")
        new_window.title('Order Confirmation')

        message = f'Thank you for ordering!\n\n'
        message += f'Patty: {patty}\n'
        message += 'Condiments: ' + ', '.join([condiment for i, condiment in enumerate(['Ketchup', 'Mayo', 'Mustard', 'Hot Sauce']) if condiments[i]]) + '\n'
        message += 'Add-ons: ' + ', '.join([add_on for i, add_on in enumerate(['Lettuce', 'Cheese', 'Pickles', 'Tomato', 'Cucumber']) if add_ons[i]]) + '\n'
        message += f'Quantity: {quantity}\n'
        message += f'Drink: {drink}'

        label = tk.Label(new_window, text=message)
        label.pack()

        button = tk.Button(new_window, text='Order Again', command=self.lift_menu)
        button.pack()

    def lift_menu(self):
        self.parent.lift()


if __name__ == '__main__':
    root = MainWindow()
    root.mainloop()
  • 3
    Post a [mre] and provide debugging details. Also it's unclear. Do you want to raise the root window? Then see [this question](https://stackoverflow.com/questions/1892339/how-to-make-a-tkinter-window-jump-to-the-front). – relent95 Mar 20 '23 at 07:56
  • Warm welcome to SO. Please try to use correct upper case letters, e.g. in the beginning of your title, sentences or the word "I". This would be gentle to your readers. Please read [How to ask](https://stackoverflow.com/help/how-to-ask) and [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Then update your question with code to show us what you have tried so far. – buhtz Mar 20 '23 at 13:39

1 Answers1

0

Edit: how to order again.

how can I make the order again button work when pressed, it goes back to the menu page

  • In line 134, remove self.parent in self.ConfirmPage(
  • In line 137-138, add self. to Toplevel. title and geometry.
  • In line 156, add self.new_window.destroy() in lift_menu function.
  • In line 157, add MainWindow() in lift_menu function.

Snippet:

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

class MainWindow(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title('Hamburger Ordering')
        self.frames = {}
        self.frames['MenuPage'] = MenuPage(self)

        self.change_window('MenuPage')
    
    def change_window(self, page_name):
        for frame in self.frames.values():
            frame.grid_remove()
        frame = self.frames[page_name]
        frame.grid(row=0, column=0)

class MenuPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.parent = parent

        # Select Patty Label
        self.label1 = tk.Label(self, text='Select Patty: ')
        self.label1.grid(row=0, column=0)

        # Set Value = ' ' in order for the radio buttons appear empty when the program runs
        self.patty = tk.StringVar(value=' ')

        # Radio Boxes
        self.radiobutton1 = tk.Radiobutton(self, text='Pork', variable=self.patty, value='Pork')
        self.radiobutton1.grid(row=1, column=0)

        self.radiobutton2 = tk.Radiobutton(self, text='Beef', variable=self.patty, value='Beef')
        self.radiobutton2.grid(row=1, column=1)

        self.radiobutton3 = tk.Radiobutton(self, text='Chicken', variable=self.patty, value='Chicken')
        self.radiobutton3.grid(row=1, column=2)

        self.radiobutton4 = tk.Radiobutton(self, text='Vegan', variable=self.patty, value='Vegan')
        self.radiobutton4.grid(row=1, column=3)

        self.radiobutton5 = tk.Radiobutton(self, text='No Patty', variable=self.patty, value='No Patty')
        self.radiobutton5.grid(row=1, column=4)

        # Select Condiments Label
        self.label2 = tk.Label(self, text='Condiment: ')
        self.label2.grid(row=3, column=0)

        # Check Boxes for Condiments
        self.var1 = tk.IntVar()
        self.var2 = tk.IntVar()
        self.var3 = tk.IntVar()
        self.var4 = tk.IntVar()

        self.checkbox1 = tk.Checkbutton(self, text='Ketchup', variable=self.var1)
        self.checkbox1.grid(row=4, column=0, padx=4, pady=5, sticky="w")

        self.checkbox2 = tk.Checkbutton(self, text='Mayo', variable=self.var2)
        self.checkbox2.grid(row=5, column=0, padx=4, pady=5, sticky="w")

        self.checkbox3 = tk.Checkbutton(self, text='Mustard', variable=self.var3)
        self.checkbox3.grid(row=6, column=0, padx=5, pady=5, sticky="w")

        self.checkbox4 = tk.Checkbutton(self, text='Hot Sauce', variable=self.var4)
        self.checkbox4.grid(row=7, column=0, padx=6, pady=5, sticky="w")

        # Select Add - ons Label
        self.label3 = tk.Label(self, text='Add - ons: ')
        self.label3.grid(row=3, column=1)

        # Check Boxes for Add - ons
        self.var5 = tk.IntVar()
        self.var6 = tk.IntVar()
        self.var7 = tk.IntVar()
        self.var8 = tk.IntVar()
        self.var9 = tk.IntVar()

        self.checkbox5 = tk.Checkbutton(self, text='Lettuce', variable=self.var5)
        self.checkbox5.grid(row=4, column=1, padx=6, pady=5, sticky="w")

        self.checkbox6 = tk.Checkbutton(self, text='Cheese', variable=self.var6)
        self.checkbox6.grid(row=5, column=1, padx=6, pady=5, sticky="w")

        self.checkbox7 = tk.Checkbutton(self, text='Pickles', variable=self.var7)
        self.checkbox7.grid(row=6, column=1, padx=6, pady=5, sticky="w")

        self.checkbox8 = tk.Checkbutton(self, text='Tomato', variable=self.var8)
        self.checkbox8.grid(row=7, column=1, padx=6, pady=5, sticky="w")

        self.checkbox9 = tk.Checkbutton(self, text='Cucumber', variable=self.var9)
        self.checkbox9.grid(row=8, column=1, padx=6, pady=5, sticky="w")

        # Quantity Picker
        self.label4 = tk.Label(self, text='Quantity: ')
        self.label4.grid(row=3, column=2)

        self.spinbox1 = tk.Spinbox(self, from_=1, to=10)
        self.spinbox1.grid(row=3, column=3)

        # Drinks Picker
        self.label5 = tk.Label(self, text='Drink: ')
        self.label5.grid(row=5, column=2)

        drink_list = ['Coke', 'Sprite', 'Pineapple', 'Iced Tea', 'Alfonso Light', 'Water','No Drinks']
        self.combobox = ttk.Combobox(self, values=drink_list)
        self.combobox.grid(row=5, column=3)

        # Confirm Order Button
        self.button1 = tk.Button(text = 'Confirm Order', command = self.confirm_order)
        self.button1.grid(row = 4, column = 3, padx=10, pady=10, sticky="e" )
        
    # Retrieves all the selected items and displays it in a messagebox    
    def confirm_order(self):
        patty = self.patty.get()
        condiments = [var.get() for var in [self.var1, self.var2, self.var3, self.var4]]
        add_ons = [var.get() for var in [self.var5, self.var6, self.var7, self.var8, self.var9]]
        quantity = self.spinbox1.get()
        drink = self.combobox.get()

        message = f'Patty: {patty}\n'
        message += 'Condiments: ' + ', '.join([condiment for i, condiment in enumerate(['Ketchup', 'Mayo', 'Mustard', 'Hot Sauce']) if condiments[i]]) + '\n'
        message += 'Add-ons: ' + ', '.join([add_on for i, add_on in enumerate(['Lettuce', 'Cheese', 'Pickles', 'Tomato', 'Cucumber']) if add_ons[i]]) + '\n'
        message += f'Quantity: {quantity}\n'
        message += f'Drink: {drink}'

        confirm_order = messagebox.askokcancel(title='Confirm Order', message=message)

        if confirm_order == True:
            self.parent.withdraw()
            self.ConfirmPage(patty, condiments, add_ons, quantity, drink)

    def ConfirmPage(self, patty, condiments, add_ons, quantity, drink):
        self.new_window = tk.Toplevel(root)
        self.new_window.geometry("600x300")
        self.new_window.title('Order Confirmation')

        message = f'Thank you for ordering!\n\n'
        message += f'Patty: {patty}\n'
        message += 'Condiments: ' + ', '.join([condiment for i, condiment in enumerate(['Ketchup', 'Mayo', 'Mustard', 'Hot Sauce']) if condiments[i]]) + '\n'
        message += 'Add-ons: ' + ', '.join([add_on for i, add_on in enumerate(['Lettuce', 'Cheese', 'Pickles', 'Tomato', 'Cucumber']) if add_ons[i]]) + '\n'
        message += f'Quantity: {quantity}\n'
        message += f'Drink: {drink}'

        label = tk.Label(self.new_window, text=message)
        label.pack()

        button = tk.Button(self.new_window, text='Order Again', command=self.lift_menu)
        button.pack()

    def lift_menu(self):         
        self.parent.lift()
        self.new_window.destroy()
        MainWindow()
         

if __name__ == '__main__':
    root = MainWindow()
    root.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19