Its an application which asks a math problem and user inputs answer in a textbox as integers, and a button submitbtn
verifies if its right or wrong.
I binded a keyboard key f
to the function that runs on pressing button submitbtn
, which works fine, but the key f
gets added to the textbox after user's answer before it submits and gives it as a wrong answer.
Text Box
text_Input = StringVar
txtbox = Entry(font=('arial',20, BOLD), textvariable=text_Input)
txtbox.grid(columnspan = 2, row = 3, pady = 20)
txtbox.focus_set()
Submit Button
submitbtn = Button(text="Submit", padx=10, pady=10, command=lambda:submit(txtbox.get(), y))
Submit Function
def submit(z, y):
global correct_answer, wrong_answer, submitbtn
y=str(y)
if z==y:
correct_answer+=1
lbl2.configure(text=correct_answer)
else:
wrong_answer+=1
lbl4.configure(text=wrong_answer)
submitbtn.config(state="disabled")
Binding
game.bind('f', lambda event: submit(txtbox.get(), y))
#"game" is the name of Tk()
#submit is the function linked to submitbtn
#This works well if I bind it to <Return> (Enter Key)
Actual Output:
5+8
User enters: 13
Presses 'f' to submit answer
Answer processed: 13f
Is there a way to process textbox inputs in real-time to make sure every character entered is an integer? If user enters anything except 0-9, I want it to note nothing in the textbox.
Also, I disable the
submitbtn
after it is pressed once, but pressing f repeatedly keep incrementing thecorrect_answer
variable. Is there a way to bind the key to thesubmitbtn
which in turn will call the functionsubmit
, instead of directly linking the keyf
tosubmit
function?