0

So I'm new to tkinter, but I've got what I want working, up to a certain point. I'm not sure I've set it up correctly, but I've got a world map with buttons on the right, and an events log on the left, which fills up with labels as stuff happens. Issue is that after a little while, the whole log fills up. What is the best way to delete all the labels, or maybe delete the oldest (top) label each time? Here's what I mean:

enter image description here

Defined here:

root=Tk()
Map=PhotoImage(file="C:/Users/Willam/Desktop/CWProgram/map2.gif")
background=Label(root,image=Map).place(x=100,y=0,relwidth=1,relheight=1)
Title=Label(root,text='                     LOG').pack(anchor=NW)

And I create my labels like this:

info=Label(root,text='Select a sector to move units from',wraplength=170)
info.pack(anchor=NW)

I tried the usual info.destoy() and info.forget(), but these only work on the last label used in that function. Should I have grouped all labels or something?

DavidG
  • 24,279
  • 14
  • 89
  • 82
W.Hunt
  • 31
  • 1
  • 2
  • 8
  • 1
    very common mistake `var = Widget().pack()` you assing to `var` value returned by `pack()`, not `Widget()`. You need two steps `var = Widget()` and `var.pack()`. The same with `place()` (and `grid()` if you will use in the future). – furas Oct 25 '16 at 12:19
  • 4
    I suggest appending your labels to a list. That makes it easy to kill (or recycle) the oldest ones. – PM 2Ring Oct 25 '16 at 12:22
  • Possible duplicate of [Tkinter: AttributeError: NoneType object has no attribute get](http://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-get) – Lafexlos Oct 25 '16 at 12:25

3 Answers3

4

As PM 2Ring suggested it is usually useful to append labels to a list for future ref:

tmp = Label(...)
labels.append(tmp)

then just:

foreach label in labels: label.destroy()

If you do not want a list, and you're sure you want to clear everything in root:

foreach label in root.children.values(): label.destroy()

The children dict always holds the objects contained within. If you want to keep the map label, you will have to make your own list as I showed, without appending info into it.

kabanus
  • 24,623
  • 6
  • 41
  • 74
0

I would recommend using:

info.pack_forget()

For each pack you created you must do it in the format:

packname.pack_forget()

Which if you have a lot of packs is impractical, but otherwise it works very well.

This also makes it very easy to selectively remove some labels and leave others as it will not purge all packs that you placed.

0

Just use:

root.children.clear

After clearing screen just input map and functions again...

Aston
  • 11
  • 1
  • 5