閱讀目錄:
-
事件綁定
- 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教學視頻