簡體   English   中英

動態添加Tkinter小部件

[英]Dynamically Adding Tkinter Widgets

在我的計算機科學課上,我們被分配了制作電影亭的任務。 用戶將物品添加到我以列表形式存儲的購物車中。 加載結帳屏幕后,我想添加標簽以顯示用戶購物車中有哪些物品。 我有以下代碼:

class CheckOut(tk.Frame): #check out page

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    #create and place base widgets
    backGroundPicture = ImageTk.PhotoImage(Image.open("Background.jpg"))
    backGroundLabel = tk.Label(self, image=backGroundPicture, borderwidth=0)
    backGroundLabel.image = backGroundPicture
    backGroundLabel.place(x=0, y=0, anchor=tk.NW)

    checkOutLabel = tk.Label(self, text="Check Out", font=("Verdana", 48), bg="#001f33", fg="red")
    checkOutLabel.place(relx=0.5, rely=0, anchor=tk.N)

    returnButton = tk.Button(self, text="Return to Main Menu", bg="#4db8ff", fg="black",
                       command=lambda: controller.show_frame(StartPage))
    returnButton.place(relx=1, rely=1, anchor=tk.SE)

def update(): #add new widgets displaying items purchase
    print("debug")
    itemLists = []
    for each in movieTitles:
        i = movieTitles.index(each)
        if(adultTickets[i] != 0):
            tempLabel = tk.Label(self, text="Adult Ticket: " + str(adultTickets[i]) + " " + each)
            itemLists.append(tempLabel)
        if(childTickets[i] != 0):
            tempLabel = tk.Label(self, text="Child Ticket: " + str(childTickets[i]) + " " + each)
            itemLists.append(tempLabel)
    for i in itemLists:
        i.place(x=100, y=200, anchor=tk.CENTER)
    print(itemLists)

一切正常,除了當我嘗試使用self的父級創建標簽時,它是未定義的。 我想將它們放在init方法使用的同一幀中。 我將如何去獲得自我? 我可以使用一些解決方法嗎?

旁注:StackOverflow期望代碼示例具有附加的4位縮進。 如果僅粘貼代碼,則在文本框中選擇它,然后在編輯器中(標記為{})單擊“代碼示例”按鈕,它將添加縮進。

class CheckOut(tk.Frame): #check out page
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        #create and place base widgets
        backGroundPicture = ImageTk.PhotoImage(Image.open("Background.jpg"))
        backGroundLabel = tk.Label(self, image=backGroundPicture, borderwidth=0)
        backGroundLabel.image = backGroundPicture
        backGroundLabel.place(x=0, y=0, anchor=tk.NW)

        checkOutLabel = tk.Label(self, text="Check Out", font=("Verdana", 48), bg="#001f33", fg="red")
        checkOutLabel.place(relx=0.5, rely=0, anchor=tk.N)

        returnButton = tk.Button(self, text="Return to Main Menu", bg="#4db8ff", fg="black",
                           command=lambda: controller.show_frame(StartPage))
        returnButton.place(relx=1, rely=1, anchor=tk.SE)

    ...

Python中類的方法將對象作為第一個參數傳遞給方法。 按照慣例,該參數名為self ,但實際上,您可以給它起任何名字。*因此,將self作為參數添加到update方法中。 我想您還想將電影標題作為參數傳遞給update方法。

    ...

    def update(self, movieTitles): #add new widgets displaying items purchase
        print("debug")
        itemLists = []
        for each in movieTitles:
            i = movieTitles.index(each)
            if(adultTickets[i] != 0):
                tempLabel = tk.Label(self, text="Adult Ticket: " + str(adultTickets[i]) + " " + each)
                itemLists.append(tempLabel)
            if(childTickets[i] != 0):
                tempLabel = tk.Label(self, text="Child Ticket: " + str(childTickets[i]) + " " + each)
                itemLists.append(tempLabel)
        for i in itemLists:
            i.place(x=100, y=200, anchor=tk.CENTER)
        print(itemLists)

這樣,在實例化對象之后,可以應用如下方法:

# instantiate object
my_frame = CheckOut(parent, controller)

# apply "update"
my_frame.update(my_movie_titles)

# The previous line has the same effect as this:
CheckOut.update(my_frame, my_movie_titles)

注意:我沒有測試代碼,只是修復了您在那兒遇到的小語法錯誤。 乍一看,這些電影的標簽似乎都具有相同的位置。

同樣,正如用戶“ zondo”所建議的那樣,到處都是lambda,使代碼混亂; 擺脫它們會使代碼更易於閱讀。 可以用functools模塊中的partial函數代替這樣的簡單事件:

from functools import partial

returnButton = tk.Button(self, ..., 
    command=partial(controller.show_frame, StartPage))
  • 其他語言(例如C ++ / Java和family)將保留關鍵字。 this

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM