老师我的运行结果按键盘的时候,不打印键的char, 键的keysym也不打印, 按a键的时候不打印press a 也没报错,麻烦老师看看是怎么回事,感谢。
from tkinter import *
root = Tk()
root.geometry('600x300')
c1 = Canvas(root,width=400,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+10,event.y+10) #在鼠标所在的位置画圈 校园前
def keyboardTest(event):
print('键的keycode:{0},键的char:{1},键的keysym:{2}'
.format(event.keycode,event.char,event.keysym))
def press_a_test(event):
print('press a')
def release_a_test(event):
print('release a')
c1.bind('<1>',mouseTest) #<1>左键单击
c1.bind('<B1-Motion>',testDrag) #<B1-Motion>拖动鼠标
root.bind('<KeyPress>',keyboardTest) #绑定键盘
root.bind('<KeyPress-a>',press_a_test) # 按下a键 只是小写
root.bind('<KeyRelease-a>',release_a_test) #释放a键
root.mainloop()
