Tkinter Listbox.curselection() error?
Chad Netzer
cnetzer at mail.arc.nasa.gov
Wed Oct 2 15:24:26 EDT 2002
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. :)
--
Chad Netzer
cnetzer at mail.arc.nasa.gov
More information about the Python-list
mailing list