问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
# coding=utf-8
# 测试键盘和鼠标事件
from tkinter import *
root=Tk();root.geometry('500x300')
c1=Canvas(root,width=200,height=200,bg='green')
c1.pack()
def mouseTest (event):
print('鼠标左键单击的位置(相对于父容器):{0},{1}'.format(event.x,event.y))
print('鼠标左键单击的位置(相对于屏幕):{0},{1}'.format(event.x_root,event.y_root))
print('事件绑定的组件:{0}'.format(event.widget))
def testDrag(event):
c1.create_oval(event.x,event.y,event.x+1,event.y+1)
def keyboardTest(event):
print('键的keycode:{0},键的插入:{1},键的keysym:{2}'
.format(event.keycode,event.char,event.keysym))
def press_a_test():
print('press a')
def release_a_test():
print('release a')
c1.bind('<Button-1>',mouseTest)
c1.bind('<B1-Motion>',testDrag)
root.bind('<KeyPress>',keyboardTest)
root.bind('<KeyPress-a>',press_a_test)
root.bind('<keyRelease-a>',release_a_test)
root.mainloop()
运行结果及报错内容
"C:\Program Files\python\python.exe" D:/pythonProject/gui/my14.py
Traceback (most recent call last):
File "D:\pythonProject\gui\my14.py", line 36, in <module>
root.bind('<keyRelease-a>',release_a_test)
File "C:\Program Files\python\lib\tkinter\__init__.py", line 1421, in bind
return self._bind(('bind', self._w), sequence, func, add)
File "C:\Program Files\python\lib\tkinter\__init__.py", line 1375, in _bind
self.tk.call(what + (sequence, cmd))
_tkinter.TclError: bad event type or keysym "keyRelease"
Process finished with exit code 1
我的解答思路和尝试过的方法
我想要达到的结果