Python Tkinter:删除使用 for 循环创建的小部件 [英] Python Tkinter :removing widgets that were created using a for loop

查看:27
本文介绍了Python Tkinter:删除使用 for 循环创建的小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习如何在 python 上使用 Tkinter 库来创建一个 GUI,该 GUI 接受经度和纬度点并将其输出到文件中.基本上,我试图自动化必须将正确格式的点行复制到另一个文件的过程.

I'm currently learning how to use the Tkinter library on python to create a GUI that takes in longitude and latitude points and outputing that into a file. Basically I'm trying to automate the process of having to copy the correct format of line of points to another file.

所以我创建了一个条目和按钮字段来查看需要多少个长/纬度点来生成一个形状".使用来自用户的这个整数输入,我有一个 for 循环来用多个小部件填充 GUI,要求长/纬度点.我让它正常工作,但现在我试图有一个清除按钮,它允许用户清除所有这些长/纬度点,并让他们能够用其他形状所需的点数重新填充字段.

So I created a Entry and button field to see how many long/lat points are needed to generate a 'shape'. Using this integer input from user, I have a for loop to populate the GUI with multiple widgets asking for the long/lat points. I have that working properly, but now I am trying to have a clear button, which would allow the user to clear all these long/lat points and give them the ability to repopulate the field with the amount of points the other shape requires.

到目前为止我有:

def clearGrid():
   coordAmount = int(pointText.get())
   latLabel.grid_forget()
   longLabel.grid_forget()

   .....(contains code that populates the GUI)
   #creating clear site Button
   clearButton = Button(main_gui, text="Clear Sites!",command=clearGrid)
   clearButton.grid(row=lastRow+1, column=5, pady=10)

然而,我遇到的问题是,当点击清除按钮时,它只清除小部件的最新实例,而不是全部.因此,在创建 5 个小部件实例/迭代的 for 循环中,它将仅删除小部件的第 5 个实例/迭代.

However, the problem that I am running into is that when the clear button is clicked, it only clears the latest instance of the widgets not all of them. So in a for loop that creates 5 instances/iteration of widgets, it will remove only the 5th instance/iteration of widgets.

我正在尝试让清除按钮功能能够删除这些小部件的所有 5 个实例.

I'm trying to have the clear button functionality be able to delete all 5 instances of these widgets.

这里是我如何用小部件填充 GUI 的简短代码

So here is a shortened code of how I am populating the GUI with widgets

          def generatePoints():
            for x in range(0,3):
              degLong_label = Label(main_gui, text="Degree:", height=2)
              degLong_label.grid(row=y,column=6,sticky=E)
              degLong = Entry(main_gui, width=4)
              degLong.grid(row=y,column=7,sticky=W)

              #minute
              minLong_Label = Label(main_gui,text="Minutes:", height=2)
              minLong_Label.grid(row=y,column=8,sticky=W)
              minLong = Entry(main_gui,width=3)
              minLong.grid(row=y,column=8,sticky=E)

              #seconds
              secLong_Label= Label(main_gui,text="Sec:",height=2)
              secLong_Label.grid(row=y,column=9,sticky=W,padx=20)
              secLong = Entry(main_gui,width=3)
              secLong.grid(row=y,column=9,sticky=E,padx=20)

              #direction
             dirLong_Label = Label(main_gui,text="Direction:",padx=5,height=2)
             dirLong_Label.grid(row=y,column=12,sticky=W)
             dirLong = Entry(main_gui,width=3)
             dirLong.grid(row=y,column=13)

推荐答案

您需要保留对所有这些小部件的引用,通常是通过列表.尝试在循环之前初始化一个列表 (list_of_widgets),然后每次创建一个小部件时,将其附加到该列表中.清除后,您可以遍历该小部件列表并销毁每个小部件.完成清除它们后,您可以清除列表,这样您就不会尝试两次销毁小部件(此时 Tkinter 会出错).

You need to hold on to references to all those widgets, usually via a list. Try initializing a list (list_of_widgets) before your loop, then every time you create a widget, append it to that list. When you clear, you can iterate through that list of widgets and destroy each one. Once you're done clearing them, you can clear the list so you don't try to destroy a widget twice (Tkinter will error at that point).

def generatePoints():
    list_of_widgets = [] # or take the list as a parameter
    for x in range(3):
        degLong_label = Label(...)
        degLong_label.grid(...)
        list_of_widgets.append(degLong_label)
        degLong = Entry(...)
        degLong.grid(...)
        list_of_widgets.append(degLong)

        # et al.

def clearGrid(list_of_widgets):
    for widget in list_of_widgets:
        widget.destroy()

请注意,如果您不打算再次显示那个特定的小部件(初始化一个新的小部件不算在内),您可能想要真正销毁这些小部件.

Note that you probably want to actually destroy the widgets if you aren't planning on showing that specific widget again (initializing a new one doesn't count).

这篇关于Python Tkinter:删除使用 for 循环创建的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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