简体   繁体   中英

Python/Tkinter right aligned cascading menu

I am using Tkinter / Python, and I have written the following code:

from Tkinter import *
from ScrolledText import *

#dummy function to be changed later
def dummy():
    print 'Oont ooncha, oont ki peeth oonchi, neechi oonth ki poonch!'

#create your main window
root = Tk(className = 'Mere Bhains wala Editor')

#mera menu
my_menu = Menu(root)

#attach this menu to the the application
root.config(menu = my_menu)

#create my file menu
filemenu = Menu(my_menu)
filemenu.add_command(label = 'New', command = dummy)
filemenu.add_command(label = 'Open', command = dummy)
filemenu.add_separator()
filemenu.add_command(label = 'Save', command = dummy)
filemenu.add_command(label = 'Save as', command = dummy)

my_menu.add_cascade(label = 'File', menu = filemenu)


#create Help menu
helpmenu = Menu(my_menu)
helpmenu.add_command(label = 'Terms of use', command = dummy)
helpmenu.add_command(label = 'Documents', command = dummy)
helpmenu.add_command(label = 'FAQ', command = dummy)
helpmenu.add_separator()
helpmenu.add_command(label = 'Community discussions', command = dummy)
helpmenu.add_command(label = 'Report issue', command = dummy)
helpmenu.add_command(label = 'Search issues', command = dummy)

my_menu.add_cascade(label = 'Help', menu = helpmenu)

#create the scrolled text area
textpad = ScrolledText(root, width=640, height = 480)
textpad.pack()

# run the window as the application
root.mainloop()

If I run this code, then I get two cascading menus titled File and Help. Now the question,

I want the Help menu to be right aligned, and the File menu to be left aligned. What additional code, has to be put, so that I can achieve this?

Anything you do with the existing Tkinter libraries will be a hack. It just doesn't support right-aligned menu entries. But here are some hacks that can be done:

  1. Use a fixed-width font like Courier and space-pad the labels like this helpmenu.add_command(label = ' Terms of use', command = dummy) . Downside is limited font selection because it is more difficult to align proportional fonts.
  2. Change all menu labels to images/bitmaps that represent the text. Each image width and height must be the same and aligned manually in the image. Here is a sample using your code and images I created using an arbitrary font in a paint program. Downside here is maintenance. If you want to change a menu entry, you have to create a new image. 在此处输入图片说明

But yes, it can be done. How much work you want to put into it and maintain it is probably the question.

You have no control over this. The official tk documentation is very comprehensive and lists everything that is possible, and it doesn't list anything related to alignment.

This issue is solved in Python 3, Python 3 changes the Tkinter version into tkinter version, in tkinter version the above code only need to change the first two lines as below,

from tkinter import *
from tkinter.scrolledtext import ScrolledText

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM