_tkinter.TclError: 预期的浮点数却得到了""

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了_tkinter.TclError: 预期的浮点数却得到了""相关的知识,希望对你有一定的参考价值。

试图连接并将收集到的数据添加到mysql数据库中,却出现了错误。请帮助解决这个故障。我刚刚开始我的tkinter之旅。

from tkinter import *
import mysql.connector as mysql

root = Tk()


def flush(*args):

    e_1.delete(0, END)
    e_2.delete(0, END)
    e_3.delete(0, END)

    name = nme.get()
    phone = ph.get()
    emirate = e_id.get()
    con = mysql.connect(host='localhost', user='root', password='monkey123', database='NZ')
    c = con.cursor()
    c.execute("Insert into gli (NAME_S,PH_NO,E_ID) VALUES ('"+name+"','"+str(phone)+"','"+str(emirate)+"')")

    c.execute('commit')

    con.close()


nme= StringVar()
ph = IntVar()
e_id = IntVar()

label_1 = Label(root,text='Patient Name',fg='blue')
label_2 = Label(root,text='Phone number',fg='blue')
label_3 = Label(root,text='Emirates ID',fg='blue')

label_1.grid(row=0,column=0)
label_2.grid(row=1,column=0)
label_3.grid(row=2,column=0)

e_1 = Entry(root,borderwidth=2,textvariable=nme)
e_2 = Entry(root,borderwidth=2,textvariable=ph)
e_3 = Entry(root,borderwidth=2,textvariable=e_id)

e_1.grid(row=0,column=1,ipady=10,padx=10,pady=10)
e_2.grid(row=1,column=1,ipady=10,padx=10,pady=10)
e_3.grid(row=2,column=1,ipady=10,padx=10,pady=10)

B_1 = Button(root,text='ENTER',command =flush)
B_1.grid(row=4,column=1)



root.mainloop()

错误信息。

 Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Users
ihaaAppDataLocalProgramsPythonPython37-32lib	kinter\__init__.py", line 508, in get
    return self._tk.getint(value)
_tkinter.TclError: expected integer but got ""

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:Users
ihaaAppDataLocalProgramsPythonPython37-32lib	kinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/nihaa/PycharmProjects/GUI/venv/db'.py", line 14, in flush
    phone = ph.get()
  File "C:Users
ihaaAppDataLocalProgramsPythonPython37-32lib	kinter\__init__.py", line 510, in get
    return int(self._tk.getdouble(value))
_tkinter.TclError: expected floating-point number but got "" 
答案

你在取值之前删除了widgets的内容。因此,值是空字符串,因此错误的说它期望一个浮点数,但得到一个空字符串。

以上是关于_tkinter.TclError: 预期的浮点数却得到了""的主要内容,如果未能解决你的问题,请参考以下文章

tkinter、python 和 seaborn 的问题:_tkinter.TclError: no display name and no $DISPLAY environment variable

Python系列_tkinter.TclError: no display name and no $DISPLAY environment variable

markdown pyplot _tkinter.TclError:没有显示名称,没有$ DISPLAY环境变量

python3 tkinter报错:_tkinter.TclError: cannot use geometry manager pack inside . which already has sla

_tkinter.TclError: invalid command name “tixComboBox“ 的解决办法

matplotlib报错_tkinter.TclError: no display name and no $DISPLAY environment variable