简体   繁体   English

在Tkinter菜单中将快捷方式信息右对齐

[英]Align Shortcut info to right in Tkinter Menu

I'm writing an application in Tkinter and I'm creating a ctrl+s save feature and I want to show that in the menu, but I can't get the actual text "Ctrl+S" to right align in the command. 我正在Tkinter中编写一个应用程序,并且正在创建ctrl + s保存功能,我想在菜单中显示该功能,但是我无法在命令中获得实际的文本“ Ctrl + S”以使其右对齐。 I tried using tabs as in the following example, but that didn't work, and I couldn't find anything on it. 我尝试使用以下示例中的选项卡,但这没有用,而且我也找不到任何东西。 Here's what I tried: 这是我尝试过的:

from tkinter import *

class Gui:
    def __init__(self):
        self.root = Tk()

        self.menu = Menu(self.root)
        self.root.config(menu=self.menu)

        self.label = Label(self.root, text="Welcome to dummy program")
        self.label.pack(padx=40, pady=20)

        self.file_menu = Menu(self.root, tearoff=False)
        self.menu.add_cascade(label="File", menu=self.file_menu)

        # In the next two lines is where I need the text aligned to the right
        self.file_menu.add_command(label="Save As \t Ctrl+S", command=lambda: self.label.config(text="Saved!"))
        self.file_menu.add_command(label="Open \t Ctrl+O", command=lambda: self.label.config(text="Opened!"))

        self.root.mainloop()

Gui()

But here's what I want it to look like: 但是这是我想要的样子: 在此处输入图片说明

And I'm not sure how to control the spacing and alignment. 而且我不确定如何控制间距和对齐方式。 Thank you. 谢谢。

Menu items have an accelerator attribute specifically for this purpose: 菜单项具有专门用于此目的的accelerator属性:

accelerator Specifies a string to display at the right side of the menu entry. accelerator指定要显示在菜单项右侧的字符串。 Normally describes an accelerator keystroke sequence that may be typed to invoke the same function as the menu entry. 通常描述一个加速键序列,可以键入该序列以调用与菜单项相同的功能。 This option is not available for separator or tear-off entries. 该选项不适用于分隔符或撕下条目。

self.file_menu.add_command(..., accelerator="Ctrl+S")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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