Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按钮添加小部件,在Tkinter中_Python_Tkinter - Fatal编程技术网

Python 按钮添加小部件,在Tkinter中

Python 按钮添加小部件,在Tkinter中,python,tkinter,Python,Tkinter,在Tkinter中,单击按钮时添加小部件的代码看起来是什么样的,如果需要,可以无限地添加 感谢并为糟糕的英语道歉。嗯,它可能看起来像这样(它可能看起来像很多不同的东西): 这是一个更“经典”的版本: 注意,您将小部件保存在self.widgets列表中,以便您可以调用它们并根据需要修改它们。我将如何在顶级添加新的小部件? import Tkinter as tk root = tk.Tk() count = 0 def add_line(): global count coun

在Tkinter中,单击按钮时添加小部件的代码看起来是什么样的,如果需要,可以无限地添加


感谢并为糟糕的英语道歉。

嗯,它可能看起来像这样(它可能看起来像很多不同的东西):

这是一个更“经典”的版本:


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

我将如何在顶级添加新的小部件?
import Tkinter as tk
root = tk.Tk()
count = 0
def add_line():
    global count
    count += 1
    tk.Label(text='Label %d' % count).pack()
tk.Button(root, text="Hello World", command=add_line).pack()
root.mainloop()
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()