Python中使用Tkinter的GUI新手。试图在输入框中进行格式检查验证(只允许浮点值(。返回此错误tkinter.TclError: expected floating-point number but got "0.0fhdhdfhdf"
,tkinter消息框不显示。
我在前面的代码中将这些变量声明为DoubleVar
。我知道这是一个数据类型问题,但我已经尝试了所有方法。有什么想法值得赞赏吗?
#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")
除了验证float之外,使用try,而不是使用.isdigit
def validate(convert):
try:
x = float(convert)
except Exception as e:
return False
else:
return True