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.
Since the complete code is not available, it's bit difficult to comment. What I assume from your available code is:
I try to help you in another way.
I had created a questioner-project for my daughter to revise sin, cos, tan values of known angles. I've just modified that for your requirement and posting here.(For multiplication table from 2 to 12). If your requirement is for higher values, you can change accordingly.
The main window has just a 'start session' button. If the kids the button,a pop-up window appears. The pop-up window will ask 10 questions one by one. Each right answer gives 10 points. Same-button meant for submitting and to ask next question. But, the text in button will change either 'submit' or 'next question' accordingly. After submitting, the text-entry widget disappears. For every question, score will be displayed. If the entry is blank or if you enter str instead of number, It gives you error text and wait for the correct input.After completing all the 10 questions, It will give you a Final score and the same button will ask you whether to ask another session. During the entire session, the start button in the main window will be disabled. And this will be re-enabled after the session is over.
In fact, I've typed the above paragraph because I couldn't add comments. Or I've to go thru the entire code once again.
After modifying, I checked and it is working alright. Please trace the functions one by one. Except the styling part, Everything i've written
Changing this to OOPS structure is not a big deal. When you yourself try to do that, You'll understand the entire concept.