我创建了一个用于更新客户数据库的简单程序。我所有的函数都可以正常工作,除了update函数,它需要在一个新窗口中打开,以便我可以编辑记录。我想我很接近,但我的新窗口没有显示任何文本框或标签。我的代码在下面的“创建更新函数”中。谢谢您。
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()
1条答案
按热度按时间w41d8nur1#
使用@stovfl提到的顶级小部件,我可以编辑我的代码。它现在可以工作了(打开一个新窗口,其中包含要编辑的正确数据)。我的书不在上面,所以谢谢你。这是我的新更新功能,如果别人需要的话。