Struggling to return integer from a Tk Program

morden morden at shadows.net
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, 
title=None):
             if title is None: title = self.title
             self.master = master

             self.top = Toplevel(master)
             self.top.title(title)
             self.top.iconname(title)

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

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

             upos,text=self.underlinepos(but2)
             Button(self.top, {'text':text, 'command':self.no, \
                               Pack:{'side':'left', 'padx':'20', 
'pady':'20'}}, underline=upos )
             self.top.bind('<Alt-n>', self.no)

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

             self.top.bind('<Alt-c>', self.cancel_command)

             self.top.bind('<Alt-w>', self.cancel_command)
             self.top.bind('<Alt-W>', self.cancel_command)
             self.top.bind('<Alt-F4>', self.cancel_command)
             self.top.protocol('WM_DELETE_WINDOW', self.cancel_command)
             self.master.mainloop()

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

         def underlinepos(self, str):
             comps = string.split( str, '~' )
             if(len(comps)<=1):
                 return -1, comps[0];
             if(len(comps)>2):
             	# two tildas??????
                 noop=1
             #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):
             ok_command()

         def ok_command(self):
             quit()

	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()
root.withdraw()
ThreeButtonDriver(root, "Body of the message\non two lines", "Yes", 
"~No", "Can~cel", "Sample YesNoMsg Window")



And get this trace:
python ./yesmsg.py
Exception in Tkinter callback
Traceback (most recent call last):
   File "/usr/local/lib/python2.2/lib-tk/Tkinter.py", line 1292, in __call__
     return apply(self.func, args)
   File "./yesmsg.py", line 59, in no
     def no(self, event=None): self.quit(self, 1)
   File "./yesmsg.py", 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 
Motif).

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