[Python / Tkinter] Connect keyboard shortcuts to Menu

Introduction

In Tkinter's Menu, you can make it look nice by specifying the accelerator option when adding a menu button with a command, but in reality this alone does not enable keyboard shortcuts. Work to associate with the method is required. Here is a sample for that.

sample

import tkinter as tk
from tkinter import ttk


class Application(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.pack()
        self.master.geometry("300x300")
        self.master.title("Menubar Sample")
        self.create_menubar()

    def create_menubar(self):
        menubar = tk.Menu(self)
        file_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="Open", command=self.onOpen, accelerator="Ctrl+O")
        self.master.config(menu=menubar)
        self.bind_all("<Control-o>", self.onOpen)

    def onOpen(self, event=None):
        print("onOpen")


def main():
    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()


if __name__ == "__main__":
    main()

OnOpen is linked to the application with self.bind_all. When executing onOpen with a keyboard shortcut, event is passed as an argument, but it is not passed when the menu button is clicked directly. Therefore, the initial value of event is set to None to correspond to the latter. If this is a hassle, let's define it separately.

Recommended Posts

[Python / Tkinter] Connect keyboard shortcuts to Menu
Connect python to mysql
Connect to BigQuery with Python
Connect to Wikipedia with Python
How to update Python Tkinter to 8.6
Connect to sqlite from python
Connect to utf8mb4 database from python
Connect to MySQL with Python within Docker
[Python / Tkinter] How to pass arguments to command
Connect to s3 with AWS Lambda Python
Connect to pepper with PEPPER Mac's python interpreter
Connect to coincheck's Websocket API from Python
Updated to Python 2.7.9
Introduction to Tkinter 1: Introduction
Connect to mysql
How to use tkinter with python in pyenv
Connect to MySQL with Python on Raspberry Pi
Use Tor to connect from urllib2 [Python] [Mac]
"Backport" to python 2
[Python] Tkinter template
[Python] Change standard input from keyboard to text file
[Python] tkinter Code that is likely to be reused
How to install Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Rewrite Python2 code to Python3 (2to3)
python decorator to retry
Introduction to Python language
Introduction to OpenCV (python)-(2)
Note to daemonize python
Introducing Python 2.7 to CentOS 6.6
[Python MinMaxScaler] Normalize to 0 ~ 1
Python Tkinter Primer Note
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
Connect to the Bitcoin network using pycoin (Python Cryptocoin Utili)
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python