473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Long Tkinter Menu

I don't know if this is because of Tkinter (ie Tk) itself or the
Windows default way of handling things, but when I create a very long
menu (my test is shown below), the way it displays is rather sucky; the
menu stretches from the top of the moniter's window to the bottom (no
matter the size of the actual application).

Is there any alternative format for how a long menu gets displayed? It
would be nice if say, I could make the menu only go to the borders of
the application itself (in this case, not that long).

As for why I'm creating such a long menu, think browser bookmarks
(That's not actually what I'm doing, but similar).

================================
# menu-example-5.py

from Tkinter import *

root = Tk()

menubar = Menu(root)

menu = Menu(menubar, tearoff=0)
for i in xrange(100):
menu.add_command(label=str(i), command=root.quit)
menu.add_command(label="Exit", command=root.quit)

menubar.add_cascade(label="Test", menu=menu)

root.config(menu=menubar)

mainloop()
================================

Oct 5 '06 #1
2 2210
On Thu, 05 Oct 2006 02:33:54 +0200, Dustan <Du**********@gmail.comwrote:
I don't know if this is because of Tkinter (ie Tk) itself or the
Windows default way of handling things, but when I create a very long
menu (my test is shown below), the way it displays is rather sucky; the
menu stretches from the top of the moniter's window to the bottom (no
matter the size of the actual application).

Is there any alternative format for how a long menu gets displayed? It
would be nice if say, I could make the menu only go to the borders of
the application itself (in this case, not that long).
To limit the menu in the application window, will be difficult. But here
are two ways of automatically limiting the number of entries that can
appear in a menu by specializing the Tkinter Menu class:

------------------------------------------------------
from Tkinter import *

class LongMenu(Menu):
"""
Automatically creates a cascade entry labelled 'More...' when the
number of entries is above MAX_ENTRIES.
"""

MAX_ENTRIES = 20

def __init__(self, *args, **options):
Menu.__init__(self, *args, **options)
self.nextMenu = None

def add(self, itemType, cnf={}, **kw):
if self.nextMenu is not None:
return self.nextMenu.add(itemType, cnf, **kw)
nbEntries = self.index(END)
if nbEntries < LongMenu.MAX_ENTRIES:
return Menu.add(self, itemType, cnf, **kw)
self.nextMenu = LongMenu(self)
Menu.add(self, 'cascade', label='More...', menu=self.nextMenu)
return self.nextMenu.add(itemType, cnf, **kw)
class AutoBreakMenu(Menu):
"""
Automatically adds the 'columnbreak' option on menu entries to make
sure that the menu won't get too high.
"""

MAX_ENTRIES = 20

def add(self, itemType, cnf={}, **kw):
entryIndex = 1 + (self.index(END) or 0)
if entryIndex % AutoBreakMenu.MAX_ENTRIES == 0:
cnf.update(kw)
cnf['columnbreak'] = 1
kw = {}
return Menu.add(self, itemType, cnf, **kw)

if __name__ == '__main__':
root = Tk()

menubar = Menu(root)

def fillMenu(menu):
for i in xrange(100):
menu.add_command(label=str(i), command=root.quit)
menu.add_command(label="Exit", command=root.quit)

menu1 = LongMenu(menubar, tearoff=0)
fillMenu(menu1)
menu2 = AutoBreakMenu(menubar, tearoff=0)
fillMenu(menu2)

menubar.add_cascade(label="Test1", menu=menu1)
menubar.add_cascade(label="Test2", menu=menu2)

root.config(menu=menubar)

root.mainloop()
------------------------------------------------------

If your application is more complicated than that (e.g if you insert menu
entries after the first adds), you'll have to change the code above a bit,
since it doesn't handle calls to insert at all. But you get the idea.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Oct 5 '06 #2

Eric Brunel wrote:
On Thu, 05 Oct 2006 02:33:54 +0200, Dustan <Du**********@gmail.comwrote:
I don't know if this is because of Tkinter (ie Tk) itself or the
Windows default way of handling things, but when I create a very long
menu (my test is shown below), the way it displays is rather sucky; the
menu stretches from the top of the moniter's window to the bottom (no
matter the size of the actual application).

Is there any alternative format for how a long menu gets displayed? It
would be nice if say, I could make the menu only go to the borders of
the application itself (in this case, not that long).

To limit the menu in the application window, will be difficult. But here
are two ways of automatically limiting the number of entries that can
appear in a menu by specializing the Tkinter Menu class:

------------------------------------------------------
from Tkinter import *

class LongMenu(Menu):
"""
Automatically creates a cascade entry labelled 'More...' when the
number of entries is above MAX_ENTRIES.
"""

MAX_ENTRIES = 20

def __init__(self, *args, **options):
Menu.__init__(self, *args, **options)
self.nextMenu = None

def add(self, itemType, cnf={}, **kw):
if self.nextMenu is not None:
return self.nextMenu.add(itemType, cnf, **kw)
nbEntries = self.index(END)
if nbEntries < LongMenu.MAX_ENTRIES:
return Menu.add(self, itemType, cnf, **kw)
self.nextMenu = LongMenu(self)
Menu.add(self, 'cascade', label='More...', menu=self.nextMenu)
return self.nextMenu.add(itemType, cnf, **kw)
class AutoBreakMenu(Menu):
"""
Automatically adds the 'columnbreak' option on menu entries to make
sure that the menu won't get too high.
"""

MAX_ENTRIES = 20

def add(self, itemType, cnf={}, **kw):
entryIndex = 1 + (self.index(END) or 0)
if entryIndex % AutoBreakMenu.MAX_ENTRIES == 0:
cnf.update(kw)
cnf['columnbreak'] = 1
kw = {}
return Menu.add(self, itemType, cnf, **kw)

if __name__ == '__main__':
root = Tk()

menubar = Menu(root)

def fillMenu(menu):
for i in xrange(100):
menu.add_command(label=str(i), command=root.quit)
menu.add_command(label="Exit", command=root.quit)

menu1 = LongMenu(menubar, tearoff=0)
fillMenu(menu1)
menu2 = AutoBreakMenu(menubar, tearoff=0)
fillMenu(menu2)

menubar.add_cascade(label="Test1", menu=menu1)
menubar.add_cascade(label="Test2", menu=menu2)

root.config(menu=menubar)

root.mainloop()
------------------------------------------------------

If your application is more complicated than that (e.g if you insert menu
entries after the first adds), you'll have to change the code above a bit,
since it doesn't handle calls to insert at all. But you get the idea.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Thanks, I'll see what I can do with that.

Oct 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
2
by: James Ash | last post by:
I'm writing a very simple and small Ptyhon/Tkinter application and I'm having trouble getting the menus to appear correctly. Rather than a name appearing on the menu bar, I see "()" instead. ...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
5
by: Paul Rubin | last post by:
I have a gui with a bunch of buttons, labels, the usual stuff. It uses the grid manager: gui = Frame() gui.grid() gui.Label(....).grid() # put some widgets into the gui ... # more widgets...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
2
by: ishtar2020 | last post by:
Hi everybody I'd appreciate some help on creating a tear off menu with TkInter. I've been reading some documentation but still no luck. Please don't get confused: when I mean "tear off" menu I...
2
by: Doran, Harold | last post by:
I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc was published in 1999 and I wonder if there is a more recent version. I've googled a bit and this version is the one I keep...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
3
by: Eric Brunel | last post by:
Hello all, I'm trying out Python 2.6 and I found what might be a bug in the Tkinter module. How can I report it? The possible bug is a traceback when trying to delete a menu item in a menu...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.