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

7stud bbxx789_05ss at yahoo.com
Sat Apr 5 20:53:46 EDT 2008


> > 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)



More information about the Python-list mailing list