2

Using python 3.6.1 and the included tkinter module

I'm trying to have tkinter display a list of 10 cards and their respective expansion packs... I can make it work one time, but I can't seem to make it repeatable with the button.

I want to remove the first set and allow the user to run it again. So I need to identify the widgets and remove them. I used this answer when I was trying to figure out how to identify the label widgets in a specific row. But when I run it, it's not picking up the widgets in the rows. It just returns empty sets for each row.

This first loop is where I setup the labels excerpted from the main class app(Frame) __init__() method and also the method that should be picking things up to clear them out. I've broken it down further than it needs to be at this point trying to troubleshoot. I think it's a scope issue, I just don't know enough python to get any further in my search without some advice.

from tkinter import *

class App(Frame):
    def __init__(self):
        super().__init__()
        row = 2
        for item in range(0, 9):
            self.label1 = Label(text="item")
            self.label1.grid(row=row, column=0, sticky=W)
            self.label2 = Label(text="item")
            self.label2.grid(row=row, column=2, sticky=E)
            row += 1

        button = Button(text="Do the thing!", command=self.replace_labels())
        button.grid(row=0, column=1)

    def replace_labels(self):
        row = 2
        for number in range(0, 9):
            temp_set = self.grid_slaves(row=number + row)
            row += 1
        for thing in temp_set:
            thing.forget()

def main():
    root = Tk()
    app = App()
    root.mainloop()

if __name__ == '__main__':
    main()

1 Answers1

0

I can't run your code, but it appears that you are creating widgets in the root window, yet trying to get a list of windows in the app object.

If you want the labels to be part of app, you need to specify that when you create the labels:

...
self.label1 = Label(self, ...)
self.label2 = Label(self, ...)
...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thank you for looking at this. I pared down the code and resubmitted it with the same problem... also accounting for that. Sadly, same problem. – Stephen DeVilbiss Oct 27 '17 at 12:40
  • @StephenDeVilbiss: the code I see as of this moment still is creating the labels in the root window, but you are still calling `grid_slaves` on the `app` frame. – Bryan Oakley Oct 27 '17 at 12:59
  • I'm sorry, I must be missing something... I'm creating the labels with `self.label1 = Label(text="item")` does that refer to the root window? I feel like I must not be understanding something fundamental about classes if that's the case. UPDATE: Nevermind. I just figured out what you meant haha. Thank you! – Stephen DeVilbiss Oct 27 '17 at 14:31
  • @StephenDeVilbiss: Does your code refer to the root window? Yes, because it doesn't refer to any other window. Look at your code, and look at the code in my answer. Pay close attention to the first argument to `Label`. – Bryan Oakley Oct 27 '17 at 15:00