Home > OS >  How can I bind KeyRelease of any spefic key in tkinter?
How can I bind KeyRelease of any spefic key in tkinter?

Time:04-18

In the following code I'm trying to make two different rectangles change their color separately, each time I release key "d" for the first rectangle and key "f" for the second one(this is because otherwise the user can just freely hold one of the two keys and increase the click count without any difficulty), but I don't know how to bind a KeyRelease of any specific key. from tkinter import * import time

def round_rectangle(canvas ,x1, y1, x2, y2, radius=25, **kwargs):
        
    points = [x1 radius, y1,
              x1 radius, y1,
              x2-radius, y1,
              x2-radius, y1,
              x2, y1,
              x2, y1 radius,
              x2, y1 radius,
              x2, y2-radius,
              x2, y2-radius,
              x2, y2,
              x2-radius, y2,
              x2-radius, y2,
              x1 radius, y2,
              x1 radius, y2,
              x1, y2,
              x1, y2-radius,
              x1, y2-radius,
              x1, y1 radius,
              x1, y1 radius,
              x1, y1]

    return canvas.create_polygon(points, **kwargs, smooth=True)

def click_one(event):
    global Clicks
    MainCanvas.itemconfig(FirstButton, fill="#ff0000")
    window.update()
    time.sleep(0.05)
    MainCanvas.itemconfig(FirstButton, fill="#111111")
    Clicks  = 1
    ClicksVar.set(Clicks)

def click_two(event):
    global Clicks
    MainCanvas.itemconfig(SecondButton, fill="#ff0000")
    window.update()
    time.sleep(0.05)
    MainCanvas.itemconfig(SecondButton, fill="#111111")
    Clicks  = 1
    ClicksVar.set(Clicks)

window = Tk()
window.geometry("960x600")
window.config(bg="#000000")

Clicks = 0
ClicksVar = IntVar()

window.bind("<KeyRelease>", click_one)
window.bind("<KeyRelease>", click_two)

MainCanvas = Canvas(window, width=960, height=600, bg="#000000")
MainCanvas.pack()

FirstButton = round_rectangle(MainCanvas ,385, 255, 475, 345, fill="#111111")
SecondButton = round_rectangle(MainCanvas ,485, 255, 575, 345, fill="#111111")

ClickLabel = Label(MainCanvas, font=("Premium", 15), width=3, height=1, textvariable=ClicksVar, bg="#111111", fg="#777777")
ClickLabel.place(x=462, y=350)

window.mainloop()

CodePudding user response:

That would be the answer to your question. If your ad an if-statement to the keydown and keyup-function you have a binding for a specific key.

def keydown(e):
   print(e.char)

def keyup(e):
    print(e.char)

widget.bind("<KeyPress>", keydown)
widget.bind("<KeyRelease>", keyup)

CodePudding user response:

How can I bind KeyRelease of any spefic key in tkinter?

You can specify the specific key along with KeyRelease. For example, the following code calls click_one when the "d" key is released, and click_two when the "f" key is released.

window.bind("<KeyRelease-d>", click_one)
window.bind("<KeyRelease-f>", click_two)

For the authoritative documentation on event specifiers, see Event Patterns in the tcl/tk manual pages.

CodePudding user response:

I use this when I want to bind a key to do an action. You can try the following:

import keyboard

shortcut = "F1"

if keyboard.is_pressed(shortcut):
  <yourcode>
  • Related