Tkinter:如何动态创建一个可以被销毁或删除的小部件? [英] Tkinter: How can I dynamically create a widget that can then be destroyed or removed?

查看:23
本文介绍了Tkinter:如何动态创建一个可以被销毁或删除的小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来创建令人作呕的小部件(很可能是一个标签),但需要注意的是,它们可以在以后被移除或解压.

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.

我的第一直觉是动态创建具有稳定约定的变量名,但这可能会不必要地打开一堆蠕虫.这个想法表达如下.我希望能够删除某个 Button 小部件,而在运行时不知道我将处理多少个.谢谢.

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)

我的功能请求无疑是短视的.我的最终目标是让一个 Label 小部件和一个 Button 并排,两者都包含在测试启动器中的步骤".单击该按钮将从 GUI 中删除其自身及其各自的标签.我能够创建两个小部件并在 Button 的回调中删除它们中的一个,但是要 pack_forget 这两个小部件,我相信我需要 def 一个函数.我相信我的问题在于传递对 def removeStep 的正确引用 一个用例如下图所示: ....[如果这可以解决我的 RTFM 请随时告诉我,我只是不能找不到]

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]

测试:做一个 PB&J

TEST: Make a PB&J

第 0 步:获取面包 [删除步骤]

Step 0: Get Bread [Remove step]

第一步:涂抹PB【去除步骤】

Step 1: Smear PB [Remove step]

第2步:涂抹果冻【去除步骤】

Step 2: Smear Jelly [Remove step]

推荐答案

您需要将动态创建的小部件存储在列表中.有类似的东西

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

关于您的用例的更多信息,我可能会这样做

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)

然后只需创建名称为第 0 步:获取面包"的 RemovableTask 实例,并将它们网格化或打包成一列.其他一切都会自动处理.

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.

这篇关于Tkinter:如何动态创建一个可以被销毁或删除的小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆