user3423572
user3423572

Reputation: 559

Tkinter key press trigger event

Hi Im trying to run a function based on input from user, what am I doing wrong?

If i do

def onKeyPress(input = 1 | 2):
    playSound

it works fine if i press 1 or 2

but if i do

def onKeyPress(input = 1 | 2):
    if input == 1:
        command = playSound()
    elif input == 2:
        command = nextFile()

I get nothing if i press 1 or 2, nothing happens. I assume im not checking if input is 1 or 2 properly? Thanks

Upvotes: 1

Views: 1356

Answers (1)

falsetru
falsetru

Reputation: 369444

The event handler (or callback function) will be called with an Event object, not with an integer. The event object will never be equal to int object.

If you check specific key is pressed, use char, keysym or keycode attribute of the event passed:

def onKeyPress(event):
    if event.char == '1':  # OR  event.keycode == 49:
        playSound()
    elif event.char == '2':  # OR  event.keycode == 50:
        nextFile()

Upvotes: 2

Related Questions