Exiting Toplevel Cleanly
jtorborg
jtorborg at fnal.gov
Tue Jul 10 17:58:40 EDT 2001
I'm writing a GUI using Tkinter that is a base program with buttons that
pop up Toplevels. Both
the main window and the Toplevel update 1/sec. Of course, when I tried
to exit the Toplevel, I get a bunch of garbage about missing objects. I
scoured old posts, and figured out (I think) how to use the after_cancel
method. As a test, I wrote the little GUI (pure fluff) below, which
models, if not emulates, what I'm trying to do. The problem is that
about 5% of the time, I still get garbage when I exit the Toplevel.
What am I doing wrong or not seeing? Is there another (better) solution
to this problem? Or can I expect this to be tempermental?
Thanks.
from Tkinter import *
import time
import random
import Conversions
root=Tk()
class TL:
def __init__(self):
self.tln=1
self.list=['up', 'down', 'strange', 'charm', 'top', 'bottom']
self.instan=Conversions.Convert()
def popup(self):
self.tl=Toplevel()
self.label_list=[]
for each in self.list:
norm=256*265*256
inte=int(random.betavariate(1,1)*norm)
color=self.instan.int_to_color(inte)
self.l=Label(self.tl, text=each, bg=color)
self.l.pack()
self.label_list.append(self.l)
self.b=Button(self.tl, text="EXIT", command=self.die)
self.b.pack()
self.id=self.tl.after(500, self.update_label)
def update_label(self):
for each in self.label_list:
norm=256*265*256
inte=int(random.betavariate(1,1)*norm
color=self.instan.int_to_color(inte)
each.configure(bg=color)
each.update()
self.id=self.tl.after(500, self.update_label)
def die(self):
self.tl.after_cancel(self.id)
self.tl.destroy()
class main:
def __init__(self):
self.mln=1
self.tl=TL()
self.mb=Button(root, text=self.mln, command=self.tl.popup)
self.mb.pack()
self.b=Button(root, text="EXIT", command=self.die)
self.b.pack()
self.id=root.after(1000, self.update_mainloop)
def update_mainloop(self):
self.mln=self.mln +1
self.mb.configure(text=self.mln)
self.mb.update()
self.id=self.mb.after(1000, self.update_mainloop)
def die(self):
root.after_cancel(self.id)
root.destroy()
root.mainloop()
More information about the Python-list
mailing list