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()
ウィジェットを 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()