Some basic questions about Tkinter (probably v easy for experts!)

Fredrik Lundh fredrik at effbot.org
Wed Feb 7 08:42:00 EST 2001


Martyn Quick wrote:
> (1) If I am going to use Pmw, then I need to have Tkinter installed.  But
> do I need to understand how to use Tkinter (at least to a basic
> degree) before I can use Pmw?

cannot hurt -- Pmw just adds (a whole bunch of) new
components to Tkinter.

> (2) Should a typical Tkinter program contain the following sort of lines:
>
> from import Tkinter *
> aardvark = Tk()
> aardvark.mainloop()
>
> Am I right in thinking the last line is what produces the Tk window and
> that without it nothing would happen?  Do I then just attach widgets the
> aardvark object defined above and further widgets to these ones, etc.?

have you read the introduction document?  the first chapter
seems to answer your questions:

http://www.pythonware.com/library/tkinter/introduction/hello-tkinter.htm

> (3) How do I run such a program?  Is the right way to open up a DOS box
> (I'm using Windows 95) and type  python aardvark.py  at the prompt?

fwiw, that's what I usually do (when I'm not using PythonWorks).

> (4) Will the following correctly add a menu to my aardvark object?

short answer: almost.

> from sys import exit
> armadillo = Menu(aardvark)

this creates a menu component instance.

> armadillo.add_cascade(label="File")

this adds a submenu to the admadillo menubar.  but you
didn't pass in the submenu instance, so there's nothing to
display when the user clicks on "File"...

try this instead:

    filemenu = Menu(armadillo) # create a submenu
    armadillo.add_cascade(label="File", menu=filemenu)

> armadillo.add_command(lable="Exit", command=exit)

if you fix the typo, this adds a command item to the armadillo
menubar.  when that item is selected, Tkinter calls sys.exit.

if you want to add it to the file menu instead of the armadillo
menubar, try this:

    filemenu.add_command(label="Exit", command=exit)

finally, you need to attach the menu to your window:

    aardvark.config(menu=armadillo)

> If this isn't right - what is right, and more importantly *why* is it
> right?

http://www.pythonware.com/library/tkinter/introduction/x953-menus.htm

hope this helps!

Cheers /F





More information about the Python-list mailing list