当前位置:   article > 正文

Tkinter 极简例子——Event篇

Tkinter 极简例子——Event篇

Tkinter Event

鼠标点击事件

from tkinter import *


def printcoords(event):
    print(event.x, event.y)


root = Tk()

bt1 = Button(root, text='leftmost button')
bt1.bind('<Button-1>', printcoords)

bt2 = Button(root, text='middle button')
bt2.bind('<Button-2>', printcoords)

bt3 = Button(root, text='rightmost button')
bt3.bind('<Button-3>', printcoords)

bt4 = Button(root, text='double click')
bt4.bind('<Button-4>', printcoords)

bt5 = Button(root, text='triple click')
bt5.bind('<Button-5>', printcoords)

bt1.pack()
bt2.pack()
bt3.pack()
bt4.pack()
bt5.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

鼠标移动事件

from tkinter import *


def printcoords(event):
    print(event.x, event.y)


root = Tk()

bt1 = Button(root, text='leftmost button')
bt1.bind('<B1-Motion>', printcoords)

bt2 = Button(root, text='middle button')
bt2.bind('<B2-Motion>', printcoords)

bt3 = Button(root, text='rightmost button')
bt3.bind('<B3-Motion>', printcoords)

bt1.pack()
bt2.pack()
bt3.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

鼠标释放事件

from tkinter import *


def printcoords(event):
    print(event.x, event.y)


root = Tk()

bt1 = Button(root, text='leftmost button')
bt1.bind('<ButtonRelease-1>', printcoords)

bt2 = Button(root, text='middle button')
bt2.bind('<ButtonRelease-2>', printcoords)

bt3 = Button(root, text='rightmost button')
bt3.bind('<ButtonRelease-3>', printcoords)

bt1.pack()
bt2.pack()
bt3.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

鼠标进入事件

from tkinter import *


def printcoords(event):
    print(event.x, event.y)


root = Tk()

bt1 = Button(root, text='leftmost button')
bt1.bind('<Enter>', printcoords)

bt1.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

鼠标离开事件

from tkinter import *


def printcoords(event):
    print(event.x, event.y)


root = Tk()

bt1 = Button(root, text='leftmost button')
bt1.bind('<Leave>', printcoords)

bt1.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

响应特殊键

from tkinter import *


def printcoords(event):
    print('event.char =', event.char)
    print('event.keycode=', event.keycode)


root = Tk()

bt1 = Button(root, text='press backspace')
bt1.bind('<BackSpace>', printcoords)

bt2 = Button(root, text='press enter')
bt2.bind('<Return>', printcoords)

bt3 = Button(root, text='F5')
bt3.bind('<F5>', printcoords)

bt1.pack()
bt2.pack()
bt3.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

响应所有按键

from tkinter import *


def printcoords(event):
    print('event.char =', event.char)
    print('event.keycode=', event.keycode)


root = Tk()

bt1 = Button(root, text='press backspace')
bt1.bind('<Key>', printcoords)

bt1.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

响应组合键

from tkinter import *


def printcoords(event):
    print('event.char =', event.char)
    print('event.keycode=', event.keycode)


root = Tk()

bt1 = Button(root, text='press shift-up')
bt1.bind('<Shift-Up>', printcoords)
bt2 = Button(root, text='press Control-Alt-a')
bt2.bind('<Control-Alt-a>', printcoords)

bt1.pack()
bt2.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

改变组件大小事件

from tkinter import *


def printsize(event):
    print(event.width, event.height)


root = Tk()

root.bind('<Configure>', printsize)

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

两个事件绑定同一个控件

from tkinter import *


def printevent(event):
    print('<Key>', event.keycode)


def printreturn(event):
    print('<Return', event.keycode)


root = Tk()

root.bind('<Key>', printevent)
root.bind('<Return>', printreturn)

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

为一个实例绑定一个事件

from tkinter import *


def printevent(event):
    print('<Key>', event.keycode)


root = Tk()

bt1 = Button(root, text='instance event')
bt1.bind('<Key>', printevent)
bt1.focus_set()
bt1.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

事件各个级别间传递

from tkinter import *


def printevent(event):
    print('<Key>', event.keycode)


def printToplevel(event):
    print('<toplevel>', event.keycode)


def printClass(event):
    print('<bind_class>', event.keycode)


def printAppAll(event):
    print('<bind_all>', event.keycode)


root = Tk()

bt1 = Button(root, text='instance event')
bt1.bind('<Key>', printevent)

bt1.winfo_toplevel().bind('<Return>', printToplevel)

root.bind_class('Button', '<Return>', printClass)

bt1.bind_all('<Return>', printAppAll)
bt1.focus_set()
bt1.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

bind_class

from tkinter import *


def printClass(event):
    print('<bind_class>', event.keycode)


root = Tk()

root.bind_class('Button', '<Return>', printClass)

bt1 = Button(root, text='a button')
bt2 = Button(root, text='another button')

bt1.focus_set()
bt1.pack()
bt2.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

使用protocol绑定

from tkinter import *


def printprotocol():
    print('WM_DELETE_WINDOW')
    root.destroy()


root = Tk()

root.protocol('WM_DELETE_WINDOW', printprotocol)

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/124954
推荐阅读
相关标签
  

闽ICP备14008679号