[英]Python/Tkinter right aligned cascading menu
我用的是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()
如果我運行這段代碼,我會得到兩個級聯菜單,標題為“文件”和“幫助”。 現在的問題是,
我希望幫助菜單右對齊,文件菜單左對齊。 必須添加哪些額外的代碼才能實現此目的?
您使用現有的Tkinter庫所做的任何事情都是黑客。 它只是不支持右對齊的菜單項。 但是這里有一些可以做的黑客:
helpmenu.add_command(label = ' Terms of use', command = dummy)
例如此helpmenu.add_command(label = ' Terms of use', command = dummy)
。 缺點是字體選擇受限,因為對齊比例字體更加困難。 但是,可以的。 您想投入多少工作並維護它,這可能是一個問題。
您對此無能為力。 官方的tk文檔非常全面,列出了所有可能的內容,並且未列出與對齊有關的任何內容。
Python 3解決了這個問題,Python 3把Tkinter版本改成了tkinter版本,在tkinter版本上面的代碼只需要改前兩行如下,
from tkinter import *
from tkinter.scrolledtext import ScrolledText
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.