1. 程式人生 > >Python:GUI之tkinter學習筆記3事件綁定(轉載自https://www.cnblogs.com/progor/p/8505599.html)

Python:GUI之tkinter學習筆記3事件綁定(轉載自https://www.cnblogs.com/progor/p/8505599.html)

borde proto mes level 字符串 from .com 當前 控件

相關內容:

  • 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()

Python:GUI之tkinter學習筆記3事件綁定(轉載自https://www.cnblogs.com/progor/p/8505599.html)