Python:GUI之tkinter學習筆記3事件繫結(轉載自https://www.cnblogs.com/progor/p/8505599.html) Python:GUI之tkinter學習筆記3事件繫結
阿新 • • 發佈:2019-01-05
Python:GUI之tkinter學習筆記3事件繫結
相關內容:
- command
- bind
- protocol
首發時間:2018-03-04 19:26
command:
- command是控制元件中的一個引數,如果使得command=函式,那麼點選控制元件的時候將會觸發函式
- 能夠定義command的常見控制元件有: Button、Menu…
- 呼叫函式時,預設是沒有引數傳入的,如果要強制傳入引數,可以考慮使用lambda
from tkinter import *
root=Tk()
def prt():
print("hello")
def func1(*args,**kwargs):
print(*args,**kwargs)
hello_btn=Button(root,text="hello",command=prt)#演示
hello_btn.pack()
args_btn=Button(root,text="獲知是否button事件預設有引數",command=func1)#獲知是否有引數,結果是沒有
args_btn.pack()
btn1=Button(root,text="傳輸引數",command=lambda:func1("running"))#強制傳輸引數
btn1.pack()
root.mainloop()
bind:
- bind的用法:控制元件.bind(event, handler),其中event是tkinter已經定義好的的事件,handler是處理器,可以是一個處理函式,如果相關事件發生, handler 函式會被觸發, 事件物件 event 會傳遞給 handler 函式
- 基本所有控制元件都能bind
- 常見event有:
- 滑鼠單擊事件:滑鼠左鍵點選為 <Button-1>, 滑鼠中鍵點選為 <Button-2>, 滑鼠右鍵點選為 <Button-3>, 向上滾動滑輪為 <Button-4>, 向下滾動滑輪為 <Button-5>.
- 滑鼠雙擊事件.:滑鼠左鍵點選為 <Double-Button-1>, 滑鼠中鍵點選為 <Double-Button-2>, 滑鼠右鍵點選為 <Double-Button-3>.
- 滑鼠釋放事件:滑鼠左鍵點選為 <ButtonRelease-1>, 滑鼠中鍵點選為 <ButtonRelease-2>, 滑鼠右鍵點選為 <ButtonRelease-3>. 滑鼠相對當前控制元件的位置會被儲存在 event 物件中的 x 和 y 欄位中傳遞給回撥函式.
- 滑鼠移入控制元件事件:<Enter>
- 獲得焦點事件:<FocusIn>
- 滑鼠移出控制元件事件: <Leave>
- 失去焦點事件:<FocusOut>
- 滑鼠按下移動事件:滑鼠左鍵點選為 <B1-Motion>, 滑鼠中鍵點選為 <B2-Motion>, 滑鼠右鍵點選為 <B3-Motion>. 滑鼠相對當前控制元件的位置會被儲存在 event 物件中的 x 和 y 欄位中傳遞給回撥函式.
- 鍵盤按下事件:<Key>,event中的keysym ,keycode,char都可以獲取按下的鍵【其他想要獲取值的也可以先看看event中有什麼】
- 鍵位繫結事件:<Return>回車鍵,<BackSpace>,<Escape>,<Left>,<Up>,<Right>,<Down>…….
- 控制元件大小改變事件:<Configure>,新的控制元件大小會儲存在 event 物件中的 width 和 height 屬性傳遞. 有些平臺上該事件也可能代表控制元件位置改變.
- Event中的屬性:
- widget:產生事件的控制元件
- x, y:當前滑鼠的位置
- x_root, y_root:當前滑鼠相對於螢幕左上角的位置,以畫素為單位。
- char:字元程式碼(僅限鍵盤事件),作為字串。
- keysym:關鍵符號(僅限鍵盤事件)。
- keycode:關鍵程式碼(僅限鍵盤事件)。
- num:按鈕號碼(僅限滑鼠按鈕事件)。
- width, height:小部件的新大小(以畫素為單位)(僅限配置事件)。
- type:事件型別。
from tkinter import *
root=Tk()
root.geometry("200x200")
text=Text(root)
text.pack()
def func(event):
print(event)
def func_release(event):
print("release")
#單擊
# text.bind("<Button-1>",func)
# root.bind("<Button-1>",func)
#雙擊
# text.bind("<Double-Button-1>",func)
# 滑鼠釋放
# text.bind("<ButtonRelease-1>",func_release)
#滑鼠移入
# text.bind("<Enter>",func)
#滑鼠按住移動事件
# text.bind("<B1-Motion>",func)
#鍵盤按下事件
# text.bind("<Key>",func)
#鍵位繫結事件
# def func3(event):
# print("你按下了回車!")
# text.bind("<Return>",func3)
#實現的一個拖拽功能
def func4(event):
# print(event)
x=str(event.x_root)
y=str(event.y_root)
root.geometry("200x200+"+x+"+"+y)
text.bind("<B1-Motion>",func4)
root.mainloop()
補充:如果想要傳參,可以使用lambda:
text.bind("<Button-1>",lambda event:func(event,"hello"))
protocol:
- protocol的使用:控制元件.protocol(protocol,handler),其中控制元件為視窗物件(Tk,Toplevel)
- 常見protocol有:
- WM_DELETE_WINDOW:最常用的協議稱為WM_DELETE_WINDOW,用於定義使用者使用視窗管理器明確關閉視窗時發生的情況。如果使用自己的handler來處理事件的話,這時候視窗將不會自動執行關閉
- WM_TAKE_FOCUS,WM_SAVE_YOURSELF:[這兩個不知道什麼來的。]
- 更多需參考ICCCM文件
- 注意:要留心協議的寫法,在作為字串填入時不要加多餘的空格
from tkinter import *
import tkinter.messagebox
root=Tk()
root.geometry("200x200")
def func1():
if tkinter.messagebox.askyesno("關閉視窗","確認關閉視窗嗎"):
root.destroy()
root.protocol("WM_DELETE_WINDOW",func1)
root.mainloop()
相關內容:
- command
- bind
- protocol
首發時間:2018-03-04 19:26
command:
- command是控制元件中的一個引數,如果使得command=函式,那麼點選控制元件的時候將會觸發函式
- 能夠定義command的常見控制元件有: Button、Menu…
- 呼叫函式時,預設是沒有引數傳入的,如果要強制傳入引數,可以考慮使用lambda
from tkinter import *
root=Tk()
def prt():
print("hello")
def func1(*args,**kwargs):
print(*args,**kwargs)
hello_btn=Button(root,text="hello",command=prt)#演示
hello_btn.pack()
args_btn=Button(root,text="獲知是否button事件預設有引數",command=func1)#獲知是否有引數,結果是沒有
args_btn.pack()
btn1=Button(root,text="傳輸引數",command=lambda:func1("running"))#強制傳輸引數
btn1.pack()
root.mainloop()
bind:
- bind的用法:控制元件.bind(event, handler),其中event是tkinter已經定義好的的事件,handler是處理器,可以是一個處理函式,如果相關事件發生, handler 函式會被觸發, 事件物件 event 會傳遞給 handler 函式
- 基本所有控制元件都能bind
- 常見event有:
- 滑鼠單擊事件:滑鼠左鍵點選為 <Button-1>, 滑鼠中鍵點選為 <Button-2>, 滑鼠右鍵點選為 <Button-3>, 向上滾動滑輪為 <Button-4>, 向下滾動滑輪為 <Button-5>.
- 滑鼠雙擊事件.:滑鼠左鍵點選為 <Double-Button-1>, 滑鼠中鍵點選為 <Double-Button-2>, 滑鼠右鍵點選為 <Double-Button-3>.
- 滑鼠釋放事件:滑鼠左鍵點選為 <ButtonRelease-1>, 滑鼠中鍵點選為 <ButtonRelease-2>, 滑鼠右鍵點選為 <ButtonRelease-3>. 滑鼠相對當前控制元件的位置會被儲存在 event 物件中的 x 和 y 欄位中傳遞給回撥函式.
- 滑鼠移入控制元件事件:<Enter>
- 獲得焦點事件:<FocusIn>
- 滑鼠移出控制元件事件: <Leave>
- 失去焦點事件:<FocusOut>
- 滑鼠按下移動事件:滑鼠左鍵點選為 <B1-Motion>, 滑鼠中鍵點選為 <B2-Motion>, 滑鼠右鍵點選為 <B3-Motion>. 滑鼠相對當前控制元件的位置會被儲存在 event 物件中的 x 和 y 欄位中傳遞給回撥函式.
- 鍵盤按下事件:<Key>,event中的keysym ,keycode,char都可以獲取按下的鍵【其他想要獲取值的也可以先看看event中有什麼】
- 鍵位繫結事件:<Return>回車鍵,<BackSpace>,<Escape>,<Left>,<Up>,<Right>,<Down>…….
- 控制元件大小改變事件:<Configure>,新的控制元件大小會儲存在 event 物件中的 width 和 height 屬性傳遞. 有些平臺上該事件也可能代表控制元件位置改變.
- Event中的屬性:
- widget:產生事件的控制元件
- x, y:當前滑鼠的位置
- x_root, y_root:當前滑鼠相對於螢幕左上角的位置,以畫素為單位。
- char:字元程式碼(僅限鍵盤事件),作為字串。
- keysym:關鍵符號(僅限鍵盤事件)。
- keycode:關鍵程式碼(僅限鍵盤事件)。
- num:按鈕號碼(僅限滑鼠按鈕事件)。
- width, height:小部件的新大小(以畫素為單位)(僅限配置事件)。
- type:事件型別。
from tkinter import *
root=Tk()
root.geometry("200x200")
text=Text(root)
text.pack()
def func(event):
print(event)
def func_release(event):
print("release")
#單擊
# text.bind("<Button-1>",func)
# root.bind("<Button-1>",func)
#雙擊
# text.bind("<Double-Button-1>",func)
# 滑鼠釋放
# text.bind("<ButtonRelease-1>",func_release)
#滑鼠移入
# text.bind("<Enter>",func)
#滑鼠按住移動事件
# text.bind("<B1-Motion>",func)
#鍵盤按下事件
# text.bind("<Key>",func)
#鍵位繫結事件
# def func3(event):
# print("你按下了回車!")
# text.bind("<Return>",func3)
#實現的一個拖拽功能
def func4(event):
# print(event)
x=str(event.x_root)
y=str(event.y_root)
root.geometry("200x200+"+x+"+"+y)
text.bind("<B1-Motion>",func4)
root.mainloop()
補充:如果想要傳參,可以使用lambda:
text.bind("<Button-1>",lambda event:func(event,"hello"))
protocol:
- protocol的使用:控制元件.protocol(protocol,handler),其中控制元件為視窗物件(Tk,Toplevel)
- 常見protocol有:
- WM_DELETE_WINDOW:最常用的協議稱為WM_DELETE_WINDOW,用於定義使用者使用視窗管理器明確關閉視窗時發生的情況。如果使用自己的handler來處理事件的話,這時候視窗將不會自動執行關閉
- WM_TAKE_FOCUS,WM_SAVE_YOURSELF:[這兩個不知道什麼來的。]
- 更多需參考ICCCM文件
- 注意:要留心協議的寫法,在作為字串填入時不要加多餘的空格
from tkinter import *
import tkinter.messagebox
root=Tk()
root.geometry("200x200")
def func1():
if tkinter.messagebox.askyesno("關閉視窗","確認關閉視窗嗎"):
root.destroy()
root.protocol("WM_DELETE_WINDOW",func1)
root.mainloop()

