-1

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()
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/a/48045508/7414759) and [When to use the Toplevel Widget](http://effbot.org/tkinterbook/toplevel.htm) and [Tkinter understanding mainloop](https://stackoverflow.com/a/29158947/7414759) – stovfl Jun 12 '20 at 15:37
  • From reading, sounds like I have the option to create another file, or make use of the toplevel widget instead for my new window. Am I on the right track? – Lache Porter Jun 12 '20 at 20:57
  • ***make use of the toplevel widget***: Yes – stovfl Jun 12 '20 at 21:01

1 Answers1

0

Using the toplevel widget that @stovfl mentioned, I was able to edit my code. It works now (opens a new window with the correct data to edit). My book does not go over toplevelwidget so thank you. Here is my new update function if others need.

def update_customer(self):    
    #Create widget 
    top1 = Toplevel(self)     
    #Define title for window 
    top1.title("Edit Customer Screen") 
    #Create label 
    NameEditLabel = ttk.Label(top1, text = "Name:").grid(column=0,row=0, sticky=tk.E)
    EmailEditLabel = ttk.Label(top1, text = "Email:").grid(column=0,row=1, sticky=tk.E)    
    #Create text boxes
    NameEditBox = ttk.Entry(top1, width=20)
    #.grids must be on seperate line or causes Nonetype error?
    NameEditBox.grid(column=1,row=0)
    EmailEditBox = ttk.Entry(top1, width = 20)
    EmailEditBox.grid(column=1,row=1)     
    #Create buttons
    ButtonSave = ttk.Button(top1, text="Save Record").grid(column=1, row=2, ipadx=97)    
    #Set padding
    for child in top1.winfo_children():
        child.grid_configure(padx=5, pady=3)

    #Create database connection
    conn = sqlite3.connect("inventory.sqlite")      
    #create cursor
    c = conn.cursor()    
    #create variable
    record_id = self.Delete.get()    
    #Query the database
    with closing(conn.cursor()) as C:  
        c.execute("SELECT * FROM customers WHERE customerID = " + record_id)
        customers = c.fetchall()        
    #loop through results
        for customer in customers:
            NameEditBox.insert(0, customer[1])
            EmailEditBox.insert(0, customer[2])