Найти - Пользователи
Полная версия: Не работают события клавиатуры
Начало » GUI » Не работают события клавиатуры
1
Anton11
События мыши работают, а клавы - нет :/ Работает всё, связанное с мышью (все три клика, зажать и потянуть и 2лкм), а Ентер, ескейп и другие клавишы не работают. Кто-нибудь подскажет почему так? (Python 3.1 и 2.7)
from tkinter import *
def showPosEvent(event):
    print('Widget=%s X=%s Y=%s' % (event.widget,event.x, event.y))
    
def showAllEvent(event):
    print(event)
    for attr in dir(event):
        if not attr.startswith('__'):
            print(attr,'==>',getattr(event,attr))
def onKeyPress(event):
    print('Got key press:',event.char)
def onArrowKey(event):
    print('Got up arrow key')
def onReturnKey(event):
    print('Got return key press')
def onLeftClick(event):
    print('Got left mouse button click:', end='')
    showPosEvent(event)
def onRightClick(event):
    print('Got right mouse button click:', end='')
    showPosEvent(event)
def onMiddleClick(event):
    print('Got middle mouse button click:', end='')
    showPosEvent(event)
    showAllEvent(event)
def onLeftDrug(event):
    print('Got left mouse button drug:', end='')
    showPosEvent(event)
def onDoubleLeftClick(event):
    print('Got double left click', end='')
    showPosEvent(event)
    tkroot.quit()
    
tkroot = Tk()
labelfont = ('courier', 20, 'bold')
widget = Label(tkroot,text='Hello bind world')
widget.config(bg='red',font=labelfont)
widget.config(height=5,width=20)
widget.pack(expand=YES,fill=BOTH)
widget.bind('<Button-1>',onLeftClick)
widget.bind('<Button-3>',onRightClick)
widget.bind('<Button-2>',onMiddleClick)
widget.bind('<Double-1>',onDoubleLeftClick)
widget.bind('<B1-Motion>',onLeftDrug)
widget.bind('<KeyPress>',onKeyPress)
widget.bind('<Up>',onArrowKey)
widget.bind('<Return>',onReturnKey)
tkroot.title('Click Me')
tkroot.mainloop()
sp3
Не на все виджеты можно забиндить события.
В вашем случае можно сделать
tkroot.bind('<KeyPress>',onKeyPress)
или
widget.bind_all('<KeyPress>',onKeyPress)
Anton11
sp3, благодарю, всё работает! :)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB