Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]

like image 343
trstowell Avatar asked Oct 22 '25 12:10

trstowell


1 Answers

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.

like image 123
Abe Karplus Avatar answered Oct 24 '25 02:10

Abe Karplus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!