Привет, у меня проблема с tkinter в Python, все еще новичок в программировании. У меня есть поле ввода, которое вводит пользователь, и оно проверяет, соответствует ли ввод словарю. Он отлично работает в первый раз, но после этого переменная enty сохраняется как ничто. Я пытался использовать .delete () и .insert (), но это возникает как ошибка атрибута. Какие-либо предложения?
import tkinter as tk
import random
hirigana = {"あ": "a", "い": "i", "う": "u", "え": "e", "お": "o"}
root = tk.Tk()
root.title("App name")
root.geometry("250x340")
def open_test():
test = tk.Toplevel()
test.title("TestLevel")
test.geometry("240x580")
test_frame = tk.Frame(test, width=300, height=600, )
test_frame.pack()
test_canvas = tk.Canvas(test_frame, bd=4, bg="gray", width=300, height=600)
test_canvas.pack()
test_button_frame = tk.Frame(test_canvas)
test_button_frame.grid()
hirigana_button = tk.Button(test_button_frame, text="Test Hirigana", width=25, height=4,
command=open_test_hirigana)
hirigana_button.grid(column=5, row=0, sticky='', padx=20, pady=20)
return_button = tk.Button(test_button_frame, text="Return", width=25, height=4, command=test.destroy)
return_button.grid(column=5, row=4, sticky='', padx=20, pady=20)
def update_hirigana_character():
global hirigana_japanese_character
global hirigana_english_character
correct_answer.set("")
hirigana_random_key = str(random.choice(list(hirigana.keys())))
hirigana_keys_answer = str(hirigana.get(hirigana_random_key))
hirigana_japanese_character.set(hirigana_random_key)
hirigana_english_character.set(hirigana_keys_answer)
# study variables
hirigana_japanese_character = tk.StringVar()
hirigana_english_character = tk.StringVar()
user_hirigana_answer = tk.StringVar()
correct_answer = tk.StringVar()
def check_hirigana_answer():
global hirigana_japanese_character
global hirigana_english_character
global user_hirigana_answer
answer = user_hirigana_answer.get()
print("player guess = {}, answer = {}".format(answer, hirigana_english_character.get()))
if answer == hirigana_english_character.get():
user_hirigana_answer = tk.StringVar()
correct_answer.set("Correct")
print("Correct")
else:
print("wrong")
correct_answer.set("Incorrect")
def open_test_hirigana():
global user_hirigana_answer
test_hirigana = tk.Toplevel()
test_hirigana.title("Test Hirigana")
test_hirigana.geometry("240x580")
test_hirigana_frame = tk.Frame(test_hirigana, bg="white")
test_hirigana_frame.grid()
hirigana_japanese_label = tk.Label(test_hirigana_frame, textvariable=hirigana_japanese_character, bg="white",
font=("Helvetica", 50))
hirigana_japanese_label.grid(row=0)
correct_label = tk.Label(test_hirigana_frame, textvariable=correct_answer, bg="white",
font=("Helvetica", 30))
correct_label.grid(row=1)
user_hirigana_answer = tk.Entry(test_hirigana_frame)
user_hirigana_answer.grid(row=2)
check_button = tk.Button(test_hirigana_frame, text="Check Answer", width=25, height=4, bg="grey",
command=check_hirigana_answer)
check_button.grid(row=3, sticky='', padx=20, pady=20)
next_button = tk.Button(test_hirigana_frame, text="Next character", width=25, height=4, bg="grey",
command=update_hirigana_character)
next_button.grid(row=4, sticky='', padx=20, pady=20)
close_button = tk.Button(test_hirigana_frame, text="Close", width=25, height=4, bg="grey",
command=test_hirigana.destroy)
close_button.grid(row=5, sticky='', padx=20, pady=20)
# main buttons
button_frame = tk.Frame(root)
button_frame.grid(sticky='')
test_button = tk.Button(button_frame, text="Test", width=25, height=4, command=open_test)
test_button.grid(row=1, sticky='', padx=20, pady=20)
quit_button = tk.Button(button_frame, text="Quit", width=25, height=4, command=root.destroy)
quit_button.grid(row=2, sticky='', padx=20, pady=20)
if __name__ == "__main__":
tk.mainloop()