I have created a simple program for updating a customer database. All of my functions work fine except the update function, which need to open in a new window so I can edit the record. I think I am close, but my new window does not show any text boxes or labels. My code is below in "create update function". Thank you.
import tkinter as tk
from tkinter import ttk
import sqlite3
from contextlib import closing
conn = None
class CustomerFrame(ttk.Frame):
def __init__(self,parent):
ttk.Frame.__init__(self, parent, padding="10 10 10 10")
self.pack(fill=tk.BOTH, expand=True)
#create string variables for entry fields
self.Name = tk.StringVar()
self.Email = tk.StringVar()
self.Delete = tk.StringVar()
#create labels
NameLabel = ttk.Label(self, text = "Name:").grid(column=0,row=0, sticky=tk.E)
EmailLabel = ttk.Label(self, text = "Email:").grid(column=0,row=1, sticky=tk.E)
DeleteLabel = ttk.Label(self, text = "Delete:").grid(column=0,row=2, sticky=tk.E)
#create entry boxes
NameEntry = ttk.Entry(self, width = 20, textvariable=self.Name).grid(column=1,row=0)
EmailEntry = ttk.Entry(self, width = 20, textvariable=self.Email).grid(column=1,row=1)
DeleteEntry= ttk.Entry(self, width = 20, textvariable=self.Delete).grid(column=1,row=2)
#create buttons
button1 = ttk.Button(self, text="View Records", command=self.display_customers).grid(column=2, row=0, ipadx=97)
button2 = ttk.Button(self, text="Add Record", command=self.add_customer).grid(column=2, row=1, ipadx=98)
button3 = ttk.Button(self, text="Delete Record", command=self.delete_customer).grid(column=2, row=2, ipadx=96)
button4 = ttk.Button(self, text="Edit Record", command=self.update_customer).grid(column=2, row=3, ipadx=100)
#add padding
for child in self.winfo_children():
child.grid_configure(padx=5, pady=3)
#----------------------------------------------------------------------------------------------------------------
#create update function
def update_customer(self):
editor = tk.Tk()
editor.title("Edit Screen")
editor.geometry("400x600")
editor.mainloop()
editor.Name = tk.StringVar()
editor.Email = tk.StringVar()
Name_entry = ttk.Entry(editor, width = 20, textvariable=editor.Name).grid(column=1,row=0)
Email_entry = ttk.Entry(editor, width = 20, textvariable=editor.Email).grid(column=1,row=1)
#----------------------------------------------------------------------------------------------------------------
#define the callback methods for buttons
def add_customer(self):
name = self.NameText.get()
email = self.EmailText.get()
conn = sqlite3.connect("inventory.sqlite")
c = conn.cursor()
with closing(conn.cursor()) as C:
sql = '''INSERT INTO Customers (name, email)
VALUES (?, ?)'''
c.execute(sql, (name, email))
conn.commit()
#----------------------------------------------------------------------------------------------------------------
#create function to delete record based on ID entry
def delete_customer(self):
conn = sqlite3.connect("inventory.sqlite")
c = conn.cursor()
with closing(conn.cursor()) as C:
c.execute("DELETE from customers WHERE customerID = " + self.Delete.get())
conn.commit()
#----------------------------------------------------------------------------------------------------------------
#create function to display table contents
def display_customers(self):
conn = sqlite3.connect("inventory.sqlite")
c = conn.cursor()
query = '''SELECT * FROM Customers'''
c.execute(query)
with closing(conn.cursor()) as c:
#query the database
query = '''SELECT * FROM Customers'''
c.execute(query)
customers = c.fetchall()
print(customers)
#loop through results, format tuple
print_customers = ''
for customer in customers:
print_customers += str(customer[0]) + " | " + str(customer[1]) + " | " + str(customer[2]) + "\n"
query_label = ttk.Label(self, text=print_customers)
query_label.grid(row=4, column=0, columnspan=4)
#Commit Changes
conn.commit()
if __name__ == "__main__":
root = tk.Tk()
root.title("Customer Screen")
CustomerFrame(root)
root.mainloop()