将值保存到数据库时出错。
from tkinter import *
import mysql.connector as c
global e1
global e2
global e3
a = Tk()
Label(a,text="name:").grid(row=0,column=0)
e1=Entry(a).grid(row=0,column=1)
Label(a,text="class:").grid(row=1,column=0)
e2=Entry(a).grid(row=1,column=1)
Label(a,text="roll_no:").grid(row=2,column=0)
e3=Entry(a).grid(row=2,column=1)
def command():
x=e1.get()
y=e2.get()
z=e3.get()
a=c.connect(user="root",password="123456789",
host="localhost",database="student")
cursor1=a.cursor()
cursor1.execute("insert into student_info
(name,class,roll_no)values(%s,%s,%s)" ,(x,y,z))
a.commit()
a.close()
button=Button(a,text="submit",command=command).grid(row=3,column=0)
a.mainloop()
这是运行代码后返回的错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\Hari om\Desktop\tp1.py", line 14, in command
x=e1.get()
AttributeError: 'NoneType' object has no attribute 'get'
答案 0 :(得分:0)
grid
是您在现有条目上调用的方法,但它本身不返回条目。将您的Entry
行更改为以下内容:
e1 = Entry(a) # Gets a new entry
e1.grid(row=0,column=1) # Sets the grid property of that entry
...
由于您立即调用.grid
,新创建的Entry(a)
对象会被该方法使用,但从未返回,因此e1
e2
和{{ 1}}被指定为e3
的默认None
返回值。