【问题标题】:when opened a new frame and then going back to the home page, the home page messes up当打开一个新框架然后返回主页时,主页混乱
【发布时间】:2021-05-30 08:16:33
【问题描述】:

当我登录时一切都很好,它把我带到主页,当我点击查看菜单然后点击返回按钮时,它把我带回到主页,一切都很好,我想要的方式 然而当我点击订单菜单然后按返回按钮返回主页时,我的主页一团糟,我看到 "function:" 函数的部分内容,我该如何解决这个问题?
对不起,如果代码有点长,我已经删掉了大部分不必要的代码(或试图)。谢谢你的帮助

从 tkinter 导入* 从 PIL 导入图像,ImageTk 将 tkinter 导入为 tk

root = Tk()
root.geometry('670x466')

accounts = []
food = ['Pizza','Burger','Nachos', 'French Toast']
foodprice=['20','9.50','7.50', '17']
drinks = ['Pepsi','Lemonade','Tea', 'Aperitivo Spritz']
drinksprice = ['3','4','3', '15.50']

class Goode_brothers:


def __init__(self, parent):

    self.myFrame = Frame(parent)
    self.myFrame.pack()


    self.load = Image.open('new-dip-project\\food.jpg')
    self.render = ImageTk.PhotoImage(self.load)
    self.img = Label(parent, image = self.render)
    self.img.place(x = -26, y =0)

    self.img_login = PhotoImage(file = 'new-dip-project\\button (3).png')
    self.b1 = Button(parent,image = self.img_login, command = self.read_info, bd = 0, bg = '#3b353b', activebackground = '#3b353b')
    self.b1.place(x = 275, y = 340)



    self.img_register = PhotoImage(file = 'new-dip-project\\register.png')
    self.b2 = Button(parent,image = self.img_register, command = self.openNewWindow, bd = 0, bg = '#3b353b', activebackground = '#3b353b')
    self.b2.place(x = 265, y = 400)

    self.canvas = Canvas(parent, width = 400, height = 120)
    self.canvas.pack()
    self.img4 = ImageTk.PhotoImage(Image.open('new-dip-project\\goode.png'))
    self.canvas.create_image(20, 20, anchor=NW, image=self.img4)



    self.email = Entry(parent)
    self.email.place(x = 340, y = 180)

    self.password = Entry(parent)
    self.password.place(x = 354, y = 250)

    self.img_label = PhotoImage(file = 'new-dip-project\\label-image.png')
    self.name = Label(parent, image = self.img_label, text = "Email:", bg = '#3c3a3b').place(x = 197,y = 178)

    self.img_label_pass = PhotoImage(file = 'new-dip-project\\label_pass.png')
    self.name = Label(parent, image = self.img_label_pass, text = "Password:", bg = '#3c3a3b').place(x = 177,y = 245)




def openMenu(self):

    for wid in root.winfo_children():
        wid.destroy()
    self.myFrame.destroy()

    self.myFrame2 = Frame(root, bg = '')
    self.myFrame2.pack(fill = "both", expand = 1)


    self.img77 = PhotoImage(file = 'new-dip-project\\goode.png')
    self.name77 = Label(self.myFrame2, image = self.img77).pack()

    self.img_menu = PhotoImage(file = 'new-dip-project\\menu_button.png')
    self.b6 = Button(self.myFrame2,image = self.img_menu, command = self.view_menu, bd  = 0)
    self.b6.place(x = 246, y = 140)

    self.img_order = PhotoImage(file = 'new-dip-project\\order_button.png')
    self.b7 = Button(self.myFrame2,image = self.img_order, command = self.order_menu, bd  = 0)
    self.b7.place(x = 239, y = 228)

    self.img_checkout = PhotoImage(file = 'new-dip-project\\checkout.png')
    self.b8 = Button(self.myFrame2,image = self.img_checkout, bd  = 0)
    self.b8.place(x = 250, y = 316)



def view_menu(self):

    self.myFrame2.destroy()
    self.myFrame3 = LabelFrame(root, height = 700)
    self.myFrame3.pack()

    self.myFrame3.columnconfigure(0, weight=1)
    self.myFrame3.columnconfigure(1, weight=2)

    self.food_title = Label(self.myFrame3, font=("Impact", "23"), text = 'Food').grid(row = 0, column = 4)
    self.food_space = Label(self.myFrame3, text = '').grid(row = 1, column = 4)
    self.drinks_title = Label(self.myFrame3, font=("Impact", "23"), text = 'Drinks').grid(row = 8, column = 4)

    self.price = Label(self.myFrame3, font=("Impact", "23"), text = 'Price($)').grid(row = 0, column = 8)


    for x in range (len(food)):
        self.foodop = Label(self.myFrame3, font=("Impact", "15"), text = food[x]).grid(row = 3+x, column = 4) #A created label defining where it is positioned
        self.fprice = Label(self.myFrame3, font=("Impact", "15"), text = foodprice[x]).grid(row = 3+x, column = 8)

    for x in range (len(drinks)):
        self.drinksop = Label(self.myFrame3, font=("Impact", "15"), text = drinks[x]).grid(row = 5+(len(food))+x, column = 4)
        self.drinksp = Label(self.myFrame3, font=("Impact", "15"), text = drinksprice[x]).grid(row = 5+(len(food))+x, column = 8)


    self.img_back = PhotoImage(file = 'new-dip-project\\back_button.png')
    self.b10 = Button(self.myFrame3,image = self.img_back, command = self.openMenu, bd  = 0)
    self.b10.grid(row = 38, column = 7)



def order_menu(self):
    for wid2 in root.winfo_children():
        wid2.destroy()
    self.myFrame2.destroy()
    self.myFrame4 = Frame(root)
    self.myFrame4.pack(fill = "both", expand = 1)

    self.tkvar = StringVar(self.myFrame4)
    self.tkvar.set("Food")

    self.tkvar2 = StringVar(self.myFrame4)
    self.tkvar2.set("Drinks")

    self.img_odmenu = PhotoImage(file = 'new-dip-project\\od_menu.png')
    self.order_menu_message = Label(self.myFrame4, image = self.img_odmenu).place(x = 220)

    self.foodMenu = OptionMenu(self.myFrame4, self.tkvar, *food)
    self.foodMenu.place(x = 160, y = 110)

    self.Foodlabel = Label(self.myFrame4, text="Choose Your Food", font=("Courier New","12"))
    self.Foodlabel.place(x = 145, y = 83)

    self.drinklabel = Label(self.myFrame4, text="Choose Your Drink", font=("Courier New","12"))
    self.drinklabel.place(x = 370, y = 83)

    self.drinkMenu = OptionMenu(self.myFrame4, self.tkvar2, *drinks)
    self.drinkMenu.place(x = 385, y = 110)

    self.pricelabel = Label(self.myFrame4, text = "Total price", font=("Courier New","12"))
    self.pricelabel.place(x = 289, y = 208)

    self.order_btn78 = PhotoImage(file = 'new-dip-project\\orderb.png')
    self.order_btn = Button(self.myFrame4, image = self.order_btn78, bd = 0)
    self.order_btn.place(x = 302, y = 160)

    self.check_btn = PhotoImage(file = 'new-dip-project\\checkpay.png')
    self.checkout_btn = Button(self.myFrame4, image = self.check_btn, bd = 0)
    self.checkout_btn.place(x = 267, y = 410)

    self.img_odmenu = PhotoImage(file = 'new-dip-project\\od_menu.png')
    self.order_menu_message = Label(self.myFrame4, image = self.img_odmenu).place(x = 220)

    self.foodMenu = OptionMenu(self.myFrame4, self.tkvar, *food)
    self.foodMenu.place(x = 160, y = 110)

    self.Foodlabel = Label(self.myFrame4, text="Choose Your Food", font=("Courier New","12"))
    self.Foodlabel.place(x = 145, y = 83)

    self.drinklabel = Label(self.myFrame4, text="Choose Your Drink", font=("Courier New","12"))
    self.drinklabel.place(x = 370, y = 83)

    self.drinkMenu = OptionMenu(self.myFrame4, self.tkvar2, *drinks)
    self.drinkMenu.place(x = 385, y = 110)

    self.pricelabel = Label(self.myFrame4, text = "Total price", font=("Courier New","12"))
    self.pricelabel.place(x = 289, y = 208)

    self.order_btn78 = PhotoImage(file = 'new-dip-project\\orderb.png')
    self.order_btn = Button(self.myFrame4, image = self.order_btn78, bd = 0)
    self.order_btn.place(x = 302, y = 160)

    self.check_btn = PhotoImage(file = 'new-dip-project\\checkpay.png')
    self.checkout_btn = Button(self.myFrame4, image = self.check_btn, bd = 0)
    self.checkout_btn.place(x = 267, y = 410)

    self.back_menu = PhotoImage(file = 'new-dip-project\\bbutton.png')
    self.back_button2 = Button(self.myFrame4, image = self.back_menu, command = self.openMenu, bd  = 0)
    self.back_button2.place(x = 30, y = 410)




if __name__ == "__main__":
    e = Goode_brothers(root)
    root.title('Goode brothers')
    root.mainloop()

【问题讨论】:

  • 我的建议是如果你想回到主页,那么你不应该破坏框架或小部件,相反,你应该使用place_forget()或@987654323隐藏框架@ 或 grid_forget() 如果您分别使用过 .place,.pack.grid
  • 你没有提供back_menu
  • 我希望函数“def openMenu(self):”在我按下此按钮时运行“self.back_button2 = Button(self.myFrame4, image = self.back_menu, command = self.openMenu, bd = 0)" 我不是已经这样做了吗?它可以工作并将我带回主页,但主页外观看起来像我在帖子中发送的第三张图片,我希望它看起来像第一张图片。
  • 我不明白你为什么要破坏框架。你应该使用pack_forget() 而不是for widget in root.winfo_children()widget.destroy() 然后frame.destroy()
  • self.self.myFrame2 = Frame(root, bg = '') 中删除bg = ''

标签: python tkinter


【解决方案1】:

你必须缩进 Goode Brothers 类下的方法

【讨论】:

    猜你喜欢
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2013-04-17
    • 2011-10-24
    相关资源
    最近更新 更多