我不知道如何在 tkinter window 中获得预期结果

I don't know that how I will get my expected result in the tkinter window

from tkinter import *
from PIL import Image,ImageTk

class 构造函数在这里声明

 class window:
    def __init__(self,root):
        self.root=root
        self.Page_2()

在此处创建食物菜单函数声明:--

  def createFoodMenu(self ,dic):

    picture = Image.open(dic["Txt&Img"][1])#image imported 
    picture = picture.resize((150, 120), Image.ANTIALIAS) #image resize
    picture = ImageTk.PhotoImage(image=picture)
    self.f1 = Frame(self.root, width=dic["canvas"][0], height=dic["canvas"][1])# frame created here
    self.canvas = Canvas(self.f1, width=dic["canvas"][0], height=dic["canvas"][1],bg="grey",highlightthickness=0)#canvas Created here
    self.canvas.image=picture
    buttonBg = self.canvas.create_rectangle(dic["bBg"][0], dic["bBg"][1], dic["bBg"][2], dic["bBg"][3], fill="orange", outline="yellow")# canvas rectangle created for button
    buttonImg = self.canvas.create_image(dic["bImg"][0], dic["bImg"][1], image=picture, anchor=NW)# image are displayed 
    buttonTxt = self.canvas.create_text(dic["bTxt"][0], dic["bTxt"][1], text=dic["Txt&Img"][0], fill="blue", font=("Dancing script", dic["bTxt"][2], "bold"))# button text created 
    self.canvas.tag_bind(buttonImg, '<Button-1>', self.Load_next)# bind the buttonImg with single click
    self.canvas.tag_bind(buttonTxt, '<Button-1>', self.Load_next)# bind the buttontext with single click
    self.canvas.tag_bind(buttonBg, '<Button-1>', self.Load_next)# bind the buttonBg Rectangle with single click
    self.canvas.pack()
    self.f1.place(x=dic["FramePos"][0],y=dic["FramePos"][1])# frame are closed here

createFoodMenu 函数从上面结束:--

menuCategory 函数从以下行声明:---

def menuCategory(self):

    Rows={
        '''
        1. we have to enter the Name of the Menu(1st value of tuple) 
        and then the image of that(2nd value of the  tuple) in the item section, 
        2. in the FramePosition section the value of list is for all three category in a row but 
        the value of tuple of is the value of coordinates x and y for position
        3. 1st,2nd,3rd value of the list of all section is for 1st,2nd,3rd menu category of a row respectively.
        '''
        "Row 1":
            {
                "item":[("Main Course","MainCourseLogo.jpg"),("Fast Food","fast Food.jpg"),("Tea and Coffee","tea and coffee.jpg")],
                "FramePosition":[(10,100),(192,100),(390,100)],
                "txtS":[20,25,18]
            },
        "Row 2":
            {
                "item":[("Ice Cream","iceCream.jpg"),("Cold Drinks","coldDrinks.jpg"),("Others","others.jpg")],
                "FramePosition": [(10, 320), (192, 320), (390, 320)],
                "txtS": [20, 20, 25]
            }
    }

    for item,value in Rows.items():
        self.Row={
            "1st Item":
                {
                    "bBg": [2, 2, 155, 165], "bImg": [4, 3], "bTxt": [78, 140, value["txtS"][0]],
                    "canvas": [160, 170],"Txt&Img":[value["item"][0][0],value["item"][0][1]],
                    "FramePos":[value["FramePosition"][0][0],value["FramePosition"][0][1]]
                },

            "2nd Item":
                {
                    "bBg": [13, 2, 165, 165], "bImg": [15, 3], "bTxt": [90, 140, value["txtS"][1]],
                    "canvas": [175, 170],"Txt&Img":[value["item"][1][0],value["item"][1][1]],
                    "FramePos":[value["FramePosition"][1][0],value["FramePosition"][1][1]]
                },

            "3rd Item":
                {
                    "bBg": [3, 2, 155, 165], "bImg": [5, 3], "bTxt": [80, 140,value["txtS"][2]],
                    "canvas":[190,170],"Txt&Img":[value["item"][2][0],value["item"][2][1]],
                    "FramePos":[value["FramePosition"][2][0],value["FramePosition"][2][1]]
                }

        }
        for item, value in self.Row.items():
            self.createFoodMenu(value)#calling the createFoodMenu function for each item in a single row(value is the dictionary of a single item)

menuCategory 函数从上一行结束

Page_2 函数从以下行声明:---

def Page_2(self):

    self.menuCategory()

Page_2 函数从上一行结束

Load_next 声明的函数调用下一个 class 并销毁 window class

的所有小部件
def Load_next(self,event):
    self.canvas.destroy()
    self.f1.destroy()        
    page3=MainCourse(self.root)

MainCourse class 及其构造函数的声明

class MainCourse:
    def __init__(self,root):
        self.root = root
        self.main()
    def main(self):
        Label(text="Hello How are You").pack()

class MainCourse 从上面一行结束:---

主函数声明,其中 root 将由 Tk() 初始化,执行将从调用主函数开始

def main():
     root=Tk()
     root.configure(background="grey")
     run=Window(root)
     root.mainloop()

python main函数在这里声明,程序会从这里初始化执行

if __name__ == '__main__':
     main()

i have an issue in this program there is no syntax error as well as no logical error program is running well till to the last line of code but when i am try to call #Load_next function to call next class so before calling the function i want to remove all the widgets that window class have using destroy function of widget like that:-

def Load_next(self,event):
    self.canvas.destroy()
    self.f1.destroy()        
    page3=MainCourse(self.root)

but it does not destroy all the widgets of window class the window class of tkinter have these widgets and the page look like as:--the window of tkinter look like this picture when it have all the widgets but i have to destroy all these widgets before when I want to enter to class MainCourse

I am expect the result from Load_next function is that the tkinter window will be cleared completely when i will run the destroy function for all the widgets like this one:--this is the expected result from Load_next function which i want before entering in the next class MainCourse

but in real the actual result i get from Load_next function is look like this one:--this the result which i get from running after the Load_next function

i don't know that what will I do for getting my expected result so please help me out from this one, who will know the answer of it please let me also know. Thank You!!

每次调用 createFoodMenu() 时,self.f1 都会指向一个新的 Frame 对象。创建所有菜单项后,self.f1 指向最后创建的 Frame。这对应于屏幕截图中的最后一个菜单项 -- 'Others'。当您执行 self.f1.destroy() 时,您只会销毁最后一个 Frame.

您需要存储所有 Frames,例如在列表中并调用 destroy()。像这样:

class Window:
  def __init__(self, root):
    ...
    self.menu_items = []

  def create_food_menu(self, dic):
    ...
    f1 = Frame(...)
    ...
    self.menu_items.append(f1)

  def load_next(self, event):
    ...
    for item in self.menu_items: item.destroy()

    self.menu_items = [] # Clean up ;D