简体   繁体   中英

Exception in Tkinter callback with KEY ERROR

The below is my code with the name of abc.py and i am getting and error of type error on line 15. i don't know how to sort out the problem The below is my code with the name of abc.py and i am getting and error of type error on line 15. i don't know how to sort out the problem

import tkinter as tk
from tkinter import ttk, messagebox
import mysql.connector
from tkinter import *
def GetValue(event):
    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e5.delete(0, END)
    row_id = listBox.selection()[0]
    select = listBox.set(row_id)
    e1.insert(0,select['id'])
    e2.insert(0,select['cname'])
    e3.insert(0,select['cfname'])
    e4.insert(0,select['ctype'])
    e5.insert(0,select['coccupation'])
 
def Add():
    cid = e1.get()
    cnam = e2.get()
    cfnam = e3.get()
    ctyp = e4.get()
    cocc = e4.get()
    mysqldb=mysql.connector.connect(host="localhost",user="root",password="",database="criminal")
    mycursor=mysqldb.cursor()
 
    try:
       sql = "INSERT INTO  criminal (id,cname,cfname,ctype,coccupation) VALUES (%s, %s, %s, %s, %s)"
       val = (cid,cnam,cfnam,ctyp,cocc)
       mycursor.execute(sql, val)
       mysqldb.commit()
       lastid = mycursor.lastrowid
       messagebox.showinfo("information", "Employee inserted successfully...")
       e1.delete(0, END)
       e2.delete(0, END)
       e3.delete(0, END)
       e4.delete(0, END)
       e5.delete(0, END)
       e1.focus_set()
    except Exception as e:
       print(e)
       mysqldb.rollback()
       mysqldb.close()
 
 
def update():
    cid = e1.get()
    cnam = e2.get()
    cfnam = e3.get()
    ctyp = e4.get()
    cocc = e4.get()
    mysqldb=mysql.connector.connect(host="localhost",user="root",password="",database="criminal")
    mycursor=mysqldb.cursor()
 
    try:
       sql = "Update  criminal set cname= %s,cfname= %s,ctype= %s,coccupation= %s where id= %s"
       val = (cid,cnam,cfnam,ctyp,cocc) 
       mycursor.execute(sql, val)
       mysqldb.commit()
       lastid = mycursor.lastrowid
       messagebox.showinfo("information", "Record Updateddddd successfully...")
 
       e1.delete(0, END)
       e2.delete(0, END)
       e3.delete(0, END)
       e4.delete(0, END)
       e5.delete(0, END)
       e1.focus_set()
 
    except Exception as e:
 
       print(e)
       mysqldb.rollback()
       mysqldb.close()
 
def delete():
    cid = e1.get()  mysqldb=mysql.connector.connect(host="localhost",user="root",password="",database="criminal")
    mycursor=mysqldb.cursor()
 
    try:
       sql = "delete from criminal where id = %s"
       val = (cid,)
       mycursor.execute(sql, val)
       mysqldb.commit()
       lastid = mycursor.lastrowid
       messagebox.showinfo("information", "Record Deleteeeee successfully...")
 
       e1.delete(0, END)
       e2.delete(0, END)
       e3.delete(0, END)
       e4.delete(0, END)
       e5.delete(0, END)
       e1.focus_set()
 
    except Exception as e:
 
       print(e)
       mysqldb.rollback()
       mysqldb.close()
 
def show():
        mysqldb = mysql.connector.connect(host="localhost", user="root", password="", database="criminal")
        mycursor = mysqldb.cursor()
        mycursor.execute("SELECT id,cname,cfname,ctype,coccupation FROM criminal")
        records = mycursor.fetchall()
        print(records)
 
        for i, (crid,crname, crfname,crtype,croccupation) in enumerate(records, start=1):
            listBox.insert("", "end", values=(crid, crname, crfname, crtype, croccupation))
            mysqldb.close()

root = Tk(className='Zohra & Team Criminal Management Software')
root['background']='#856ff8'
root.geometry("1200x500")
global e1
global e2
global e3
global e4
global e5
 
tk.Label(root, text="Criminal Management By zohra & Team", fg="Green",background="yellow" , font=(None, 30)).place(x=300, y=5)


tk.Label(root, text="Criminal ID",background="yellow",fg="green").place(x=10, y=10)
Label(root, text="Criminal Name",background="yellow").place(x=10, y=40)
Label(root, text="Criminal Father-Name",background="yellow").place(x=10, y=70)
Label(root, text="Criminal Type",background="yellow").place(x=10, y=100)
Label(root, text="Criminal Occupation",background="yellow").place(x=10, y=130)

 
e1 = Entry(root)
e1.place(x=140, y=10)
 
e2 = Entry(root)
e2.place(x=140, y=40)
 
e3 = Entry(root)
e3.place(x=140, y=70)
 
e4 = Entry(root)
e4.place(x=140, y=100)

e5 = Entry(root)
e5.place(x=140, y=130)
 
Button(root, text="Add",command = Add,height=3, width= 13).place(x=30, y=130)
Button(root, text="update",command = update,height=3, width= 13).place(x=140, y=130)
Button(root, text="Delete",command = delete,height=3, width= 13).place(x=250, y=130)
 
cols = ('Criminal id', 'Criminal Name', 'Criminal Father-Name', 'Criminal Type', 'Criminal Occupation')
listBox = ttk.Treeview(root, columns=cols, show='headings' )
 
for col in cols:
    listBox.heading(col, text=col)
    listBox.grid(row=1, column=0, columnspan=2)
    listBox.place(x=10, y=200)
 
show()
listBox.bind('<Double-Button-1>',GetValue)
 
root.mainloop()

The below is key error message and i am not getting the solution for this

Error message:


Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\97150\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "abc.py", line 15, in GetValue
    e1.insert(0,select['id'])
KeyError: 'id'

this is full code which i updated

Since select is the result of listBox.set(row_id) , it is a dictionary of column/value pairs for the selected item. The keys are then the column names of the treeview listBox instead of the column names from the database table:

def GetValue(event):
    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e5.delete(0, END)
    row_id = listBox.selection()[0]
    select = listBox.set(row_id)
    e1.insert(0, select['Criminal id'])
    e2.insert(0, select['Criminal Name'])
    e3.insert(0, select['Criminal Father-Name'])
    e4.insert(0, select['Criminal Type'])
    e5.insert(0, select['Criminal Occupation'])

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