don't understand namespaces...

Simon Forman sajmikins at
Fri May 1 05:54:16 CEST 2009

On Apr 30, 10:11 am, Lawrence Hanser <lhan... at> wrote:
> Dear Pythoners,
> I think I do not yet have a good understanding of namespaces.  Here is
> what I have in broad outline form:
> ------------------------------------
> import Tkinter
> Class App(Frame)
>       define two frames, buttons in one and Listbox in the other
> Class App2(Frame)
>       define one frame with a Text widget in it
> root = Tk()
> app = App(root)
> win2 = Toplevel(root)
> app2 = App2(win2)
> root.mainloop()
> ------------------------------------
> My understanding of the above goes like this:
> 1) create a root window
> 2) instantiate a class that defines a Frame in the root window
> 3) create another Toplevel window
> 4) instantiate another class that defines a frame in the Toplevel window (win2)
> What I cannot figure out is how to reference a widget in app2 from app...
> I hope this is sort of clear.
> Any assistance appreciated.
> Thanks,
> Larry

There are lots of different ways to do this in python.  If you were to
set an instance variable to the widget you're interested in in the
__init__() method of the App2 class, i.e. self.some_name = Text(...),
then you can pass it to a method of App as a parameter.

class App(Frame):
    def doSomething(self, widget):
        do something with the widget...

In your case, I think this is what you might want to do:

class App(Frame):
    def setWidget(self, widget):
        self.widget = widget

root = Tk()
app = App(root)
win2 = Toplevel(root)
app2 = App2(win2)

Then code in App can use self.widget to access the widget.


More information about the Python-list mailing list