0

I am making a GUI tkinter program for a school assignment, which requires the user to enter name, and age, and then do a quiz, my problem is when they enter their age as a string it should say please enter a number but i get this error

_tkinter.TclError: expected floating-point number but got

this is the part which concerns the age input

def show_frame2(self):

   try:

        if self.name.get() == "":
            self.warning.configure(text = "Please enter your name")
            self.NameEntry.focus()
        elif self.name.get().isalpha() == False:
            self.warning.configure(text = "Please enter text")
            self.NameEntry.delete(0, END)
            self.NameEntry.focus()

        elif self.AgeEntry.get() == "":
            self.warning.configure(text = "Please enter a number")
            self.AgeEntry.delete(0, END)
        elif self.age.get() > 14:
            self.warning.configure(text = "You are to old")
            self.AgeEntry.delete(0, END)
        elif self.age.get() <= 0:
            self.warning.configure(text = "Please enter a number number other than 0")
            self.age_entry.delete(0, END)
        elif self.age.get() <= 7:
            self.warning.configure(text = "Sorry, You are to young")
        else:
            self.frame1.grid_remove()
            self.frame2.grid(row = 1, columnspan = 4)
            self.next_problem()

    except ValueError:
        self.warning.configure(text = "Please enter a number")
        self.AgeEntry.delete(0,END)
        self.AgeEntry.focus()
luigigi
  • 4,146
  • 1
  • 13
  • 30
  • Relevant [Interactively validating Entry widget content in tkinter](https://stackoverflow.com/questions/4140437)a – stovfl May 06 '20 at 07:31
  • ***TclError: expected floating-point number but got***: [Edit] your question and show the full Traceback – stovfl May 06 '20 at 07:35

2 Answers2

0

You should change the except ValueError into except _tkinter.TclError.

This would fix your problem.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
0

self.age should probably be changed to be a StrVar; then do the float conversion yourself:

try:
    age = float(self.age.get())
except (ValueError, TypeError):
    # ... appropriate warning
Torben Klein
  • 2,943
  • 1
  • 19
  • 24