views:

43

answers:

1

Hello,

I'm working on a gui and I'd like to know how to adjust the size of the menus of a frame in order to have them take all the horizontal space of the frame.

The problem has changed : now the menu buttons are ok when the window is in normal size but when I resize it the menu buttons drop in the middle of the window. How can I make them stick to the top of the frame ?

rgds,

A: 

Your question lacks enough detail to give you a good answer. Are you creating a menubar by putting menu buttons in a frame? If so, that's the wrong way to do it. Create a menu widget and assign it to the menu property of the main window and you'll end up with standard menus that behave normally.

Here's a simple example:

import Tkinter

root = Tkinter.Tk()
menubar = Tkinter.Menu(root)
root.config(menu=menubar)

fileMenu = Tkinter.Menu(menubar, tearoff=False)
editMenu = Tkinter.Menu(menubar, tearoff=False)

menubar.add_cascade(label="File",underline=0, menu=fileMenu)
menubar.add_cascade(label="Edit",underline=0, menu=editMenu)

fileMenu.add_command(label="Open...", underline=0)
fileMenu.add_command(label="Save", underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=1)

editMenu.add_command(label="Cut", underline=2)
editMenu.add_command(label="Copy", underline=0)
editMenu.add_command(label="Paste", underline=0)

root.mainloop()
Bryan Oakley
Thanks for your tips. My main window being a frame does it have a menu property ?
Bruno
@Bruno: "menu" is an attribute of toplevel windows. Every Tkinter app has at least (and usually only) one. This is commonly called "root". I'll add an example to my answer to hopefully clarify.
Bryan Oakley
Thanks, I've tried your approach and it works great. There is only a minor problem : when i resize the window the menus stay in the left of the menu bar, is it possible to have them to take all the horizontal space (curiosity) ?
Bruno
@Bruno: no, it is not possible, and you don't want to do that. Your menu should be consistent with standard menu behavior. Your users will thank you. If you _really_ need the behavior you describe your only choice is to use frames and menubuttons. I don't recommend that approach if you're trying to create a GUI that people will want to use.
Bryan Oakley
Thanks a lot, I'll follow your recommendation !
Bruno