在按钮上制作新标签时出现问题 tkinter

   import tkinter as tk


root = tk.Tk()

root.title("To Do")

root.geometry("500x750")

root.resizable(False, False)


class App:

    def __init__(self, window):

        #Button font

        self.buttonFont = ("Courier", 13, "normal")

        #Header

        self.header = tk.Label(text = "To Do List\n", font = ("Helvetica", 26, "bold"))

        self.header.grid()

        #Add item button

        self.addButton = tk.Button(text = "Add Item", font = self.buttonFont, bg = "green", command = self.OpenAddItem)

        self.addButton.grid(row = 1, column = 0, )

        #Del item button

        self.delButton = tk.Button(text = "Delete Item", font = self.buttonFont, bg = "red")

        self.delButton.grid(row = 1, column = 1, )

        #items frame and stuff

        self.itemsframe = tk.Frame()

        self.itemsframe.grid(row = 2)

        self.item = tk.Label(self.itemsframe, font = ("Helvetica", 11, "normal"))

    def OpenAddItem(self):

        #COnfiguring prompt win

        self.addPromptWin = tk.Toplevel(root)

        self.addPromptWin.title("Prompt")

        self.addPromptWin.geometry("375x100")

        self.addPromptWin.resizable(False,False)

        #Entry label

        self.entryLabel = tk.Label(self.addPromptWin,text = "Enter item name:", font = ("Courier", 11, "normal"))

        self.entryLabel.grid(sticky = tk.E)

        #Adding entry 

        self.entry = tk.Entry(self.addPromptWin, font = ("Courier", 11, "normal"))

        self.entry.grid(row = 0, column = 1)

        self.entry.bind("<Return>", lambda e: self.AddItem())

        #Add new item button

        self.addNewItemButton = tk.Button(self.addPromptWin, text = "Add", font = self.buttonFont, bg = "green", command = self.AddItem)

        self.addNewItemButton.grid(row = 1, sticky = tk.W)


大家好,我的 tkinter gui 程序有问题。当我调用 addItem 函数时,它不是每次都创建一个新标签,而是替换当前标签。任何帮助将非常感激!


慕雪6442864
浏览 92回答 1
1回答

慕码人2483693

你创建了一个Label命名文件self.item,然后不断地覆盖它,期望它神奇地成为一个新的Label. 修复方法很简单~不要创建self.item. 相反,创建一个列表并Labels根据需要将新内容推送到其中。这为您的删除方法奠定了基础,因为您可以解压缩缓存中的所有内容,删除所需的项目并重新打包缓存中剩余的所有内容。我的例子在重要的地方进行了评论。我没有给你写删除方法。import tkinter as tkroot = tk.Tk()root.title("To Do")root.geometry("500x750")root.resizable(False, False)class App:&nbsp; &nbsp; def __init__(self, window):&nbsp; &nbsp; &nbsp; &nbsp; #Button font&nbsp; &nbsp; &nbsp; &nbsp; self.buttonFont = ("Courier", 13, "normal")&nbsp; &nbsp; &nbsp; &nbsp; #Header&nbsp; &nbsp; &nbsp; &nbsp; self.header = tk.Label(text = "To Do List\n", font = ("Helvetica", 26, "bold"))&nbsp; &nbsp; &nbsp; &nbsp; self.header.grid()&nbsp; &nbsp; &nbsp; &nbsp; #Add item button&nbsp; &nbsp; &nbsp; &nbsp; self.addButton = tk.Button(text = "Add Item", font = self.buttonFont, bg = "green", command = self.OpenAddItem)&nbsp; &nbsp; &nbsp; &nbsp; self.addButton.grid(row = 1, column = 0, )&nbsp; &nbsp; &nbsp; &nbsp; #Del item button&nbsp; &nbsp; &nbsp; &nbsp; self.delButton = tk.Button(text = "Delete Item", font = self.buttonFont, bg = "red")&nbsp; &nbsp; &nbsp; &nbsp; self.delButton.grid(row = 1, column = 1, )&nbsp; &nbsp; &nbsp; &nbsp; #items frame and stuff&nbsp; &nbsp; &nbsp; &nbsp; self.itemsframe = tk.Frame()&nbsp; &nbsp; &nbsp; &nbsp; self.itemsframe.grid(row = 2)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ''' you just keep overwriting this '''&nbsp; &nbsp; &nbsp; &nbsp; #self.item = tk.Label(self.itemsframe, font = ("Helvetica", 11, "normal"))&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; #start a cache instead&nbsp; &nbsp; &nbsp; &nbsp; self.items = []&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def OpenAddItem(self):&nbsp; &nbsp; &nbsp; &nbsp; #COnfiguring prompt win&nbsp; &nbsp; &nbsp; &nbsp; self.addPromptWin = tk.Toplevel(root)&nbsp; &nbsp; &nbsp; &nbsp; self.addPromptWin.title("Prompt")&nbsp; &nbsp; &nbsp; &nbsp; self.addPromptWin.geometry("375x100")&nbsp; &nbsp; &nbsp; &nbsp; self.addPromptWin.resizable(False,False)&nbsp; &nbsp; &nbsp; &nbsp; #Entry label&nbsp; &nbsp; &nbsp; &nbsp; self.entryLabel = tk.Label(self.addPromptWin,text = "Enter item name:", font = ("Courier", 11, "normal"))&nbsp; &nbsp; &nbsp; &nbsp; self.entryLabel.grid(sticky = tk.E)&nbsp; &nbsp; &nbsp; &nbsp; #Adding entry&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; self.entry = tk.Entry(self.addPromptWin, font = ("Courier", 11, "normal"))&nbsp; &nbsp; &nbsp; &nbsp; self.entry.grid(row = 0, column = 1)&nbsp; &nbsp; &nbsp; &nbsp; self.entry.bind("<Return>", lambda e: self.AddItem())&nbsp; &nbsp; &nbsp; &nbsp; #Add new item button&nbsp; &nbsp; &nbsp; &nbsp; self.addNewItemButton = tk.Button(self.addPromptWin, text = "Add", font = self.buttonFont, bg = "green", command = self.AddItem)&nbsp; &nbsp; &nbsp; &nbsp; self.addNewItemButton.grid(row = 1, sticky = tk.W)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def AddItem(self):&nbsp; &nbsp; &nbsp; &nbsp; self.entryValue = self.entry.get()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; '''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; you can keep configuring and repacking this for infinity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; it's not going to change the fact that it's the same label every time&nbsp; &nbsp; &nbsp; &nbsp; '''&nbsp; &nbsp; &nbsp; &nbsp; #self.item.config(text = f"1.&nbsp; {self.entryValue}")&nbsp; &nbsp; &nbsp; &nbsp; #self.item.pack()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; #append a new label to the cache instead&nbsp; &nbsp; &nbsp; &nbsp; self.items.append(tk.Label(self.itemsframe, font = ("Helvetica", 11, "normal"), anchor='w'))&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; #config and pack the last item in the cache&nbsp; &nbsp; &nbsp; &nbsp; self.items[-1].config(text = f"{len(self.items)}.&nbsp; {self.entryValue}",)&nbsp; &nbsp; &nbsp; &nbsp; self.items[-1].pack(anchor='w')&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; self.addPromptWin.destroy()app = App(root)&nbsp;root.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python