In python, with tkinter, how do I create shortcut keys for my widgets?

T. Bug Reporter

I'm trying to use tkinter to make an options dialog for my Python program, and I want to add some of the refinements that most GUI programs have. Specifically, I want my options to have what tk calls "accelerator keys" - but tk's method of defining these only works on menus; for things like buttons and "checkbuttons", I can specify a character in the button text to underline, signifying the accelerator key to use, but I can't get the code to actually perform the action that would occur if the user used the mouse instead. My "invoke" commands are being flagged with error messages in the console, and I don't know why. What's wrong in the example below? (I know, probably lots of things - I'm still learning.)

# -*- coding: utf-8 -*-
import Tkinter as tk

def main():

    print("Program started")
    my_dlg_bx = tk.Tk()

    def ckbtn_action():
        if tk_ckbtn_var.get():
            print("checkbox was changed to checked")
        else:
            print("checkbox was changed to UNchecked")

    tk_ckbtn_var = tk.BooleanVar()
    tk_ckbtn = tk.Checkbutton(my_dlg_bx,
        command     = ckbtn_action,
        offvalue    = False,
        onvalue     = True,
        text        = "Press C to check the box",
        underline   = 6,
        variable    = tk_ckbtn_var,
        )
    tk_ckbtn.pack()
    
    my_dlg_bx.bind("<KeyPress-c>", tk_ckbtn.invoke)     # TypeError: invoke() takes exactly 1 argument (2 given)
    
    my_dlg_bx.attributes("-toolwindow", True)
    my_dlg_bx.attributes("-topmost", True)
    my_dlg_bx.resizable(width = False, height = False)

    my_dlg_bx.focus_force()
    my_dlg_bx.mainloop()
    print("Program ended")

main()
Pignotto

The problem is with .invoke not taking arguments other than self,

and bind expecting the callback function to take an Event instance as an argument, which tk_cktn.invoke does not do

To make it work we can just use a lambda (or equivalently a function) to take the event argument and then call tk_ckbtn.invoke():

my_dlg_bx.bind("<KeyPress-c>", lambda event: tk_ckbtn.invoke())

Then it should all work as intended

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I position my tkinter widgets right?

How do I create a Shortcut Link on my Favorites Sidebar with batch?

how to create traceable widgets with functions (python, tkinter)

How do I center widgets on tkinter?

How do I make my widgets in tkinter adjust their size automatically with respect to the window size?

Widgets become invisible when i shrink my window - Tkinter - Python

How do I create a valid shortcut for Eclipse?

How do I create a terminal shortcut to this path?

How do I use numbers in a list as keys for my dictionary in Python?

Is a shortcut and symbolic link the same? If not, how do I create a shortcut?

How do i create array/list keys in python?

How do I create a desktop shortcut for Eclipse in my Mint Cinnamon environment?

How do I create a shortcut that opens Cygwin bash shell on a directory of my choosing (UNC especially)?

How do I correct this function to create a label in Tkinter Python?

How do I use the .create_image in Tkinter in Python

How do I bind functions to tkinter widgets that are in a list of variable size?

How do I position tkinter widgets under an existing widget?

How do I resize placed tkinter widgets when the window is resized?

How do I add a keyboard shortcut to a function in tkinter

Python tkinter how to zoom in widgets

How do I create a desktop shortcut to create a new Google Doc?

How do I organize my tkinter appllication?

How do I create a table with foreign keys?

How do I align my Neumorphic widgets in concentric circles Flutter?

How do I Align my widgets with MediaQuery for all devices in Flutter?

How do I load the Antetype default widgets to use in my project?

How do I use tkinter to create a menubar?

How do i create a stop button in tkinter?

How do I create a RadioButton in my GUI in python?