don't understand namespaces...

norseman norseman at hughes.net
Fri May 1 21:04:48 CEST 2009


Simon Forman wrote:
> On Apr 30, 10:11 am, Lawrence Hanser <lhan... at gmail.com> 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
> 
...(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



More information about the Python-list mailing list