I am writing a program to learn English. The program itself is very simple. The sentences in Russian are displayed alternately and the user must write this sentence in English. I cannot verify the user's input with the value of what is stored in the dictionary. Here is my code:

#-*- coding: utf-8 -*- import random, time from tkinter import * root = Tk() root.title('MyGui') root.geometry('500x500') d = {'собака': 'dog', 'кошка': 'cat', 'стул': 'table'} def foo(*event): s = random.choice(list(d.keys())) lab.config(text=s) data = ent.get() if data == d.get(s): ent.delete(0, END) not_correct.place_forget() else: ent.delete(0, END) not_correct.place(x=250, y=200, anchor='center') not_correct = Label(root, text='No! it is error') lab = Label(root, text='Demo') lab.place(x=250, y=10, anchor='center') ent = Entry(root) ent.place(x=250, y=50, anchor='center') ent.bind('<Return>', foo) but = Button(root, text='Ok') but.place(x=250, y=100, anchor='center') but.bind('<Button-1>', foo) oot.mainloop() 
  • Try to bring to the terminal the contents of both objects, as well as their types. I did not read the code. - aryndin
  • If I use variables that I commented out in the hundler function, then the str () type is displayed in the terminal, but for some reason ..... Yesterday I spent all day sitting above this, I was already sick of this code. - Mike Ru
  • Well, is the content the same? - aryndin
  • Shorten the code to 10-20 lines that you think the problem is in, and explain what you are trying to do, what you expect, what you get in the end. - aryndin
  • And best of all, make a console proof-of-concept application for yourself first, check if it works, and then transfer the logic to the GUI. - aryndin

1 answer 1

Solved. Added a few functions and excluded the method lab.config ().

 #-*- coding: utf-8 -*- import random from tkinter import * root = Tk() root.title('MyGui') root.geometry('500x500') frm = Frame(root, width=500, height=500) def clear(): if frm.winfo_children(): frm.winfo_children()[0].destroy() d = {'собака': 'dog', 'кошка': 'cat', 'стул': 'table'} def foo(*event): def new_label(): s = random.choice(list(d.keys())) return s def handler(event): '''Функция которая обрабатывает введенные данные''' data = ent.get() if data == d.get(nl): ent.delete(0, END) not_correct.place_forget() # Здесь я удаляю виджет чтоб слова в виджете # не накладывались друг на друга lab.place_forget() foo() # вернуться на исходную) else: ent.delete(0, END) not_correct.place(x=250, y=200, anchor='center') nl = new_label() lab = Label(frm, text=nl) lab.place(x=250, y=10, anchor='center') not_correct = Label(frm, text='it is not correct') ent = Entry(frm) ent.place(x=250, y=50, anchor='center') ent.focus_force() ent.bind('<Return>', handler) big = Button(frm, text='Ok') big.place(x=250, y=80, anchor='center') big.bind('<Button-1>', handler) clear(), clear() # Удаляю/прячу виджеты из предыдущего фрейма clear(), clear() labus = Label(frm, text='Go') labus.place(x=250, y=10, anchor='center') but = Button(frm, text='Ok') but.place(x=250, y=100, anchor='center') but.bind('<Button-1>', foo) root.focus_force() frm.pack() root.mainloop()