Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

python - Tkinter: How can I dynamically create a widget that can then be destroyed or removed?

I am looking for a method to create widgets (most likely a Label) ad nauseam, with the caveat that they can be removed or unpacked later on.

I can generate the widgets just fine, but they are not assigned a name. I do not understand how I would, if it is possible, remove a certain anonymous widget.

My first instinct was to dynamically create variable names with a stable convention, but that may unnecessarily open a can of worms. The idea is expressed below. I'd like to be able to remove a certain Button widget while not knowing at run-time how many I will handle. Thank you.

from Tkinter import *
import time
import ttk


def onDoubleClick(event):
    item = t.selection()
 #print "you clicked on", t.item(item,"text")

    if (t.item(item,"text")=="Start IO"):
        Button2 = Button(frame2,text="Button2",command=but).pack()


def but():
    pack_forget()

root=Tk()
root.geometry("800x300")
frame1 = Frame(root)
frame2 = Frame(root)

t=ttk.Treeview(frame1)
t.heading("#0",text="Test steps")
t.insert("",0,"IO",text="IO")
t.insert("IO","end",text="Start")
t.bind("<Double-1>", onDoubleClick)
t.pack()
frame1.pack(side=LEFT)
frame2.pack(side=LEFT)

EDIT: My feature request was admittedly short-sighted. My ultimate goal is to have a Label widget and a Button side-by-side, both comprising what is to be a 'step' in a test launcher. Clicking the button will remove both itself and its respective Label from the GUI. I'm able to create both widgets and delete either one of them on the Button's callback, but to pack_forget both I believe I need to def a function. I believe my problem lies in passing a correct reference to def removeStep A use case is diagrammed below: ....[If this could be solved my RTFM please feel free to let me know, I just couldn't find it]

TEST: Make a PB&J

Step 0: Get Bread [Remove step]

Step 1: Smear PB [Remove step]

Step 2: Smear Jelly [Remove step]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll want to store the dynamically-created widgets in a list. Have something like

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()

Edit: With more information about your use case, I would probably do

class RemovableTask(Frame):
    def __init__(self, master, name, **options):
        Frame.__init__(self, master, **options)
        lbl = Label(self, text=name)
        btn = Button(self, text='Remove step', command=self.destroy)
        lbl.grid(row=0, column=0)
        btn.grid(row=0, column=1)

Then just create instances of RemovableTask with names like "Step 0: Get Bread", and grid or pack them in a column. Everything else would be handled automatically.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...