[英]Python/Tkinter right aligned cascading menu
I am using Tkinter / Python, and I have written the following code:我用的是Tkinter / Python,我写了如下代码:
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. 您使用现有的Tkinter库所做的任何事情都是黑客。 It just doesn't support right-aligned menu entries.
它只是不支持右对齐的菜单项。 But here are some hacks that can be done:
但是这里有一些可以做的黑客:
helpmenu.add_command(label = ' Terms of use', command = dummy)
. helpmenu.add_command(label = ' Terms of use', command = dummy)
例如此helpmenu.add_command(label = ' Terms of use', command = dummy)
。 Downside is limited font selection because it is more difficult to align proportional fonts. 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.
官方的tk文档非常全面,列出了所有可能的内容,并且未列出与对齐有关的任何内容。
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, Python 3解决了这个问题,Python 3把Tkinter版本改成了tkinter版本,在tkinter版本上面的代码只需要改前两行如下,
from tkinter import *
from tkinter.scrolledtext import ScrolledText
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.