Tkinter, add pressed buttons onto string display string, how to?
Steve Holden
steve at holdenweb.com
Sat Apr 5 22:24:33 EDT 2008
7stud wrote:
>>> 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.
>>> Ciao,
>>> Marc 'BlackJack' Rintsch
>> 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?
>>
>
> "all over __init__" ? You could practice with a trivial example to
> discover how things work in python:
>
> def f():
> num = 10
> print num
>
> f()
>
> def g():
> print num
> num = 10
>
> g()
>
>
>> 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?
>>
>
> You don't declare variables in python. You just start using a
> variable when you need it. In other words you don't do this:
>
> string my_str
> my_str = "hello"
>
> You just write:
>
> my_str = "hello"
>
>
>> 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).
>
> Does this look familiar:
>
>> Another thing you should be aware of: self is like a class wide
>> bulletin board. If you are writing code inside a class method, and
>> there is data that you want code inside another class method to be
>> able to see, then post the data on the class wide bulletin board, i.e.
>> attach it to self. But in your code, you are doing this:
>>
>> self.btnDisplay = Button(self, text="7", default=ACTIVE)
>> self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)
>>
>> self.btnDisplay = Button(self, text="8", default=ACTIVE)
>> self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)
>>
>> As a result, your code continually overwrites self.btnDisplay. That
>> means you aren't preserving the data assigned to self.btnDisplay.
>> Therefore, the data does not need to be posted on the class wide
>> bulletin board for other class methods to see. So just write:
>>
>> btnDisplay = Button(self, text="7", default=ACTIVE)
>> btnDisplay.grid(row=5, column=0, padx=5, pady=5)
>>
>> btnDisplay = Button(self, text="8", default=ACTIVE)
>> btnDisplay.grid(row=5, column=1, padx=5, pady=5)
To pick nits, "str" is not a reserved word (normally referred to in
Python as s "keyword"). It's perfectly possible to write:
>>> def str(x):
... return "Fooled you!"
...
>>> str(3.14148)
'Fooled you!'
>>>
and have your program work. But it's not generally good practice to
*shadow* built-in names, even when nothing stops you from doing so,
simply because there is usually a group somewhere with an investment in
using the standard names, and you will make their lives more difficult.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list