1

基本的に同様の質問がありますが、適切に回答されているとは思いません。

Tkinter: 破棄または削除できるウィジェットを動的に作成するにはどうすればよいですか?

受け入れられた答えは次のとおりです。

動的に作成されたウィジェットをリストに格納する必要があります。次のようなものがあります

     dynamic_buttons = []

     def onDoubleClick(event):
     ...
     button = Button(...)
     dynamic_buttons.append(button)
     button.pack() 

You can then access the buttons for removal with, say,

     dynamic_buttons[0].destroy()

彼らが話している参照は変数ではないことがわかります。ここでは数値 0 が使用されています。しかし、ウィジェットを動的に作成する場合、これらの参照をボタンにどのように接続するのでしょうか?

トップレベル ウィジェット (ファイルのコンテンツを表示する) を作成し、ウィジェットを閉じるボタンが必要だとします。動的作成により、複数のファイルを開くことができます。問題は、このリストを使用しても、ハード リファレンスがないため、ボタンがどのウィジェットに属しているかをどのように「認識する」かということです (アイテムのリストがあるのは素晴らしいことですが、トップレベル 5 + ボタン 5 では、それらがどのウィジェットであるかの手がかりがありません)。リストの5番目)。ボタンとトップレベルの「アクティブな」バージョンは常に 1 つだけであり、これは削除できます。

aanstuur_files = []
aanstuur_frames = []
aanstuur_buttons = []

def editAanstuur():
    openfiles = filedialog.askopenfilenames()
    if not openfiles:
        return 
    for file in openfiles:
        newtop = Toplevel(nGui, height=100, width=100)
        labelTitle = Label(newtop, text=file).pack()
        newButton = Button(newtop, text="save & close", command= ...).pack()
        aanstuur_files.append(file)
        aanstuur_buttons.append(newButton)
        aanstuur_frames.append(newtop)
4

2 に答える 2

1

How does the button know which window it belongs to? You tell it:

newButton = Button(newtop, command=lambda top=newtop: top.destroy())

By the way, you're assigning None to newButton in your code. This is because you are doing newbutton = Button(...).pack(), which means newbutton gets the value of pack() which is always None.

If you are going to save a reference to a widget, you must create the widget in a separate step from when you place it in a window.

An even better solution is to take advantage of classes and objects. Create your own subclass of Toplevel, and the instance will keep track of all of the subwidgets for you. For example:

class MyToplevel(Toplevel):
    def __init__(self, parent, filename, *args, **kwargs):
        Toplevel.__init__(self, parent, *args, **kwargs)
        self.filename = filename
        self.savebutton = Button(..., command=self.save)
        ...
    def save(self):
        print "saving...", self.filename
        ...
        self.destroy()
...
openfiles = filedialog.askopenfilenames()
if not openfiles:
    return 
for file in openfiles:
    newtop = MyToplevel(nGui, file, height=100, width=100)
于 2013-07-07T13:11:53.500 に答える
0

enumerate()function を使用して、コマンド関数へのインデックスで AP を渡します。

def editAanstuur():
    openfiles = filedialog.askopenfilenames()
    if not openfiles:
        return 
    for i, file in enumerate(openfiles):
        newtop = Toplevel(nGui, height=100, width=100)
        labelTitle = Label(newtop, text=file).pack()
        newButton = Button(newtop, text="Save", command=lambda index=i: print(index)).pack()
        aanstuur_files.append(file)
        aanstuur_buttons.append(newButton)
        aanstuur_frames.append(newtop)

ラムダを定義するときに値をバインドするキーワード パラメータとしてインデックスを渡すようにしてください (クロージャは の最後の値を使用しますi)。

enumerate()2 番目の引数として、開始するインデックスを受け取ります。デフォルトは 0 です。

于 2013-07-07T12:55:29.460 に答える