2014-11-22 180 views
1

嗨我试图运行基于用户的输入功能,我做错了什么?Tkinter按键触发事件

,如果我做

def onKeyPress(input = 1 | 2): 
    playSound 

如果我按1或2

,但如果我这样做

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

我得到什么,如果我按1或2个能正常工作,没有任何反应。我假设即时通讯不检查输入是否正确1或2?谢谢

+0

你怎么看'输入= 1 | 2'在做什么,确切地说?它只是使默认值'3',它没有测试任何东西。 – jonrsharpe 2014-11-22 16:24:24

+0

我通常使用C++编程,所以python对我来说是陌生的,我认为它的意思是'or'in C++ ||是或 – user3423572 2014-11-22 16:34:42

+0

是的,这是一个按位或,但为什么不只是设置'input = 3'? – jonrsharpe 2014-11-22 16:35:45

回答

2

事件处理程序(或回调函数)将与一个事件对象调用,而不是一个整数。事件对象永远不会等于int对象。

如果检查特定的按键,使用char,事件的keysymkeycode属性传递:

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