Search code examples
pythontkintermessageboxstripfunction

Tkinter: TypeError: Submit() takes exactly 1 positional argument (0 given)


I am trying to make a tkinter program that once a answer has been entered into an entry box submit is pressed which then activates the Submit code. The submit code should receive the information in the entry widget and then check if the user has actually submitted an answer.

If this is true then INCORRECT or CORRECT is prompt if the answer the user input is the same as the answer I prescribed earlier.

I have looked at other solutions and they do not seem applicable to this circumstance

    entryWidget = Entry(root)
    entryWidget["width"] = 50
    entryWidget.pack()
    entryWidget.pack()
    submitButton = Button(root, text= "Submit Answer", command =Submit)
    submitButton.pack()



def Submit(entryWidget):
     """ Display the Entry text value. """

     userAnswer= entryWidget.get()

     if userAnswer.strip() == "":
         tkinter.messagebox.showerror("Tkinter Entry Widget", "Please enter a number.")

     if int(correctAnswer) != userAnswer.strip():
         tkinter.messagebox.showinfo("Answer", "INCORRECT!")
     else:
         tkinter.messagebox.showinfo("Answer", "CORRECT!")

Solution

  • The submit code should receive the information in the entry widget...

    No, there is no reason for the submit function to receive the entry widget as its first argument. Functions bound to command will not receive any arguments. If you want Submit to be called with an argument, you need to do so yourself, wrapped in an anonymous lambda function:

    submitButton = Button(root, text= "Submit Answer", command =lambda: Submit(entryWidget))