Python Tkinter :removing widgets that were created using a for loop

北城余情 提交于 2021-02-11 07:08:32

问题


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.

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.

So far I have:

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)

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.

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

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)

回答1:


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



来源:https://stackoverflow.com/questions/37977885/python-tkinter-removing-widgets-that-were-created-using-a-for-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!