python - 在 Tkinter 中添加小部件的按钮

标签 python tkinter

在 Tkinter 中,单击时添加小部件的按钮代码看起来如何,如果需要的话可以无限添加?

谢谢,抱歉英语不好。

最佳答案

这是一个更“优雅”的版本:

from Tkinter import *

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.number = 0
        self.widgets = []
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.cloneButton = Button ( self, text='Clone', command=self.clone)
        self.cloneButton.grid()

    def clone(self):
        widget = Label(self, text='label #%s' % self.number)
        widget.grid()
        self.widgets.append(widget)
        self.number += 1


if __name__ == "__main__":
    app = Application()
    app.master.title("Sample application")
    app.mainloop()

enter image description here

请注意,您将小部件保存在 self.widgets 列表中,以便您可以根据需要调用它们并进行修改。

关于python - 在 Tkinter 中添加小部件的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10420049/

相关文章:

python - 是否有与 HighLine 等效的 Python?

python - 需要显示在 3 行而不是 1 行

python - 子类化 Tkinter.Text 以创建自定义小部件

Python gmail api发送带附件pdf的电子邮件全部空白

python - 类型错误 : Can't convert 'bytes' object to str implicitly in python

Python GTK : How to make it so a checkbox determines if a window will open or not

python - 将嵌套字典从 json 转换为以值作为列的数据框

python - tkinter 中类似电子表格的应用程序?

python - ttk TreeView : selected color

python - 如何在 tk.Canvas 上定位形状和文本以免它们被切断?