Home > Back-end >  How to disable the <KeyRelease> event only when the enter key is pressed
How to disable the <KeyRelease> event only when the enter key is pressed

Time:11-13

I want to disable the <KeyRelease> event only when the enter key is pressed because I have two functions that use either the <Return> event or the <KeyRelease> event, but when I press enter to activate the function that uses the <Return> event, the other function that gets activated with the <KeyRelease> event also activates, and this is an issue. Anything I looked up simply said to disable the specific key, but I need the enter key enabled for one of the functions to activate.

import tkinter as tk


root = tk.Tk()
root.geometry("500x500 0 0")


def function1(e):
    print('hi')


def function2(e):
    print('hello')


root.bind("<Return>", function1)
root.bind("<KeyRelease>", function2)


root.mainloop()

CodePudding user response:

You can exit from function2 if Enter key was pressed. To do this, you can check keysym event property:

def function2(e):
    if e.keysym == 'Return':
        return
    print('hello')
  • Related