-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
45 lines (35 loc) · 1.16 KB
/
main.py
File metadata and controls
45 lines (35 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tkinter as tk
# create the main window
root = tk.Tk()
root.title("Keyboard Tester")
root.geometry("600x400")
# Label to show key pressed
key_label = tk.Label(root, text="Press a key!", font=("Arial", 14))
key_label.pack(pady=20)
# Dictionary to store key buttons
key_buttons = {}
# Function to handle key press
def on_key_press(event):
key = event.keysym
key_label.config(text=f"key pressed: {key}")
if key in key_buttons:
key_buttons[key].config(bg="yellow")
# Function to handle key press
def on_key_release(event):
key = event.keysym
key_label.config(text=f"Key Pressed: {key}")
if key in key_buttons:
key_buttons[key].config(bg="SystemButtonFace")
# create a frame for the keyboard
keyboard_frame = tk.Frame(root)
keyboard_frame.pack(pady=20)
# Simple keyboard layout (A-Z)
for i, letter in enumerate("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
btn = tk.Button(keyboard_frame, text=letter, width=3, height=2)
btn.grid(row=i // 13, column=i % 13, padx=2, pady=2)
key_buttons[letter.lower()]= btn
# Bind Key events
root.bind("<KeyPress>", on_key_press)
root.bind("<KeyRelease>", on_key_release)
# start the app
root.mainloop()