tkinter menus

Keith Murphy kpmurphy at my-deja.com
Tue Jul 11 10:49:08 EDT 2000


In article <vi7lms4dirkhhk7cr1kjfrjkgv5sp86e5d at 4ax.com>,
  mwr at hsl.tcp.co.uk wrote:
> On Mon, 10 Jul 2000 17:59:28 GMT, Keith Murphy <kpmurphy at my-deja.com>
> wrote:
>
> >In article <5vujmsg8hdf54luae6e2c0jivv93qheu9h at 4ax.com>,
> >  mwr at hsl.tcp.co.uk wrote:
> >> On Mon, 10 Jul 2000 14:26:40 GMT, Keith Murphy
<kpmurphy at my-deja.com>
> >> wrote:
> >>
> >> >menu, menu... anyone, anyone?
> >> >
> >> >	How do you configure top-level menu entries?  I'd like to put a
> >'help'
> >> >menu (and possibly others) on the right end of the menu bar.  I've
> >seen
> >> >examples where you make your own frame... but there has to be a
way
> >> >built in... doesn't there?  :)
> >> >
> >> In Tkinter, when you use the appropriate geometry manager to put
the
> >> button in the menu bar. E.g:
> >> helpButton = Menubutton(myMenuBar, ...)
> >> helpButton.Pack(side=RIGHT, ...)
> >>
> >> Or if you're using Pmw, then you can do something like:
> >> myMenuBar = Pmw.MenuBar(...)
> >> myMenuBar.addmenu(side=RIGHT,...)
> >>
> >> --
> >> Martin Rand
> >> Highfield Software Ltd
> >> mwr at highfield-software.co.uk
> >> Phone: +44 (0)23 8025 2445
> >> Fax:   +44 (0)23 8025 2445
> >>
> >
> >
> >well i am using a system menubar, not one i've created.  i'm using
> >fredrik lundh's 'an introduction to tkinter'
> >(http://www.pythonware.com/library/tkinter/introduction/index.htm) as
my
> >guide.  here is an example he uses:
> >
> ># File: menu-example-2.py
> >
> >from Tkinter import *
> >
> >root = Tk()
> >
> >def hello():
> >   print "hello!"
> >
> ># create a toplevel menu
> >menubar = Menu(root)
> >menubar.add_command(label="Hello!", command=hello)
> >menubar.add_command(label="Quit!", command=root.quit)
> >
> ># display the menu
> >root.config(menu=menubar)
> >
> >mainloop()
> >
> >
> >
> >...if you try and pack or anything, you will get the following
message:
> >TclError: can't pack ".1977984.1980464": it's a top-level window
> >
> >... any more help?  thanks :)
> >
> Well, you could go at it this way:
>
> ##########################
>
> from Tkinter import *
>
> def doExit():
>     pass  # Your code goes here...
>
> root = Tk()
> root.geometry("640x480") # Just to show packing works as expected
> menubar = Frame(root)
> menubar.pack(fill=X)
>
> # If you want to add a drop-down...
> hellobutton = Menubutton(menubar, text='Hello!')
> hellobutton.pack(side=LEFT)
> hellobutton.menu = Menu(hellobutton)
> # etc...
> goodbyebutton = Menubutton(menubar, text='Goodbye!')
> goodbyebutton.pack(side=LEFT)
> goodbyebutton.menu = Menu(goodbyebutton)
> # etc....
>
> # If you want to add an immediate action...
> quitbutton = Button(menubar, text='Quit', relief=FLAT, command =
> doExit)
> quitbutton.pack(side=RIGHT)
>
> root.mainloop()
>
> #############################
>
> But I must admit that it's to avoid this sort of grunt work that I go
> for Pmw wherever I can...
>

if you notice, there isn't any packing involved in my example.  i think
that's because it's not a part of the window's geometry, but is actually
built into the window itself. the best would be to be able to alter the
line to:

menubar.add_command(label="Quit!", side=RIGHT, command=root.quit)

(the new part is 'side=RIGHT')

is there a way to do something like this?  thanks gang,

-->keith


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list