tkinter学习-事件绑定与窗口

阅读目录:

  • 事件绑定

  • Toplevel组件
  • 标准对话框

事件绑定:

  说明:对于每个组件来说,可以通过bind()方法将函数或方法绑定到具体的事件上。

事件序列:

  说明:用户需要使用bind()方法将具体的事件序列与自定义的方法绑定,时间序列是以字符串的形式表示的。

  语法描述:

    <modifier - type - dateil>

    事件序列必须包含在尖括号(<...>)中

    type部分的内容是最重要的,它通常用来描述普通的数据类型,例如鼠标单击或键盘单击(<Button-1>,表示用户单击鼠标左键)

    modifier部分是可选的,它通常是用于描述组合键,例如Ctrl+c,(<Key-H>,表示用户按下H)

    dateil部分是可选的,它通常是描述具体的键,(<Control-Shift-Key-H>,表示用户同时按下Ctrl+Shift+H)

  type(常用的):

    Button:当用户点击鼠标按键的时候触发该事件,<Button-1>左键,<Button-2>中键,<Button-3>右键,<Button-4>滚轮上(liunx),<Button-5>滚轮下(liunx)  

    ButtonRelease:当用户释放鼠标按键的时候触发的事件

    KeyPress:当用户按下键盘的时候触发该事件,简写key

  modifier(常用的):

    Alt:当用户按下Alt按键的的时候

    Any:表示任何类型的按键被按下的时候,例如<Any-KeyPress>,表示当用户按下任何键的时候

    Control:表示按下Ctrl键的时候

    Double:当后续两个事件被连续触发的时候,例如<Double-Button-1>,表示双击鼠标左键的时候

    Lock:当打开大写字母的时候

    Shift:当按下Shift的时候

    Triple:当后续三个事件被触发的时候

Event对象:

  说明:当Tkinter去回调预定定义的函数的时候,将会带着Evnet对象去调用,常用的有:

  x,y 当前鼠标位置坐标(相对于窗口左上角)

  x_root,y_root 当前鼠标位置坐标(相对于屏幕左上角)

  char 按键对应的字符

  keysym 按键名

  keycode 按键码

获取鼠标的位置:

from tkinter import *
root = Tk()
frame = Frame(root,width=200,height=200)
def callback(event):
    print('当前位置为:',event.x,event.y)
frame.bind('<Button-1>',callback)
frame.pack()
mainloop()

结果:

    

  接受键盘事件,只有组件获取焦点才能接受键盘事件,用focus_set()获取焦点

from tkinter import *
root = Tk()
def callback(event):
    print('敲击位置:',repr(event.char))
frame = Frame(root,width=200,height=200)
frame.bind('<Key>',callback)
frame.focus_set()
frame.pack()
mainloop()

结果:

    

  获取鼠标的运动轨迹,用的是<Motion>

from tkinter import *
root = Tk()
def callback(event):
    print('当前位置:',event.x,event.y)
frame = Frame(root,width = 600,height=200)
frame.bind('<Motion>',callback)
frame.pack()
mainloop()

结果:

    

Toplevel:

  说明:顶级窗口,用于显示额外的窗口,其他弹出的窗口

from tkinter import *
root = Tk()
def show():
    top = Toplevel()
    top.title('山丘')
    top.geometry('200x200')
    Label(top,text='越过山丘').pack()
Button(root,text='创建顶级窗口',command=show).pack()
mainloop()

结果:

      

  这里还有一个attributes方法,用于设置窗口的属性

from tkinter import *
root = Tk()
def show():
    top = Toplevel()
    top.title('山丘')
    top.geometry('200x200')
    top.attributes('-alpha',0.5)  #设置窗口透明度为50%
    Label(top,text='越过山丘').pack()
Button(root,text='创建顶级窗口',command=show).pack()
mainloop()

结果:

    

标准对话框:

  说明:主要有三个模块,messagebox(消息对话框),filedialog(文件对话框),colorchooser(颜色对话框)

消息对话框:

  一共有七种样式,格式是:showwarning(title,message,options)

    title:设置标题栏文本

    message:设置文本内容

    options:设置选项的含义

  返回值:

    askokcancel(),askretrycancel(),askyesno()返回的是布尔类型的值

    askquestion()返回的是‘yes’或‘no’

    showerror(),showinfo(),showwarning()返回的是‘ok’表示用户单击了‘确定’

from tkinter import *
from tkinter import messagebox
root = Tk()
def show():
    #messagebox.askokcancel('山丘','askokcancel')
    #messagebox.askquestion('山丘','askquestion')
    #messagebox.askretrycancel('山丘','askretrycancel')
    #messagebox.askyesno('山丘','askyesno')
    #messagebox.showerror('山丘','showerror')
    #messagebox.showinfo('山丘','showinfo')
    messagebox.showwarning('山丘','showwarning')
Button(root,text='打开',command=show).pack()
mainloop()

结果:

              

文件对话框:

  两个函数 askopenfilename()打开文件,asksaveasfilename()保存文件

  常用的值:

    initialdir:指定打开或保存的路径

  返回值:

    如果用户选择了一个文件,那么返回的就是该文件的完整路径

    如果用户选择了取消按钮,那么返回的就是空的字符串   

from tkinter import *
from tkinter import filedialog
root = Tk()
def show():
    filedialog.askopenfilename(initialdir=r'路径名')
Button(root,text='打开文件',command=show).pack()
mainloop()

颜色对话框:

  返回值:

    如果用户选择了一个颜色并点击确定按钮,返回一个二元组,第一个元素是RGB颜色值,第二个是对应的元素的十六进制颜色值

    如果用户点击取消,返回的是(None,None)

from tkinter import *
from tkinter import colorchooser
root = Tk()
def show():
    colorchooser.askcolor()
Button(root,text='选择颜色',command=show).pack()
mainloop()

结果:

  

  最后,做一个注册窗口

from tkinter import *
from tkinter import messagebox
root = Tk()
def show():
    messagebox.askyesno('山丘','确认注册?')
def create():
    #top.attributes('-alpha',0.5)
    top = Toplevel(root)
    top.title('山丘')
    top.geometry('400x250')
    Label(top,text='用户名:').place(x=20,y=50)
    Label(top,text='密码:').place(x=20,y=120)
    Entry(top).place(x=110,y=50)
    Entry(top,show='*').place(x=110,y=120)
    Button(top,text='确认注册',width=10,command=show).place(x=170,y=190) 
root.title('山丘')
root.geometry('450x400')
Label(root,text='用户名:').place(x=100,y=170)
Label(root,text='密码:').place(x=100,y=230)
photo = PhotoImage(file='welcome.gif')
Label(root,image=photo).place(x=0,y=0)
Entry(root).place(x=190,y=170)
Entry(root,show='*').place(x=190,y=230)
Button(root,text='注册',width=10,command=create).place(x=100,y=300)  
Button(root,text='提交',width=10).place(x=300,y=300)
mainloop()

结果:

    

参考文献:

  小甲鱼python教学视频

原文地址:https://www.cnblogs.com/mengd/p/7305400.html