don't understand namespaces...

L

Lawrence Hanser

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
 
M

Mike Driscoll

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

It depends on how you're doing this. If the first frame opens the 2nd
frame, then you can have a handle to the 2nd in the 1st. Something
like this:

self.newFrame = NewFrame()

Then you can get at the attributes of the 2nd frame:

self.newFrame.SomeWidget.GetSomeValue()

If you're opening both at the same time, you'll have to come up with
something else, like Queues or pubsub.

- Mike
 
S

Simon Forman

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...
....
app.doSomething(app2.some_name)

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)
app.setWidget(app2.some_name)
root.mainloop()

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

HTH,
~Simon
 
N

norseman

....(snip) # you already have Simon's approach and I'm just shorting the
overall reply.

---------------------------------

IF you think each widget is supposed to be a program,
I would have to say NO!

Each widget is just a pretty interface to the human.

build your program!
use the GUI to communicate with the human IN LIEU OF A COMMAND LINE. !!
That is all the GUI is for.!!!!(teacher stomping foot, shaking finger :)

instead of print() do msgbox()
instead of getch() do textbox()
instead of (command line thingy) do (GUI thingy)

and so on.
Your program is a program.
It just communicates differently.
Remember: Designing for the clicker is designing for the brain dead. :)
(True - not in all cases, but mostly)

import Tkinter
help(Tkinter)
(read!)

in short

import Tkinter # load lib

root= Tk # use template, go live

main= Frame(root,... # the mockup board
frame1= Frame(main,... # frame1 will be found inside main
frame2= Frame(main,.... # frame2 will be found inside main
button1= Button(frame1,... # button1 will be found inside frame1
frame3= Frame(root,... # frame3 will float about on its own
msgbox1= MsgBox(where,... # and so it goes
..
..
root.mainloop() # run it

Those widgets with a callback= allowed can send their answers to a
function that has has been defined previously *above* in the code.

MyYNVal= StrVal() # here they are before example below
MyYNVal.set("*") # to be used with above example they
#
#
def somehandler(): # need to be in same order and
a_global= MyYNVal.get() # before the root= line
root.quit() #



rbframe= Frame(Tk,...
rb1= Radiobutton(rbframe,....., variable=MyYNVal, value= "N",
command=somehandler, ... )
rb2= Radiobutton(rbframe,....., variable=MyYNVal, value= "Y",
command=somehandler, ... )



program calls GUI # how? encapsulate widgets in a def name()
# name()
human pushes button of choice which sets MyYNVal and calls somehandler
which returns to program
human_said= a_global
if human_said == "Y":
program process accordingly
else:
program process accordingly
..
..

Does that help?

And Yes - you can pre-edit/validate the GUI entries while still in the
GUI. It gets a bit complicated because anything Tkinter is to use is
already defined by time of use. Read that sentence again.
(One pass interpreter. The pre-scan is for syntax only. It does not
build address or set variables until actual run.
)


Today: 20090430
snippets are generalized for concept

HTH
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top