我正在创建一个从用户获取值的程序,然后使用sqlite3数据库将该值附加到db文件。但是在将值插入输入框时会发生奇怪的错误。
import sqlite3,time,datetime
from Tkinter import *
def create_table():
conn = sqlite3.connect("medicaps.db")
c = conn.cursor()
c.execute("CREATE TABLE data(NAME TEXT,ENROLLMENT_NUMBER INT,MARKS INT,GRADE TEXT,UNIXTIME REAL ,DATETIME TEXT)")
class Application(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
Label(self,text = "Welcome to the database entry program.").grid(row = 0,column = 0,columnspan = 2,sticky = W)
Label(self,text = "Name : ").grid(row = 1,column = 0,sticky = W)
Label(self,text = "Enrollment number : ").grid(row = 2,column = 0,sticky = W)
Label(self,text = "Marks : ").grid(row = 3,column = 0,sticky = W)
Label(self,text = "Grade : ").grid(row = 4,column = 0,sticky = W)
self.name = Entry(self)
self.name.grid(row = 1,column = 1)
self.number = Entry(self)
self.number.grid(row = 2,column = 1)
self.marks = Entry(self)
self.marks.grid(row = 3,column = 1)
self.grade = Entry(self)
self.grade.grid(row = 4,column = 1)
self.text = Text(self,width = 35,height = 25,wrap = WORD)
self.text.grid(row = 6,column = 0,columnspan = 2,sticky = W)
Button(self,text = "Submit",command = self.reveal).grid(row = 5,column = 0,sticky = W)
def reveal(self):
conn = sqlite3.connect("medicaps.db")
c = conn.cursor()
unix = time.time()
date = str(datetime.datetime.fromtimestamp(int(time.time())).strftime("%Y-%M-%d %H:%M:%S"))
name = self.name.get()
number = self.number.get()
marks = self.marks.get()
grade = self.grade.get()
self.text.insert(0.0,"yoyo")
try:
number = int(number)
except(ValueError):
error_n = "Invalid Input"
self.number.insert(0.0,error_n)
try:
marks = int(number)
except(ValueError):
error_m = "Invalid Input"
self.marks.insert(0.0,error_m)
c.execute("INSERT INTO data(NAME,ENROLLMENT_NUMBER,MARKS,GRADE,UNIXTIME,DATETIME) VALUES = (?,?,?,?,?,?)",
(name,number,marks,grade,unix,date))
conn.commit()
message = "Value appended successfully"
self.text.delete(0.0,END)
root = Tk()
root.title("Dataentry")
root.geometry("500x750")
frame = Application(root)
root.mainloop()
引发了以下错误
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
return self.func(*args)
File "C:\Users\sahib navlani\Desktop\New folder\vdsf.py", line 62, in reveal
self.number.insert(0.0,error_n)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2517, in insert
self.tk.call(self._w, 'insert', index, string)
TclError: bad entry index "0.0"
答案 0 :(得分:1)
错误正在告诉您问题的确切原因。 0.0
是一个糟糕的索引。 self.number
是Entry
,索引是从零开始的整数。
您还尝试对Text
窗口小部件使用相同的索引,这也是无效的。 Text
窗口小部件索引是< line>格式的字符串(不浮点数)。< char> ,其中< line&gt ; 是从1
(一)开始的数字编号,< char> 以0
(零)开头。因此,Text
窗口小部件的第一个索引是字符串"1.0"
。
答案 1 :(得分:0)
阅读与您的代码相关的追溯。 self.number是一个Entry()。此fine reference中定义了一个条目索引,我建议人们加入书签。由于Entry小部件包含单行文本,因此这样的索引是单个int。请尝试使用0
代替0.0
。