Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách hiển thị trạng thái phím CAPS Lock trong Tkinter

Dùng binding <Lock-KeyPress> và <Lock-KeyRelease> để kiểm tra CAPS Lock ON/OFF. Tạo 2 hàm caps_lock_on() và caps_lock_off() bắt sự kiện và cập nhật label.

Ví dụ

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x250")
win.title("CAPS Lock Status")

def caps_lock_on(e):
    label_caps.config(text="CAPS Lock is ON")

def caps_lock_off(e):
    label_caps.config(text="CAPS Lock is OFF")

label_caps = Label(win, font="Helvetica 15 bold")
label_caps.pack(pady=20)

win.bind("", caps_lock_on)
win.bind("", caps_lock_off)

win.mainloop()

Kết quả

Khi nhấn CAPS Lock, label hiển thị trạng thái ON/OFF.

Cách hiển thị trạng thái phím CAPS Lock trong Tkinter

Cách hiển thị trạng thái phím CAPS Lock trong Tkinter