如何在 Tkinter 中繫結 shift+tab 組合鍵?
在需要執行特定任務或操作的任何應用程式中,Tkinter 事件都非常有用。在 Tkinter 中,通常透過定義一個包含程式碼片段和特定事件的邏輯的函式來建立事件。要呼叫事件,我們通常將事件與某些鍵或按鈕小部件繫結。bind 函式採用兩個引數 ('<Key-Combination>', callback) 使按鈕能夠觸發事件。
在以下示例中使用相同的方法,我們將透過按快捷鍵 <Shift + Tab> 觸發彈出訊息。
示例
# Import the required libraries from tkinter import * from tkinter import messagebox # Create an instance of tkinter frame win= Tk() # Set the size of the tkinter window win.geometry("700x350") # Define a function to show the popup message def show_msg(e): messagebox.showinfo("Message","Hey There! I hope you are doing well.") # Add an optional Label widget Label(win, text = "Admin Has Sent You a Message. " "Press <Shift+Tab> to View the Message.", font = ('Aerial 15')).pack(pady= 40) # Bind the Shift+Tab key with the event win.bind('<Shift-Tab>', lambda e: show_msg(e)) win.mainloop()
輸出
執行以上程式時,它將顯示一個包含一個標籤小部件的視窗。當我們按下按鍵組合 <Shift + Tab> 時,它將在螢幕上彈出訊息。
廣告