Python| Tkinter 中的绑定函数
Tkinter 是一个广泛用于桌面应用程序的 GUI(图形用户界面)模块。它与Python一起提供,但您也可以借助pip命令在外部安装它。
它提供了各种 Widget 类和功能,借助这些类和功能,可以使我们的 GUI 在外观和功能方面更具吸引力和用户友好性。
绑定函数用于处理事件。我们可以将Python 的函数和方法绑定到一个事件,也可以将这些函数绑定到任何特定的小部件。
代码 #1:使用 tkinter Frame 绑定鼠标移动。
Python3
# Import all files from
# tkinter and overwrite
# all the tkinter files
# by tkinter.ttk
from tkinter import *
from tkinter.ttk import *
# creates tkinter window or root window
root = Tk()
root.geometry('200x100')
# function to be called when mouse enters in a frame
def enter(event):
print('Button-2 pressed at x = % d, y = % d'%(event.x, event.y))
# function to be called when when mouse exits the frame
def exit_(event):
print('Button-3 pressed at x = % d, y = % d'%(event.x, event.y))
# frame with fixed geometry
frame1 = Frame(root, height = 100, width = 200)
# these lines are showing the
# working of bind function
# it is universal widget method
frame1.bind('', enter)
frame1.bind('', exit_)
frame1.pack()
mainloop()
Python3
# Import all files from
# tkinter and overwrite
# all the tkinter files
# by tkinter.ttk
from tkinter import *
from tkinter.ttk import *
# creates tkinter window or root window
root = Tk()
root.geometry('200x100')
# function to be called when button-2 of mouse is pressed
def pressed2(event):
print('Button-2 pressed at x = % d, y = % d'%(event.x, event.y))
# function to be called when button-3 of mouse is pressed
def pressed3(event):
print('Button-3 pressed at x = % d, y = % d'%(event.x, event.y))
## function to be called when button-1 is double clocked
def double_click(event):
print('Double clicked at x = % d, y = % d'%(event.x, event.y))
frame1 = Frame(root, height = 100, width = 200)
# these lines are binding mouse
# buttons with the Frame widget
frame1.bind('', pressed2)
frame1.bind('', pressed3)
frame1.bind('', double_click)
frame1.pack()
mainloop()
Python3
# Import all files from
# tkinter and overwrite
# all the tkinter files
# by tkinter.ttk
from tkinter import *
from tkinter.ttk import *
# function to be called when
# keyboard buttons are pressed
def key_press(event):
key = event.char
print(key, 'is pressed')
# creates tkinter window or root window
root = Tk()
root.geometry('200x100')
# here we are binding keyboard
# with the main window
root.bind('', key_press)
mainloop()
输出:
代码 #2:用 Tkinter 框架绑定鼠标按钮
Python3
# Import all files from
# tkinter and overwrite
# all the tkinter files
# by tkinter.ttk
from tkinter import *
from tkinter.ttk import *
# creates tkinter window or root window
root = Tk()
root.geometry('200x100')
# function to be called when button-2 of mouse is pressed
def pressed2(event):
print('Button-2 pressed at x = % d, y = % d'%(event.x, event.y))
# function to be called when button-3 of mouse is pressed
def pressed3(event):
print('Button-3 pressed at x = % d, y = % d'%(event.x, event.y))
## function to be called when button-1 is double clocked
def double_click(event):
print('Double clicked at x = % d, y = % d'%(event.x, event.y))
frame1 = Frame(root, height = 100, width = 200)
# these lines are binding mouse
# buttons with the Frame widget
frame1.bind('', pressed2)
frame1.bind('', pressed3)
frame1.bind('', double_click)
frame1.pack()
mainloop()
输出:
代码 #3:将键盘按钮与根窗口(tkinter 主窗口)绑定。
Python3
# Import all files from
# tkinter and overwrite
# all the tkinter files
# by tkinter.ttk
from tkinter import *
from tkinter.ttk import *
# function to be called when
# keyboard buttons are pressed
def key_press(event):
key = event.char
print(key, 'is pressed')
# creates tkinter window or root window
root = Tk()
root.geometry('200x100')
# here we are binding keyboard
# with the main window
root.bind('', key_press)
mainloop()
输出:
注意:当我们将键盘按钮与 tkinter 窗口绑定时,每当我们按下特殊字符时,我们只会获得空格,而在字母和数字的情况下,我们将获得实际值(在字符串中)。