from tkinter import *
from PIL import Image,ImageTk
類和建構式在這里宣告
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 函式并銷毀視窗類的所有小部件
def Load_next(self,event):
self.canvas.destroy()
self.f1.destroy()
page3=MainCourse(self.root)
MainCourse 類及其建構式的宣告
class MainCourse:
def __init__(self,root):
self.root = root
self.main()
def main(self):
Label(text="Hello How are You").pack()
類 MainCourse 從上面的行結束:---
主函式宣告,其中 root 將由 Tk() 初始化,執行將從呼叫主函式開始
def main():
root=Tk()
root.configure(background="grey")
run=Window(root)
root.mainloop()
python main函式在這里宣告程式將從這里初始化執行
if __name__ == '__main__':
main()
我在這個程式中有一個問題,沒有語法錯誤,也沒有邏輯錯誤程式運行良好,直到最后一行代碼但是當我嘗試呼叫 #Load_next 函式來呼叫下一個類時,所以在呼叫我想要的函式之前使用像這樣的小部件的破壞功能洗掉視窗類具有的所有小部件:-
def Load_next(self,event):
self.canvas.destroy()
self.f1.destroy()
page3=MainCourse(self.root)
但它不會破壞視窗類的所有小部件 tkinter 的視窗類具有這些小部件,頁面如下所示:-- tkinter 的視窗在具有所有小部件時看起來像這張圖片,但我必須銷毀所有這些小部件在我想進入 MainCourse 課程之前
我希望 Load_next 函式的結果是,當我為所有像這樣的小部件運行破壞函式時,tkinter 視窗將被完全清除:--這是我在進入下一個之前想要的 Load_next 函式的預期結果班級主課
但實際上我從 Load_next 函式得到的實際結果看起來像這樣:--這是我在 Load_next 函式之后運行得到的結果
我不知道我會怎么做才能得到我的預期結果,所以請幫我解決這個問題,誰會知道答案,請告訴我。謝謝!!
uj5u.com熱心網友回復:
每次呼叫createFoodMenu(),都self.f1指向一個新Frame物件。創建所有選單項后,self.f1指向最后Frame創建的選單項。這對應于螢屏截圖中的最后一個選單項——“其他”。當你這樣做時self.f1.destroy(),你只會摧毀最后一個Frame。
您需要存盤 all 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431680.html
