tkinter hanging

Robin Becker robin at jessikat.demon.co.uk
Mon Jul 3 19:23:07 EDT 2000


In article <yget$DAtiEY5EwYZ at jessikat.demon.co.uk>, Robin Becker <robin at jessikat.co.uk> writes
>In article <slrn8m0hm3.m2b.thomas at centurion.xs4all.nl>, Thomas Wouters
><thomas at xs4all.nl> writes
>>On Sun, 2 Jul 2000 15:25:21 +0100, Robin Becker <robin at jessikat.demon.co.uk>
>>wrote:
>>>I'm having problems with zombie processes under win32 with a tkinter
>>>linked against Tk 8.3. I seem to be exiting the mainloop ok and have
>>>destroyed all subwindows and then the gui object itself, but
>>>occasionally I still generate a zombie. I have checked that my after's
>>>are cancelled and debug prints seem to show me exiting the script.
>>
...
Under Win95 + Python 1.6a2 this code hangs about 1 in 10 times when the gui is killed using the
window manager. The output indicates that the mainloop is terminated, afters are cancelled and
all the widgets are destroyed. Even so it seems to hang after printing 'sys.exit'. It is using
the common Tk idiom of delayed polling with after, but the exit code makes strenuous efforts to
remove any pending calls.

Can anyone see what I'm doing wrong?

####################
from Tkinter import *
import string, sys, os, regex, telnetlib
import traceback, time

class CommandEntry(Entry):
    def __init__(self, master, partk, **kw):
        apply(Entry.__init__, (self, master), kw)

class Gui:
    def __init__(self):
        self.tk = Tk()
        try:
            self._build()
            self._alive = 1
        except:
            traceback.print_exc()
            self.destroy()

    def _build(self):
        self.tk.geometry("800x600")
        self.tk.title("Client")
        self.tk.protocol("WM_DELETE_WINDOW", self.close)
        self.after_id = []
        
        tA = {'fg':'white','bg':'black','state':'disabled','height':20}
        tC = {'fg':'white','bg':'black','insertbackground':'yellow','insertwidth':2}
        if os.name != 'posix':
            # need tcl/tk 8.0 on windows
            fnt = ("Fixedsys", 12)
            tA['font'] = fnt
            tC['font'] = fnt

        self.txt = Text(self.tk,tA)
        self.entry = apply(CommandEntry,(self.tk, self), tC)

        # set up the scrollbar for the txt widget
        self.scrollVertical = Scrollbar(self.tk,orient=VERTICAL)
        self.txt.configure(yscrollcommand=self.scrollVertical.set)
        self.scrollVertical.config(command=self.txt.yview)
        self.scrollVertical.pack(side=RIGHT, anchor=E, fill=Y)

        self.entry.pack({'side': 'bottom', 'fill': 'both'})
        self.entry.focus_set()
        self.txt.pack({'side': 'bottom', 'fill': 'both', 'expand': 1})

    def mainloop(self):
        self.after_id.append(self.tk.after(100, self.iterate))
        self.tk.mainloop()
        print 'mainloop end'
        
    def destroy(self):
        self._alive = 0
        self.after_cancel()

        for w in ('scrollVertical','txt','entry','tk'):
            try:
                getattr(self,w).destroy()
                delattr(self,w)
                print '%s destroyed' % w
            except:
                traceback.print_exc()
        
    def iterate(self):
        if self.loop():
            self.after_id.append(self.tk.after(50, self.iterate))
        else:
            self.tk.quit()

    def after_cancel(self):
        while self.after_id!=[]:
            id = self.after_id[0]
            del self.after_id[0]
            try:
                self.tk.after_cancel(id)
            except:
                traceback.print_exc()

    def close(self,event=None):
        self._alive = 0

    def loop(self):
        if not self._alive: return 0
        try:
            pass
        except:
            traceback.print_exc()
            self._alive = 0

        return self._alive

if __name__ == '__main__':
    gui = Gui()
    gui.mainloop()
    print 'Exiting'
    gui.destroy()
    del gui
    while 1:
        time.sleep(1)
        print 'sys.exit'
        sys.exit()
-- 
Robin Becker



More information about the Python-list mailing list