繁体   English   中英

我不知道如何在 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()

在这里创建食物菜单 Function 声明:--

  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 从上面结束:--

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 function 从上一行结束

Page_2 function 从以下行声明:---

def Page_2(self):

    self.menuCategory()

Page_2 function 从上一行结束

load_next function 声明调用next class 并销毁window class 的所有widget

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 从上面一行结束:---

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

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

python main function 在这里声明程序将从这里初始化执行

if __name__ == '__main__':
     main()

我在这个程序中有一个问题,没有语法错误,也没有逻辑错误程序运行良好,直到最后一行代码但是当我尝试调用#Load_next function 来调用下一个 class 所以在调用 function 之前我想要要删除 window class 拥有的所有小部件,请使用像这样的小部件的销毁 function:-

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

但是,它并没有破坏window class 8window class tkinter整体上有88819的整体,以及888233707070907090907070907090909090909090909090907090909090909090909090909 0906年。在我想进入 class MainCourse 之前

我希望 Load_next function 的结果是 tkinter window 将被完全清除,当我为所有像这样的小部件运行 destroy function 时:-这是 Load_next function 的预期结果,然后进入我想要的下一个class 主菜

但实际上我从 Load_next function 得到的实际结果看起来像这样:-这是我在 Load_next function 之后运行得到的结果

我不知道我将如何获得我的预期结果,所以请帮助我解决这个问题,谁知道它的答案请让我也知道。 谢谢你!!

每次调用createFoodMenu()时, self.f1指向一个新的Frame object。创建所有菜单项后, self.f1指向创建的最后一个Frame 这对应于屏幕截图中的最后一个菜单项——“其他”。 当您执行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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM