Struggling to return integer from a Tk Program

morden morden at
Wed Jan 15 03:14:00 CET 2003

I run this code:

from Tkinter import *
import string

retval = [-1]

class ThreeButtons:
	title = "???"

         def __init__(self, master, textbody, but1, but2, but3=None, 
             if title is None: title = self.title
             self.master = master

    = Toplevel(master)

             Label(, text=textbody).pack(side=TOP, fill=X, 
expand=1, pady=20)

             Button(, {'text':text, 'command':self.yes, \
                               Pack:{'side':'left', 'padx':'20', 
'pady':'20'}}, underline=upos )

             Button(, {'text':text, 'command', \
                               Pack:{'side':'left', 'padx':'20', 
'pady':'20'}}, underline=upos )

             if but3 is not None:
                 Button(, {'text':text, 
'command':self.cancel_command, \
                                   Pack:{'side':'left', 'padx':'20', 
'pady':'20'}}, underline=upos )

   '<Alt-c>', self.cancel_command)

   '<Alt-w>', self.cancel_command)
   '<Alt-W>', self.cancel_command)
   '<Alt-F4>', self.cancel_command)
   'WM_DELETE_WINDOW', self.cancel_command)

         def droptilda(self, str):
             comps = string.split( str, '~' )
             res = comps[0]
             	res += comps[1]
             return res

         def underlinepos(self, str):
             comps = string.split( str, '~' )
                 return -1, comps[0];
             	# two tildas??????
             #print "len is %d %d\r" % ( len(comps), len(comps[0]) )
             return len(comps[0]), comps[0]+comps[1]

         def yes(self, event=None): self.quit(self, 0)
         def no(self, event=None): self.quit(self, 1)
         def cancel_command(self, event=None): self.quit(self, 2)

         def ok_event(self, event):

         def ok_command(self):

	def quit(self, ret_val, how=None):
             how = how
             rv = int(ret_val)
             retval[0] = rv
             print "%d\r" % retval[0]
             self.master.quit()              # Exit mainloop()

def ThreeButtonDriver(root, body, first, second, third, title):
     ThreeButtons(root, body, first, second, third, title)
     print "%s\r" % retval[0]
     return retval

"""Simple test program."""
root = Tk()
ThreeButtonDriver(root, "Body of the message\non two lines", "Yes", 
"~No", "Can~cel", "Sample YesNoMsg Window")

And get this trace:
python ./
Exception in Tkinter callback
Traceback (most recent call last):
   File "/usr/local/lib/python2.2/lib-tk/", line 1292, in __call__
     return apply(self.func, args)
   File "./", line 59, in no
     def no(self, event=None): self.quit(self, 1)
   File "./", line 70, in quit
     rv = int(ret_val)
AttributeError: ThreeButtons instance has no attribute '__int__'

upon pressing the second button (~No). Replacing int(ret_val) with 
ret_val on line             rv = ret_val
results in the same trace

Is there an easier way to get the number of selected button out of the 
Tkinter script?

I plan to extend the interpreter with ThreeButtonDriver and use that for 
modal dialogs in the program (the rest of the gui is in Zinc on top of 

How could I place the dialog in the cetner of the root X Window? Should 
I use placer for that?

I would appreciate any comments on how I could deuglify this script a bit.

More information about the Python-list mailing list