Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีแสดงสถานะ CAPS Lock Key ใน Tkinter

เราสามารถใช้การผูก (binding) <Lock-KeyPress> และ <Lock-KeyRelease> เพื่อตรวจสอบว่า CAPS Lock Key กำลัง ON หรือ OFF ตัวอย่างนี้สร้างฟังก์ชัน "caps_lock_on()" และ "caps_lock_off()" เพื่อจับเหตุการณ์และแสดงสถานะบนหน้าจอ

ตัวอย่าง

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

ผลลัพธ์

เมื่อผู้ใช้กด CAPS Lock จะแสดงสถานะปัจจุบันว่ากำลัง ON หรือ OFF:

วิธีแสดงสถานะ CAPS Lock Key ใน Tkinter

วิธีแสดงสถานะ CAPS Lock Key ใน Tkinter