Tkinter Listbox.curselection() error?
Bob
cokefiend66 at hotmail.com
Wed Oct 2 03:08:19 EDT 2002
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.
Thanks,
-Bob
Error text:
Traceback (most recent call last):
File "ERGFontChooser.py", line 104, in ?
f = fb.getFont();
File "ERGFontChooser.py", line 71, in getFont
items = self.familyBox.curselection();
File "C:\Python22\lib\lib-tk\Tkinter.py", line 2285, in curselection
return self.tk.splitlist(self.tk.call(
TclError: invalid command name ".9321880.9318744.9334288"
My code:
(sorry for the linewraps, I've been using 4-space tabs)
###########################################################
# ERGFontChooser.py
from Tkinter import *
from tkFont import *
class ERGFontChooser:
# a simple font selectiondialog
def __init__( self, root ):
self.root = root;
self.top = Toplevel( root );
self.top.protocol( "WM_DELETE_WINDOW", self.cancel );
self.top.bind( "<Return>", self.ok );
self.top.bind( "<Escape>", self.cancel );
f1 = Frame( self.top );
Label( f1, text = "Font: " ).grid( row = 0, column = 0 );
# create the listbox and scroll bar
scrollbar = Scrollbar( f1, orient=VERTICAL );
self.familyBox = Listbox( f1, height = 5,
yscrollcommand = scrollbar.set );
scrollbar.config( command = self.familyBox.yview );
scrollbar.grid( row = 0, column = 2, pady=5, sticky = W+N+S );
self.familyBox.grid( row = 0, column = 1, pady = 5 );
# fill the listbox
for fam in families():
self.familyBox.insert( END, fam );
# create size selection
Label( f1, text = "Size: " ).grid( row = 1, column = 0 );
sizeFrame = Frame( f1 );
dnb = Button( sizeFrame, text = "-", command = self.sizeDown,
width = 1, height = 1);
dnb.grid( row = 0, column = 0 );
self.sizeVar = StringVar();
self.sizeVar.set( "10" );
sen = Entry( sizeFrame, textvariable = self.sizeVar, width = 2 );
sen.grid( row = 0, column = 1, padx = 5 );
upb = Button( sizeFrame, text = "+", command = self.sizeUp,
width = 1, height = 1);
upb.grid( row = 0, column = 2, padx = 5 );
sizeFrame.grid( row = 1, column = 1, padx = 5, pady = 5, sticky = W
);
# create bold selection
self.boldVar = IntVar();
self.boldVar.set( 0 );
cb = Checkbutton( f1, text = "Bold", variable = self.boldVar );
cb.grid( row = 2, column = 1, columnspan = 2, sticky = W, pady = 5
);
f1.pack();
f2 = Frame( self.top );
okb = Button( f2, text = "OK", command = self.ok, width = 10 );
cnb = Button( f2, text = "Cancel", command = self.cancel, width =
10);
okb.grid( row = 0, column = 0, padx = 5, pady = 5 );
cnb.grid( row = 0, column = 1, padx = 5, pady = 5 );
f2.pack();
def getFont( self ):
self.top.update_idletasks();
self.top.lift();
self.top.grab_set();
self.root.wait_window( self.top );
# 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 sizeUp( self, event = None ):
size = int( self.sizeVar.get() ) + 1;
self.sizeVar.set( `size` );
def sizeDown( self, event = None ):
size = int( self.sizeVar.get() ) - 1;
self.sizeVar.set( `size` );
def ok( self, event=None ):
print "OK";
self.top.destroy();
def cancel( self, event = None ):
print "Cancel";
self.top.destroy();
if( __name__ == "__main__" ):
root = Tk();
fb = ERGFontChooser( root );
f = fb.getFont();
print( `f` );
print( f.actual() );
More information about the Python-list
mailing list