Repository navigation
Expand file tree
/
Copy pathhelloworld tkinter.py
More file actions
153 lines (110 loc) · 4.22 KB
/
Copy pathhelloworld tkinter.py
File metadata and controls
153 lines (110 loc) · 4.22 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import tkinter as tk
import time
import random
import re
# 🔹 Wörter laden
def load_words(filename, amount):
words = []
with open(filename, "r", encoding="utf-8") as f:
lines = f.readlines()
random.shuffle(lines)
for line in lines:
line = re.sub(r"\(.*?\)", "", line)
parts = [w.strip() for w in line.split(";") if w.strip()]
if parts:
words.append(random.choice(parts))
if len(words) >= amount:
break
return words
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Typing Game")
self.show_menu()
# 🔹 MENU
def show_menu(self):
self.clear()
tk.Label(self, text="Typing Game", font=("Arial", 20)).pack(pady=10)
# ✅ StringVars (wichtig!)
self.word_var = tk.StringVar(value="10")
self.time_var = tk.StringVar()
self.error_var = tk.StringVar()
tk.Label(self, text="Anzahl Wörter:").pack()
tk.Entry(self, textvariable=self.word_var).pack()
tk.Label(self, text="Max Zeit (Sek, optional):").pack()
tk.Entry(self, textvariable=self.time_var).pack()
tk.Label(self, text="Max Fehler (optional):").pack()
tk.Entry(self, textvariable=self.error_var).pack()
tk.Button(self, text="Start", command=self.start_game).pack(pady=10)
# 🔹 GAME START
def start_game(self):
# ✅ Werte holen (ohne Widget-Abhängigkeit)
self.word_count = int(self.word_var.get())
self.max_time = self.time_var.get()
self.max_errors = self.error_var.get()
self.max_time = float(self.max_time) if self.max_time else None
self.max_errors = int(self.max_errors) if self.max_errors else None
self.clear()
self.words = load_words("openthesaurus.txt", self.word_count)
self.index = 0
self.correct = 0
self.errors = 0
self.start_time = time.time()
# UI
self.label = tk.Label(self, text="", font=("Arial", 20))
self.label.pack()
self.info = tk.Label(self, text="")
self.info.pack()
self.entry = tk.Entry(self)
self.entry.pack()
self.entry.focus_set()
# ⚡ ohne Enter
self.entry.bind("<KeyRelease>", self.check_word)
self.next_word()
# 🔹 NEXT WORD
def next_word(self):
if self.index < len(self.words):
self.current_word = self.words[self.index]
self.label.config(text=self.current_word)
self.entry.delete(0, tk.END)
else:
self.finish()
# 🔹 CHECK
def check_word(self, event):
typed = self.entry.get().strip()
# 🔥 Live Fehleranzeige
if self.current_word.startswith(typed):
self.entry.config(fg="black")
else:
self.entry.config(fg="red")
# ✅ Nur bei komplett richtig
if typed == self.current_word:
self.correct += 1
self.info.config(text="✅ Richtig")
self.index += 1
# Abbruchbedingungen
if self.max_errors and self.errors >= self.max_errors:
self.finish()
return
if self.max_time and (time.time() - self.start_time) >= self.max_time:
self.finish()
return
self.next_word()
# 🔹 FINISH
def finish(self):
self.clear()
total_time = time.time() - self.start_time
wpm = self.correct / total_time * 60 if total_time > 0 else 0
tk.Label(self, text="Fertig!", font=("Arial", 20)).pack(pady=10)
tk.Label(self, text=f"Richtig: {self.correct}").pack()
tk.Label(self, text=f"Fehler: {self.errors}").pack()
tk.Label(self, text=f"Zeit: {round(total_time,2)}s").pack()
tk.Label(self, text=f"WPM: {round(wpm,2)}").pack()
tk.Button(self, text="Zurück zum Menü", command=self.show_menu).pack(pady=10)
# 🔹 CLEAR
def clear(self):
for widget in self.winfo_children():
widget.destroy()
# 🔹 START
app = App()
app.mainloop()