I'm trying to insert rows in a MySQL database using the treeview widget in Tkinter.
The entire data set is supposed to be entered into the table when I execute this code. But I'm only getting an empty string in the places where I gave StringVar.get() and zeroes in the place where I gave IntVar.get(). What's wrong with this code? I tried my best to debug, but I could not.
from tkinter import *
import tkinter as tk
import mysql.connector
from tkinter import messagebox
r = tk.Tk()
r.title("Book Details")
r.geometry("500x500")
connect = mysql.connector.connect(host="localhost", user="root", passwd="1234", database="bookdat")
conn = connect.cursor()
conn.execute("select * from bookinfo")
tree = ttk.Treeview(r)
tree["show"] = "headings"
tree["columns"] = ("Name", "Author", "Edition", "Category", "Cost", "Owner_Address", "Owner_phoneno")
tree.column("Name", width=50, minwidth=50, anchor=tk.CENTER)
tree.column("Author", width=100, minwidth=100, anchor=tk.CENTER)
tree.column("Edition", width=50, minwidth=50, anchor=tk.CENTER)
tree.column("Category", width=150, minwidth=150, anchor=tk.CENTER)
tree.column("Cost", width=150, minwidth=150, anchor=tk.CENTER)
tree.column("Owner_Address", width=150, minwidth=150, anchor=tk.CENTER)
tree.column("Owner_phoneno", width=150, minwidth=150, anchor=tk.CENTER)
tree.heading("Name", text="Name", anchor=tk.CENTER)
tree.heading("Author", text="Author", anchor=tk.CENTER)
tree.heading("Edition", text="Edition", anchor=tk.CENTER)
tree.heading("Category", text="Category", anchor=tk.CENTER)
tree.heading("Cost", text="Cost", anchor=tk.CENTER)
tree.heading("Owner_Address", text="Owner's Address", anchor=tk.CENTER)
tree.heading("Owner_phoneno", text="Owner's contact no", anchor=tk.CENTER)
i = 0
for ro in conn:
tree.insert('', i, text="", values=(ro[0], ro[1], ro[2], ro[3], ro[4], ro[5], ro[6]))
i += 1
hsb = ttk.Scrollbar(r, orient="horizontal")
hsb.configure(command=tree.xview)
tree.configure(xscrollcommand=hsb.set)
hsb.pack(fill=X, side=BOTTOM)
vsb = ttk.Scrollbar(r, orient="vertical")
vsb.configure(command=tree.yview)
tree.configure(yscrollcommand=vsb.set)
vsb.pack(fill=Y, side=RIGHT)
tree.pack()
name = tk.StringVar()
author = tk.StringVar()
edition = tk.IntVar()
category = tk.StringVar()
cost = tk.IntVar()
owner_address = tk.StringVar()
owner_phoneno = tk.IntVar()
def add_data(tree):
f = Frame(r, width=400, height=320, background="grey")
f.place(x=100, y=250)
l1 = Label(f, text="name", width=8, font=("Times", 11, "bold"))
e1 = Entry(f, textvariable=name, width=25)
l1.place(x=50, y=30)
e1.place(x=170, y=30)
l2 = Label(f, text="author", width=8, font=("Times", 11, "bold"))
e2 = Entry(f, textvariable=author, width=25)
l2.place(x=50, y=70)
e2.place(x=170, y=70)
l3 = Label(f, text="edition", width=8, font=("Times", 11, "bold"))
e3 = Entry(f, textvariable=edition, width=25)
l3.place(x=50, y=110)
e3.place(x=170, y=110)
e3.delete(0, END)
l4 = Label(f, text="category", width=8, font=("Times", 11, "bold"))
e4 = Entry(f, textvariable=category, width=25)
l4.place(x=50, y=150)
e4.place(x=170, y=150)
l5 = Label(f, text="cost", width=8, font=("Times", 11, "bold"))
e5 = Entry(f, textvariable=cost, width=25)
l5.place(x=50, y=190)
e5.place(x=170, y=190)
e5.delete(0, END)
l6 = Label(f, text="address", width=8, font=("Times", 11, "bold"))
e6 = Entry(f, textvariable=owner_address, width=25)
l6.place(x=50, y=230)
e6.place(x=170, y=230)
l7 = Label(f, text="phoneno", width=8, font=("Times", 11, "bold"))
e7 = Entry(f, textvariable=owner_phoneno, width = l7.place(x=50, y=270)
e7.place(x=170, y=270)
e7.delete(0, END)
def insert_data():
nonlocal e1, e2, e3, e4, e5, e6, e7
s = name.get()
a = author.get()
e = edition.get()
cat = category.get()
cst = cost.get()
addr = owner_address.get()
ph = owner_phoneno.get()
conn.execute("INSERT INTO bookinfo(name,author,edition,category,cost,owner_address,owner_phoneno) VALUES(%s,%s,%s,%s,%s,%s,%s)",(s,a,e,cat,cst,addr,ph))
connect.commit()
tree.insert('', "end", text="", values=(s, a, e, cat, cst, addr, ph))
messagebox.showinfo("Success", "Data registered")
e1.delete(0, END)
e2.delete(0, END)
e3.delete(0, END)
e4.delete(0, END)
e5.delete(0, END)
e6.delete(0, END)
e7.delete(0, END)
f.destroy()
submitbutton = tk.Button(f, text="Submit", command=insert_data)
submitbutton.configure(font=("Times 11 bold"), bg="PaleTurquoise3", fg="white")
submitbutton.place(x=100, y=280)
cancelbutton = tk.Button(f, text="Cancel", command=f.destroy)
cancelbutton.configure(font=("Times 11 bold"), bg="PaleTurquoise3", fg="white")
cancelbutton.place(x=240, y=280)
insertbutton = tk.Button(r, text="Insert", command=lambda:add_data(tree))
insertbutton.configure(font=("calibri 14 bold"), bg="PaleTurquoise3", fg="white")
insertbutton.place(x=600, y=260)
r.mainloop()
'''