Struggling to return integer from a Tk Program
Peter Abel
p-abel at t-online.de
Wed Jan 15 16:49:45 EST 2003
morden <morden at shadows.net> wrote in message news:<b029ub$7lg$1 at bob.news.rcn.net>...
> 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)
???=============================================== 1 2 3
???---Here you try to call quit with the 3 paras (self, self, 2)
>
> def ok_event(self, event):
> ok_command()
>
> def ok_command(self):
> quit()
>
> def quit(self, ret_val, how=None):
??? calling quit(self, self , 2)
> how = how
> rv = int(ret_val)
??? you try to convert "self" to an "int"
> 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.
See my remarks with ??? in your code
concerning the error.
Tkinter problems no answer.
More information about the Python-list
mailing list