我创建了一个简单的程序来更新客户数据库。除更新功能外,我的所有功能均正常运行,该功能需要在新窗口中打开,以便我可以编辑记录。我想我已经关闭了,但是我的新窗口没有显示任何文本框或标签。我的代码在“创建更新功能”中。谢谢。
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()
使用@stovfl提到的顶级小部件,我可以编辑代码。现在可以正常工作(打开一个新窗口,其中包含要编辑的正确数据)。我的书没有超出toplevelwidget,所以谢谢。如果其他人需要,这是我的新更新功能。
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)
#grid must be on seperate line or causes Nonetype error.
NameEditBox.grid(column=1,row=0)
#grid must be on seperate line or causes Nonetype error.
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])