Tkinter, add pressed buttons onto string display string, how to?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Apr 5 20:20:57 EDT 2008


En Sat, 05 Apr 2008 18:23:50 -0300, <skanemupp at yahoo.se> escribió:

>> Just like the message says: You are trying to use `str` (on the right  
>> hand
>> side of the assignment) before anything is bound to that name.
>>
>
> i know but i want the variable str(which i found out is a reserved
> word so i changed it) to be accessible all over __init__ right?
> so i tried to delcare it in __init__ in the beginning of the framework
> class but then when i access it in the method Display i get that
> error.
>
> so how should i declare this variable to be able to access it
> everywhere?

It should be an instance attribute: self.expr by example.
Remember to initialize it with '' in __init__

> i want another method "calculate" that can access the same string
> later and do the calculations(writing some other code now that will
> read and interpret that).

Ok, use self.expr in calculate. Something like this:

def calculate(self):
     self.expr = str(eval(self.expr))

and probably force a repaint of the calculator display. You may want to  
use a try/except block to catch any errors.


-- 
Gabriel Genellina




More information about the Python-list mailing list