简体   繁体   中英

Error while inserting a value in Python tkinter Entry box

I was making a program that takes values from a user and then appends that values to a db file using sqlite3 database. But while inserting the value to entry box a strange error occured.

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()

The following error was raised

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"

The error is telling you exactly what the problem is. 0.0 is a bad index. self.number is an Entry , and indexes are integers starting at zero.

You also try to use this same index for a Text widget, which is also invalid. Text widget indexes are a string ( not floating point number) of the form <line>.<char> , where <line> is the number number starting at 1 (one) and <char> starts with 0 (zero). Thus, the very first index of a Text widget is the string "1.0" .

Read the traceback in relation to your code. self.number is an Entry(). An entry index is defined in this fine reference , which I recommend that people bookmark. Since Entry widgets contain a single line of text, an index for such is single int. Try 0 instead of 0.0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM