Computer >> Máy Tính >  >> Lập trình >> Python

Làm thế nào để hiển thị trạng thái của Khóa CAPS trong tkinter?

Chúng tôi có thể sử dụng liên kết để kiểm tra xem Phím Khóa CAPS đang BẬT hay tắt. Trong ví dụ sau, chúng tôi sẽ tạo hai hàm do người dùng xác định "caps_lock_on ()" "caps_lock_off ()" sẽ nắm bắt sự kiện Lock-KeyPress và Lock-KeyRelease và in trạng thái trên màn hình.

Ví dụ

# Import required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win = Tk()

# Define the geometry of the window
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("<Lock-KeyPress>", caps_lock_on)
win.bind("<Lock-KeyRelease>", caps_lock_off)

win.mainloop()

Đầu ra

Khi người dùng nhấn phím CAPS Lock, nó sẽ hiển thị trạng thái hiện tại, cho dù nó đang BẬT hay TẮT.

Làm thế nào để hiển thị trạng thái của Khóa CAPS trong tkinter?

Làm thế nào để hiển thị trạng thái của Khóa CAPS trong tkinter?