Tkinter question

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Wed Jun 15 05:43:57 EDT 2005


I realise I was a bit short on advice earlier...

Martin Franklin wrote:
> Nicholas.Vaidyanathan at aps.com wrote:
> 
>>I'm sure there must be a way to do this, but I can't figure it out for 
>>the life of me… I'm writing a program where I would like to use a 
>>button's text field as part of an if statement. I set up my button like 
>>this:
>>
>> i = [ "7", "8","9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", 
>>".", "=", "+"]

you can write this as

i = "789/456*123-0.=+"

as strings are sequences and can be sliced [] just like lists


>> t = 0   #iterator through the sequence
>>
>>for x in range(4):
>>            for y in range(4):
>>                self.buttonx = Button(self, text = "%s" %i[t] , 
>>width=10, command = self.pressed)

just a note about Tkinter Button command option.  You may not know but
the callback self.pressed will not get any arguments, other tool kits
would send the instance of the button or perhaps an event, not Tkinter
there are several ways around this, my prefered is with a callback class
instance:

class Command:
     def __init__(self, text):
         self.text = text

     def __call__(self):
         ## user just pressed my button
         ## do somthing with self.text
	
and the creation of the button would look like:

self.buttonx = Button(self, text="%s" %i[t],
     width=10,
     command=Command(i[t]))


>>                self.buttonx.grid( row=x+1, column = y, sticky = W+E+S)
>>                t+=1
>>
>>What I would like to do is is check which buttons' text values are 
>>digits, and if the text is, I would like to append the number to a 
>>label. But:
>>
>>if(self.buttonx.title.isdigit):
>>
> 
> 
> To get the text of a button:
> 
> self.buttonx["text"]
> 

and to test if it is a digit:

if self.buttonx["text"] in "0123456789":
     # yay I'm a number


Martin






More information about the Python-list mailing list