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

Time:02-25

from tkinter import *
from PIL import Image,ImageTk

class and constructor is declared here

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

create Food Menu Function declaration here:--

  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 function end from above:--

menuCategory functon are declared from below line:---

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 function ended from above line

Page_2 function is declared from below line:---

def Page_2(self):

    self.menuCategory()

Page_2 function ended from above line

Load_next function declared for calling next class and destroy all the widgets of window class

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

declaration of the MainCourse class with its constructor

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

class MainCourse is ended from above line:---

main function declaration where root will be initialized by Tk() and execution will be start from calling main function

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

python main function is declared here the program will be initialized from here to execute

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!!

CodePudding user response:

Each time you call createFoodMenu(), self.f1 points to a new Frame object. Once all menu items are created, self.f1 points to the last Frame created. This corresponds to the last menu item -- 'Others' in your screenshot. When you do self.f1.destroy(), you destroy only the last Frame.

You need to store all Frames, say in a list and call destroy() on them all. Like so:

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
  • Related