Uncategorized

Python Tkinter Entry return value


Does anyone have experience with the tkinter module in Python? I’m trying to create an application and I’m stuck on a probably trivial, but for me permanent problem. I want Entry to return the user’s input, but only after the user enters the answer and presses Enter. for a while. I get either that it returns the value before I press Enter. Or that it first returns None, so in the next iteration I get a returned answer to the previous question. The idea is to generate a question and the user enters the answer that I need to keep so that I can further check etc…

# Entry for entering answers within the quest_frame, within this frame I have another label for printing questions.

self.answer_entry = ttk.Entry(self.quest_frame)
self.answer_entry.pack(side=tk.LEFT,padx=10)
self.answer_entry["textvariable"] = self.answer_var
self.answer_entry.bind('<Key-Return>',      self.run_level)
# the rest of the code      
def run_level(self, event=None):
    self.answer_entry.focus() 
    #      user_answer = self.check_answer()      
    question = f"{first_num[0]} * {second_num[0]}"
    print(question)
    self.quest_label.config(text=question)
    self.answer_entry.delete(0,tk.END)`

I fell into some kind of loop because when I clear the input field, the run_level() method is restarted and the question is generated again and I can enter the answer again ….

I’m trying to write a program that will help children learn multiplication through games and competitions. I wrote the upper part but I’m stuck because I want the user’s input to be forwarded to me only when the user presses Enter. For that I used the event in Entry.

self.answer_entry.bind('<Key-Return>',      self.run_level)

I tried to save the value in a variable that is an attribute of the class.
self.answer_var = tk.StringVar()

In the end, after much persuasion, I got the order that the user enters back in the run_level method. But I have a problem that I get stuck and everything repeats itself endlessly. And the first time the question is asked self.answer_var
return “None” to me, and then return the answer to the previous question on the next question.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *