Tkinter Listbox.curselection() error?

Bob cokefiend66 at hotmail.com
Thu Oct 3 01:49:25 EDT 2002


I think not having a mainloop() call was ok because wait_window enters
the event processing loop.  I also tested this as part of my larger
program which does have a normal mainloop and still got the same
error.

I hadn't thought about the fact that the widget was already destroyed
when I tried to read its contents, but you're absolutely right.  I
moved the code to create the Font object to the ok callback and stored
it in a member variable that getFont() can return and everything works
great now (except that my dialog is so ugly :) ).

Thanks for your help!

-Bob


Chad Netzer <cnetzer at mail.arc.nasa.gov> wrote in message news:<mailman.1033586767.32004.python-list at python.org>...
> On Wednesday 02 October 2002 00:08, Bob wrote:
> > Hello,
> > I'm trying to write a fairly simple font selection dialog for a
> > bigger application I'm working on.  Every time I try to use the
> > curselection() method on a listbox I created I get a weird tcl error.
> > Can anyone tell me what I'm doing wrong?  Any help would be much
> > appreciated.
> >
> > [snipped long class definition]
> >
> > if( __name__ == "__main__" ):
> > 	root = Tk();
> > 	fb = ERGFontChooser( root );
> >
> > 	f = fb.getFont();
> >
> > 	print( `f` );
> > 	print( f.actual() );
> 
> You initialize Tk, and create ERGFontChooser, but you never enter into 
> the TK mainloop().  So, first, put root.mainloop() at the end of the 
> program.  Then delete the use of fb.getFont() outside of the mainloop, 
> since it won't work like you want it to. (see below)
> 
> When I run what you have, I get the error only when I hit "OK" or 
> "Cancel", causing self.top.destroy() to be run.  But since the 
> mainloop() was never entered, the Tk state is not fully defined, and so 
> this isn't unexpected.  You can't use getFont() outside of the mainloop 
> (at least, not effectively) without expecting these kinds of trouble.  
> Outside of the mainloop() Tk isn't really processing events; you could 
> call an after_idle(), before entering mainloop, to start some 
> prcoessing that isn't event based (such as forcing the window to raise, 
> etc.), but I won't guarantee the results.
> 
> Also, using wait_window() in getFont, doesn't seem right.  After all, 
> getFont() MUST stall until the Toplevel window is destroyed (which will 
> destroy all the widgets in it.)  At which point, getFont() tries to 
> continue operating on Tkinter widgets that are now destroyed.
> 
> If I put the getFont() call in the ok() method (changing it to 
> self.getFont()), and get rid of the wait_window() call in getFont(), 
> then it does what I think you want:  It returns the font info when you 
> press 'Ok', and destroys the window.  Here is the end of the program as 
> I wrote it:
> 
>         def getFont( self ):
> 
>                 # we have returned
>                 items = self.familyBox.curselection();
>                 fam = self.familyBox.get( items[0] );
>                 if( self.boldVar.get() == 0 ):
>                         weight = NORMAL;
>                 else:
>                         weight = BOLD;
> 
>                 font = Font( self.root, family = fam );
>                 font.config( size = self.sizeVar.get(),
>                              weight = weight );
> 
>                 return font;
> 
>         def ok( self, event=None ):
>                 f = self.getFont();
> 
>                 print( `f` );
>                 print( f.actual() );
> 
>                 print "OK";
>                 self.top.destroy();
> 
> if( __name__ == "__main__" ):
>         root = Tk();
>         fb = ERGFontChooser( root );
> 
>         root.mainloop()
> 
> 
> Oh, and lay off the coke, Bob. :)



More information about the Python-list mailing list