试图..." .pack_forget()"循环列表中的所有项目

时间:2016-11-24 13:04:32

标签: python tkinter

我的方法中有这个代码:

class Graphics:
    def __init__(self, root):
        root.geometry("640x640")    
        root.title("Hello")
        root.configure(bg = "black")

        self.scoreButton = Button(root, text = "See Score", command = self.score)
        self.HomeButton = Button(root, text = "Menu", command = self.deleteScore)

     def score(self):
         for i,e in enumerate(self.myList, start=1):                                               
             if e == "10":
                 self.seeScore = Label(text = "Times played: " + str(i) + " - Loss", bg="black", fg="white", font = ("Arial", 15))
                 self.seeScore.pack()
             else:                                           
                 self.seeScore = Label(text = "Times played " + str(i) + "Score: " + str(e), bg="black", fg="white", font = ("Arial", 15))
                 self.seeScore.pack()

     def deleteScore(self):
         self.seeScore.pack_forget()

截至目前,它只会在我运行方法时忘记最后一项,但我希望它忘记所有项目......我无法弄清楚如何做到这一点。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

如果要删除或隐藏小部件列表,则必须将小部件实际存储在列表中。

实施例

self.labels = []
for i,e in enumerate(self.myList, start=1):                                               
    if e == "10":
        label = Label(text = str(i), bg="black", fg="white", font = ("Arial", 15))
        self.HelloWorld.pack()
    else:                                           
        label = Label(text = str(i) + str(e), bg="black", fg="white", font = ("Arial", 15))
        self.HellowWorld.pack()
    self.labels.append(label)

...

def deleteHelloWorld(self):
    for widget in self.labels:
        widget.pack_forget()