-1

New to GUI's in Python and using Tkinter. Trying to get a format check validation to work in an entry box (want to allow float's only). Returns this error tkinter.TclError: expected floating-point number but got "0.0fhdhdfhdf" and tkinter message box does not display.

I declared the variables earlier in the code as DoubleVar. I know it's a data type issue but have tried everything. Any ideas appreciated?

#3 x variables declared 
value0 = StringVar()
convert = DoubleVar()
currency = DoubleVar()

#This is the boolean currency converter defining the ConCurency method which calls the variable value0 and speeding up conversion of the float using parallel program with the get and set class for the value of USD/JPY/EUR
max_input = 50000
min_input = 10

def ConCurrency(): #performing the conversion #credit to https://www.youtube.com/channel/UCI0vQvr9aFn27yR6Ej6n5UA This was a you tube video, that I found and have changed to suit my needs.  
 if value0.get() == "USD":
     convert1 = float(convert.get() *1.34)
     convert2 = "USD", str('%.2f' %(convert1))
     currency.set(convert2)
 elif value0.get() == "JPY":
     convert1 = float(convert.get() *146.4131)
     convert2 = "JPY", str('%.2f' %(convert1))
     currency.set(convert2)
 elif value0.get() == "EUR":
     convert1 = float(convert.get() *1.1300)
     convert2 = "EUR", str('%.2f' %(convert1))
     currency.set(convert2)
 
# defining the Reset value for the original value0 using the set class.
def Reset():
   value0.set("")
   convert.set("0.0")
   currency.set("0.0")
   
#Entry Widget - to create the input space for the user to add their amount of GBP.  
ent_Currency=Entry(MainFrame,font=('PlayFairDisplay'), textvariable=convert, bd=2,width=28, justify='center')
ent_Currency.grid(row=0,column=1)

def validate(convert):
        if convert.isdigit:
            return True
            tkinter.messagebox.showinfo("Correct Data","That is a valid input")
        else:
            False
            currency.set("")
            tkinter.messagebox.showwarning("Wrong Data", "Numbers Only")
                        

    
martineau
  • 119,623
  • 25
  • 170
  • 301
  • `.isdigit` is a function so you need to use `convert.isdigit()`. Also I don't think `.isdigit()` is the correct function because `"1.1".isdigit()` returns `False`. Look at [this](https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python) – TheLizzard Jun 17 '21 at 10:33
  • Please provide a complete stack trace. Also, what are you entering to get the error? And finally, the code you posted never calls `validate`, so it seems like it is impossible to reproduce your problem with this code. – Bryan Oakley Jun 17 '21 at 14:36

1 Answers1

0

Use try-except for validating float instead of using .isdigit

def validate(convert):
    try:
        x = float(convert)
    except Exception as e:
        return False
    else:
        return True
Mikhail M
  • 1
  • 1
  • 1
    There is no point to the `else` statement, you can just `return True` there. – TheLizzard Jun 17 '21 at 10:40
  • Thanks Mikhail M, will try it. Maybe won't bother with the else. – pricen29 Jun 17 '21 at 11:07
  • The `else` is perfectly fine IMO. – martineau Jun 17 '21 at 11:20
  • Still getting this error, is it something to do with the way I've declared variables earlier on? return self._tk.getdouble(self._tk.globalgetvar(self._name)) _tkinter.TclError: expected floating-point number but got "0.fhsdfhfhfh0" – pricen29 Jun 17 '21 at 11:41
  • @martineau It is unneeded because of the other return. – TheLizzard Jun 17 '21 at 12:12
  • @pricen29 Change your `DoubleVar`s to `StringVar`s. – TheLizzard Jun 17 '21 at 12:13
  • @TheLizzard thanks, but this then can't do the conversion bit. convert1 = float(convert.get() *1.34) TypeError: can't multiply sequence by non-int of type 'float' – pricen29 Jun 17 '21 at 13:05
  • @pricen29 Then use `float(convert.get()) *1.34`. – TheLizzard Jun 17 '21 at 13:05
  • @pricen29 You can't use `.get` on a `DoubleVar` if there isn't a float inside the entry. It will raise an error. So you have to use a `StringVar` and convert it into a `float` before multiplying it. That is just how `tkinter` works. – TheLizzard Jun 17 '21 at 13:07
  • @TheLizzard: I know that, but using an `else` is fine, too — one could argue it's more "symmetric". – martineau Jun 17 '21 at 13:15
  • @martineau It is more symmetrical but when it is compiled it is compiled with 2 more statements (that are never executed). Also when I program, I try removing all code lines that don't effect the program's execution. It's just a preference so having the `else` is fine too. – TheLizzard Jun 17 '21 at 13:22