Python 3 中 Tkinter 的鍵盤快捷鍵


Tkinter 視窗包含許多內建功能,這些功能可用於各種應用程式開發。在某些情況下,我們必須藉助一些鍵或函式來執行應用程式的特定部分。可以透過將特定鍵與包含操作函式的回撥函式繫結來實現。該鍵可以是滑鼠按鈕到鍵盤鍵的任意鍵。我們甚至可以將回調函式與鍵盤鍵組合繫結。

示例

#Import the Tkinter Library
from tkinter import *

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry of window
win.geometry("700x350")

#Define a callback function for exit
def quit_program(e):
   win.destroy()

#Add a Label widget
label = Label(win, text= "Press Ctrl + x to Exit", font= ('Helvetica 15 bold'))
label.pack(pady= 40)

#Bind the Keyboard shortcut Key
win.bind('<Control-x>', quit_program)
win.mainloop()

輸出

在以上程式碼中,我們添加了鍵組合。按這些鍵將會關閉視窗。

更新於:2021 年 5 月 26 日

4K+ 瀏覽量

開啟您的職業生涯

透過完成本課程獲得認證

開始
廣告
© . All rights reserved.