3 beginner questions!

Chad Netzer cnetzer at mail.arc.nasa.gov
Wed May 7 20:33:45 EDT 2003


On Tue, 2003-05-06 at 10:04, Ali Dada wrote:

> second question (Tkinter and Pmw)

Don't take this the wrong way, but because of limited time, I'm just
going to give you some basic advice, and then show you the application
as it should be written:

1)  Don't call your file ScrolledListBox.py, because you are using a
Pmw.ScrolledListBox and things will be confusing (besides, you aren't
defining a ScrolledListBox in it at all; your module should be named
after what it ultimately wants to do, even just "app.py" would be
better)

2) You want to call Tk() once, in the main section.  Similar to the
mainloop().  You should NOT call either of these in object methods; my
example below will show you what I mean.  Notice that the mainloop() is
the last function that the python interpreter enters... It is called
once, and doesn't return until the user somehow destroys the root window
(ie. exits the program).  Within the mainloop(), Tk is executing and
handling all the "events" that occur (button presses, mouse moves,
window events, etc.)

3) If you call Pmw.initialise(), this will initialize Tk for you (ie. it
does a Tk() call, and returns the result, among other things.)

4) You should give your subject line more details (ie. at the VERY leats
mention Tkinter; I missed it the first time because I didn't scroll down
to your second question, and had no idea there was any Tkinter content
in your original post)

5) Index is much too general a name for a class, unless it is
unambiguously part of a larger framework.  I called it ImageIndex, which
is marginally better.

6) Check out <a
href="http://home.att.net/~stephen_ferg/thinking_in_tkinter/">Thinking
in Tkinter</a> by Steve Ferg, if you haven't already.  Your career with
Tkinter will be MUCH happier.

7) Do not EVER derive from a Frame widget, and then pack that widget in
the __init__() method.  A widget should NOT pack itself.  The Frame
widget you derive from does not pack() itself, and since you are
deriving ImageSelection from a Frame widget, it is ALSO a Frame widget. 
And thus you leave it up to the code that instantiates that widget to
pack it.  This is *THE NUMBER ONE* mistake that new Tkinter users make,
and although it seems unimportant, you should unlearn it now.  What
happens when you try to use that widget and put it in a gridded frame? 
Your application crashes.

Here is a rewitten version of your original program.  It is all in one
file.  This does what your original program attempted to do, but is
still not perfect.  For example, see what happens when the "Regression"
button is pressed multiple times?  You can deal with these issues once
you understand the changes I made:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/env python
from Tkinter import *

import Pmw

class ImageSelection(Frame):
    def __init__( self):
        Frame.__init__(self)

        self.suites=['a', 'b', 'c']

        widget = Pmw.ScrolledListBox
        self.listBox = widget(root,
                              items = self.suites,
                              listbox_height = 5,
                              vscrollmode = "static",
                              selectioncommand = self.switchSuite)
        self.listBox.pack(side=LEFT, expand=YES, fill=BOTH,
                          padx=5, pady=5)
        return

    def switchSuite(self):
        print 'hi'
  

class Index(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)

        button = Button(self, text='Regression',
                        command=self.button_func)
        button.pack(side=TOP, fill=BOTH)
        return

    def button_func( self ):
        image_selection = ImageSelection()
        image_selection.pack(expand = YES, fill=BOTH)
        return


if __name__ == "__main__":
    root = Pmw.initialise()

    image_index = Index()
    image_index.pack()

    root.mainloop()




> third question (Tkinter):
> 
> i am programming a menu bar that should appear alone in the window and should
> span the width of the screen and should be displayed at the top of the screen.
> any idea how this should be implemented?

Well, your third example is WAY too much code for me to even look at. 
I'd suggest you post a small example with the minimal attempt at what
you want.  In the meantime, I'll say that if you want the menubar to
appear at the top of the screen, and NOT at the top of your application
window, this behavior depends on the system you are running on.  It'll
work on Macs, for example, but Windows doesn't work this way.  If you
want it at the top of your applicatin window, then keep trying after
looking at more Tkinter examples on the Web or search for my old
postings on Google.  For example, search Google groups for "Chad Netzer
tkinter menubar".


-- 

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)






More information about the Python-list mailing list