Python 有没有办法用tkinter通过按下按钮来创建标签

Python 有没有办法用tkinter通过按下按钮来创建标签,python,tkinter,window,Python,Tkinter,Window,我有一个程序,我希望它存储用户编写的信息,除了我希望它存储在一个窗口中,并在用户每次存储数据时不断制作新的标签和按钮。但是,我找不到一种方法让代码本身向程序添加标签。事先编写一堆标签以供以后启用似乎不切实际,我正在寻找更好的解决方案。当然。你可以很容易做到。下面的例子肯定可以用不同的方式来考虑,但它说明了您想要做的事情背后的基本原则 import tkinter as tk #create root root = tk.Tk() root.geometry('400x300') #confi

我有一个程序,我希望它存储用户编写的信息,除了我希望它存储在一个窗口中,并在用户每次存储数据时不断制作新的标签和按钮。但是,我找不到一种方法让代码本身向程序添加标签。事先编写一堆标签以供以后启用似乎不切实际,我正在寻找更好的解决方案。

当然。你可以很容易做到。下面的例子肯定可以用不同的方式来考虑,但它说明了您想要做的事情背后的基本原则

import tkinter as tk

#create root
root = tk.Tk()
root.geometry('400x300')

#configure grid
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)

#just a container for generated labels
label_frame = tk.Frame(root, bg='black')
label_frame.grid(row=0, column=0, sticky='nswe', columnspan=3)

#instructions for this example
tk.Label(root, text='enter label text and click create', anchor='w').grid(row=1, column=0, sticky='w')

#entry field for example purposes
label_ent = tk.Entry(root)
label_ent.grid(row=1, column=1, sticky='we')

#called when the button is clicked
def create_label(master, row, column, sticky='w', **kwargs):
    #really, it's as simple as creating a label and adding it to the display
    #~ with grid, pack or place, within a function that is called by a button
    tk.Label(master, **kwargs).grid(row=row, column=column, sticky=sticky)
    
#button that creates labels
tk.Button(root, text='create', command=lambda: create_label(label_frame, 
                                                            row=len(label_frame.winfo_children()), 
                                                            column=0, 
                                                            text=label_ent.get())).grid(row=1, column=2, sticky='w')

root.mainloop()

没有任何东西可以阻止您从按钮创建小部件。只需创建一个创建标签的命令,并通过按钮调用它

import tkinter as tk

def create_label():
    count = len(root.winfo_children())
    label = tk.Label(root, text=f"Label #{count}")
    label.pack(side="top")

root = tk.Tk()
root.geometry("500x500")
button = tk.Button(root, text="Create label", command=create_label)
button.pack(side="top")
root.mainloop()
如果希望以后能够访问这些标签,请将它们添加到全局数组或字典:

labels = []
def create_label():
    count = len(root.winfo_children())
    label = tk.Label(root, text=f"Label #{count}")
    label.pack(side="top")
    labels.append(label)
如果要使用一个或多个按钮创建标签,建议创建自定义类。下面是一个模拟“待办事项”的示例,其中有一个会破坏自身的按钮。这不是特别好的设计,但它展示了一般的概念

class TodoItem(tk.Frame):
    def __init__(self, parent, text):
        super().__init__(parent)
        self.label = tk.Label(self, text=text, anchor="w")
        self.delete = tk.Button(self, text="Delete", command=self.delete)
        self.delete.pack(side="right")
        self.label.pack(side="left", fill="both", expand=True)

    def delete(self):
        self.destroy()
然后,您可以像对待任何其他小部件一样对待它,包括能够通过单击按钮创建它:

def create_label():
    count = len(root.winfo_children())
    item = TodoItem(root, f"This is item #{count}")
    item.pack(side="top", fill="x")

您应该能够在命令上生成标签,并使用显示管理器显示它们?如果你可以发布一个。你可以使用一个
文本
小部件来保存所需的信息,而不是一堆
标签
s。columnconfigure()和
rowconfigure()
做什么?@CoolCloud-谷歌It。答案太长,无法评论。在尽可能短的时间内,根据我使用它们的方式,它们像
pack(fill='both',expand=True)
一样协同工作,但并不完全如此。