Tkinter汉堡点餐系统:Order Again按钮返回菜单页面的实现问题
Tkinter汉堡点餐系统:Order Again按钮无法返回菜单问题修复
问题描述
我开发基于Tkinter的汉堡点餐系统时遇到问题:点击Order Again按钮后,无法回到菜单页面,目前仅能关闭弹窗,无法返回可选择订单的主页面。尝试多种方法仍未解决,希望获得帮助修复代码。
原代码:
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()
修复后的代码
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 - 指定父组件为当前Frame self.button1 = tk.Button(self, 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, parent, patty, condiments, add_ons, quantity, drink): # 使用传入的parent创建弹窗,避免依赖全局变量 new_window = tk.Toplevel(parent) 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() #
相关产品推荐
相关产品推荐

