import sys
import random
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QVBoxLayout,
QWidget, QLabel, QLineEdit, QHBoxLayout)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
Hiragana-characters to readings mapping
hiragana = {
"あ": "a", "い": "i", "う": "u", "え": "e", "お": "o",
"か": "ka", "き": "ki", "く": "ku", "け": "ke", "こ": "ko",
"さ": "sa", "し": "shi", "す": "su", "せ": "se", "そ": "so",
"た": "ta", "ち": "chi", "つ": "tsu", "て": "te", "と": "to",
"な": "na", "に": "ni", "ぬ": "nu", "ね": "ne", "の": "no",
"は": "ha", "ひ": "hi", "ふ": "fu", "へ": "he", "ほ": "ho",
"ま": "ma", "み": "mi", "む": "mu", "め": "me", "も": "mo",
"や": "ya", "ゆ": "yu", "よ": "yo",
"ら": "ra", "り": "ri", "る": "ru", "れ": "re", "ろ": "ro",
"わ": "wa", "を": "wo", "ん": "n"
# ...
# Add all hiragana characters and their readings here
}
def generate_hiragana():
character = random.choice(list(hiragana.keys()))
reading = hiragana[character]
return character, reading
Function to update the score
def update_score(is_correct, score_label):
score = int(score_label.text().split(': ')[1])
if is_correct:
score += 1
else:
score -= 1
score_label.setText(f"Score: {score}")
save_score(score)
Function to create buttons for mode 1
def create_buttons(layout, character, reading, feedback_label, score_label):
# Get 3 random choices that are not the correct reading
choices = [hiragana[char] for char in hiragana if hiragana[char] != reading]
random.shuffle(choices)
choices = choices[:3]
choices.append(reading)
random.shuffle(choices)
def button_callback(button_reading):
if button_reading == reading:
update_score(True, score_label)
feedback_label.setText("Correct!")
else:
update_score(False, score_label)
feedback_label.setText(f"Wrong! Correct reading is: {reading}")
new_character, new_reading = generate_hiragana()
character_label.setText(new_character)
feedback_label.setText("")
create_buttons(layout, new_character, new_reading, feedback_label, score_label)
# Clear existing buttons before creating new ones
while layout.count():
button = layout.takeAt(0).widget()
if button:
button.deleteLater()
# Create new buttons
for choice in choices:
button = QPushButton(choice)
button.clicked.connect(lambda ch, r=choice: button_callback(r))
layout.addWidget(button)
def save_score(score):
with open('score.txt', 'w') as file:
file.write(str(score))
def load_score():
try:
with open('score.txt', 'r') as file:
score = int(file.read())
except FileNotFoundError:
score = 0
except ValueError:
score = 0
return score
Main Application Window
class HiraganaApp(QMainWindow):
def init(self):
super().init()
self.char_label = QLabel('')
self.char_label.setAlignment(Qt.AlignCenter)
self.char_label.setFont(QFont('Arial', 50))
self.setWindowTitle('Hiragana Practice')
# Load the score when the application starts
self.score = load_score()
# Central widget
self.central_widget = QWidget()
self.general_layout = QVBoxLayout()
self.central_widget.setLayout(self.general_layout)
self.setCentralWidget(self.central_widget)
# Score label
self.score_label = QLabel(f"Score: {self.score}")
self.general_layout.addWidget(self.score_label)
# All other
character, reading = generate_hiragana()
# Mode 1 Interface
self.char_label = QLabel()
self.char_label.setAlignment(Qt.AlignCenter)
self.char_label.setFont(QFont('Arial', 50))
self.char_label.setText(character)
self.general_layout.addWidget(self.char_label)
# Define and set up your feedback label here
feedback_label = QLabel('')
feedback_label.setAlignment(Qt.AlignCenter)
self.general_layout.addWidget(feedback_label)
self.feedback_label = QLabel("")
self.feedback_label.setAlignment(Qt.AlignCenter)
self.general_layout.addLayout(button_layout)
button_layout = QHBoxLayout()
self.char_label.setText(character)
create_buttons(button_layout, character, reading, feedback_label, self.score_label)
# Initialize buttons
create_buttons(button_layout, character, reading, feedback_label, self.score_label)
self.general_layout.addLayout(button_layout)
self.general_layout.addWidget(self.character_label)
self.general_layout.addWidget(self.feedback_label)
# Mode 2 Interface (Type Mode)
# You can add a QLineEdit and connect its 'returnPressed' signal to a function that checks the input
# against the correct reading, updates the feedback label, and updates the character label.
def setup_mode_2(layout, score_label, character_label, feedback_label):
character, reading = generate_hiragana()
character_label.setText(character)
def check_typing():
typed_reading = typing_input.text()
if typed_reading == reading:
update_score(True, score_label)
feedback_label.setText("Correct!")
feedback_label.setStyleSheet("color: green;")
else:
update_score(False, score_label)
feedback_label.setText(f"Wrong! Correct reading is: {reading}")
feedback_label.setStyleSheet("color: red;")
# Prepare next character
new_character, new_reading = generate_hiragana()
character_label.setText(new_character)
typing_input.clear()
# Clear any existing widgets first
while layout.count():
widget = layout.takeAt(0).widget()
if widget:
widget.deleteLater()
# Create text input for typing mode
typing_input = QLineEdit()
typing_input.returnPressed.connect(check_typing)
layout.addWidget(typing_input)
typing_input.setFocus() # Set focus to the typing input
def switch_mode(self, mode):
# Clear existing widgets in central layout before switching mode
for i in reversed(range(self.general_layout.count())):
widget_to_remove = self.general_layout.itemAt(i).widget()
# remove it from the layout list
self.general_layout.removeWidget(widget_to_remove)
# remove it from the gui
widget_to_remove.setParent(None)
# Set up the chosen mode
if mode == 1:
self.setup_mode_1()
elif mode == 2:
self.setup_mode_2()
def setup_mode_1(self):
pass
# Add button_mode_1 and its functionality
# ...
def setup_mode_2(self):
button_layout = QHBoxLayout()
feedback_label = QLabel("")
feedback_label.setAlignment(Qt.AlignCenter)
self.general_layout.addWidget(feedback_label)
character_label = QLabel("")
character_label.setAlignment(Qt.AlignCenter)
character_label.setFont(QFont('Arial', 50))
self.general_layout.addWidget(character_label)
self.general_layout.addLayout(button_layout)
setup_mode_2(button_layout, self.score_label, character_label, feedback_label) # Use the set up function for mode 2
def main():
app = QApplication(sys.argv)
view = HiraganaApp()
view.show()
sys.exit(app.exec_())
if name == 'main':
main()
Fis this, it's not working