简体   繁体   中英

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. 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 Specifies a string to display at the right side of the menu entry. 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")

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