Python 当我在另一个嵌套类函数中调用某个类函数时,该类函数失败
在我的程序中,我定义了如下函数:Python 当我在另一个嵌套类函数中调用某个类函数时,该类函数失败,python,sqlite,tkinter,Python,Sqlite,Tkinter,在我的程序中,我定义了如下函数: def update_household_information(self, parent): try: sub = tk.Toplevel(master=parent) sub.title(f"Update household") sub.resizable(height=False, width=False) # Cr
def update_household_information(self, parent):
try:
sub = tk.Toplevel(master=parent)
sub.title(f"Update household")
sub.resizable(height=False, width=False)
# Create text entries
id_ent = tk.Entry(sub, width=30)
id_ent.grid(row=0, column=1, padx=20)
# Create text labels
id_lbl = tk.Label(sub, text="Enter the ID of the household to be updated: ")
id_lbl.grid(row=0, column=0)
# Create Submit Function for Database:
def submit():
try:
# Create the electric.db or connect to it
conn = sqlite3.connect('electric.db')
# Create cursor
c = conn.cursor()
c.execute("PRAGMA foreign_keys = ON")
# Check if area_id exist:
c.execute("""SELECT id FROM households""")
results = c.fetchall()
household_ids = []
for result in results:
household_ids.append(result[0])
if int(id_ent.get()) not in household_ids:
messagebox.showerror(message="Household ID does not exist")
sub.destroy()
self.update_household_information(parent)
else:
sub.destroy()
self.update_household_information_with_id(parent, id_ent.get())
except Error as e:
return e
# Create Submit Button
submit_btn = tk.Button(sub, text="Submit", command=submit)
submit_btn.grid(row=5, column=0, columnspan=2, ipadx=5, ipady=5, pady=5, padx=5)
except Error as e:
return e
def update_household_information_with_id(self, parent, household_id):
try:
sub = tk.Toplevel(master=parent)
sub.title(f"Update household {household_id}")
sub.resizable(height=False, width=False)
# Create text entries
name_ent = tk.Entry(sub, width=30)
name_ent.grid(row=1, column=1, padx=20)
address_ent = tk.Entry(sub, width=30)
address_ent.grid(row=2, column=1, padx=20)
phone_number_ent = tk.Entry(sub, width=30)
phone_number_ent.grid(row=3, column=1, padx=20)
area_ent = tk.Entry(sub, width=30)
area_ent.grid(row=4, column=1, padx=20)
# Create text labels
id_lbl = tk.Label(sub, text=f"Updating Household {household_id}: ")
id_lbl.grid(row=0, column=0, columnspan=2)
name_lbl = tk.Label(sub, text="Owner's name: ")
name_lbl.grid(row=1, column=0)
address_lbl = tk.Label(sub, text="Address: ")
address_lbl.grid(row=2, column=0)
phone_number_lbl = tk.Label(sub, text="Phone number: ")
phone_number_lbl.grid(row=3, column=0)
area_lbl = tk.Label(sub, text="Area: ")
area_lbl.grid(row=4, column=0)
# Create Submit Function for Database:
def submit():
try:
# Create the electric.db or connect to it
conn = sqlite3.connect('electric.db')
# Create cursor
c = conn.cursor()
c.execute("PRAGMA foreign_keys = ON")
# Check if area_id exist:
c.execute("""SELECT id FROM areas""")
results = c.fetchall()
area_ids = []
for result in results:
area_ids.append(result[0])
if int(area_ent.get()) not in area_ids:
messagebox.showerror(message="Area ID does not exist")
sub.destroy()
self.update_household_information_with_id(parent)
# Insert into table
c.execute(
f"""UPDATE households
SET owner_name = :owner_name,
address = :address,
phone_number = :phone_number,
area_id = :area_id
WHERE id = {household_id}""",
{
'owner_name': name_ent.get(),
'address': address_ent.get(),
'phone_number': phone_number_ent.get(),
'area_id': area_ent.get()
}
)
# Commit changes
conn.commit()
# Close connection
conn.close()
except Error as e:
return e
# Create Submit Button
submit_btn = tk.Button(sub, text="Update Record to Database", command=submit)
submit_btn.grid(row=5, column=0, columnspan=2, ipadx=5, ipady=5, pady=5, padx=5)
except Error as e:
return e
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:/Users/Administrator/Desktop/Git/projectpp/Electricity.py", line 389, in submit
self.update_household_information_with_id(parent, id_ent.get())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 3043, in get
return self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".!toplevel.!entry"
基本上,它尝试获取住户id
,然后调用住户id
,并将其传递给另一个函数self。使用同一类中定义的
更新住户信息,如下所示:
def update_household_information(self, parent):
try:
sub = tk.Toplevel(master=parent)
sub.title(f"Update household")
sub.resizable(height=False, width=False)
# Create text entries
id_ent = tk.Entry(sub, width=30)
id_ent.grid(row=0, column=1, padx=20)
# Create text labels
id_lbl = tk.Label(sub, text="Enter the ID of the household to be updated: ")
id_lbl.grid(row=0, column=0)
# Create Submit Function for Database:
def submit():
try:
# Create the electric.db or connect to it
conn = sqlite3.connect('electric.db')
# Create cursor
c = conn.cursor()
c.execute("PRAGMA foreign_keys = ON")
# Check if area_id exist:
c.execute("""SELECT id FROM households""")
results = c.fetchall()
household_ids = []
for result in results:
household_ids.append(result[0])
if int(id_ent.get()) not in household_ids:
messagebox.showerror(message="Household ID does not exist")
sub.destroy()
self.update_household_information(parent)
else:
sub.destroy()
self.update_household_information_with_id(parent, id_ent.get())
except Error as e:
return e
# Create Submit Button
submit_btn = tk.Button(sub, text="Submit", command=submit)
submit_btn.grid(row=5, column=0, columnspan=2, ipadx=5, ipady=5, pady=5, padx=5)
except Error as e:
return e
def update_household_information_with_id(self, parent, household_id):
try:
sub = tk.Toplevel(master=parent)
sub.title(f"Update household {household_id}")
sub.resizable(height=False, width=False)
# Create text entries
name_ent = tk.Entry(sub, width=30)
name_ent.grid(row=1, column=1, padx=20)
address_ent = tk.Entry(sub, width=30)
address_ent.grid(row=2, column=1, padx=20)
phone_number_ent = tk.Entry(sub, width=30)
phone_number_ent.grid(row=3, column=1, padx=20)
area_ent = tk.Entry(sub, width=30)
area_ent.grid(row=4, column=1, padx=20)
# Create text labels
id_lbl = tk.Label(sub, text=f"Updating Household {household_id}: ")
id_lbl.grid(row=0, column=0, columnspan=2)
name_lbl = tk.Label(sub, text="Owner's name: ")
name_lbl.grid(row=1, column=0)
address_lbl = tk.Label(sub, text="Address: ")
address_lbl.grid(row=2, column=0)
phone_number_lbl = tk.Label(sub, text="Phone number: ")
phone_number_lbl.grid(row=3, column=0)
area_lbl = tk.Label(sub, text="Area: ")
area_lbl.grid(row=4, column=0)
# Create Submit Function for Database:
def submit():
try:
# Create the electric.db or connect to it
conn = sqlite3.connect('electric.db')
# Create cursor
c = conn.cursor()
c.execute("PRAGMA foreign_keys = ON")
# Check if area_id exist:
c.execute("""SELECT id FROM areas""")
results = c.fetchall()
area_ids = []
for result in results:
area_ids.append(result[0])
if int(area_ent.get()) not in area_ids:
messagebox.showerror(message="Area ID does not exist")
sub.destroy()
self.update_household_information_with_id(parent)
# Insert into table
c.execute(
f"""UPDATE households
SET owner_name = :owner_name,
address = :address,
phone_number = :phone_number,
area_id = :area_id
WHERE id = {household_id}""",
{
'owner_name': name_ent.get(),
'address': address_ent.get(),
'phone_number': phone_number_ent.get(),
'area_id': area_ent.get()
}
)
# Commit changes
conn.commit()
# Close connection
conn.close()
except Error as e:
return e
# Create Submit Button
submit_btn = tk.Button(sub, text="Update Record to Database", command=submit)
submit_btn.grid(row=5, column=0, columnspan=2, ipadx=5, ipady=5, pady=5, padx=5)
except Error as e:
return e
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:/Users/Administrator/Desktop/Git/projectpp/Electricity.py", line 389, in submit
self.update_household_information_with_id(parent, id_ent.get())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 3043, in get
return self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".!toplevel.!entry"
这将使用提供的id更新住户信息。
但是,当我尝试运行第一个函数时,出现了如下错误:
def update_household_information(self, parent):
try:
sub = tk.Toplevel(master=parent)
sub.title(f"Update household")
sub.resizable(height=False, width=False)
# Create text entries
id_ent = tk.Entry(sub, width=30)
id_ent.grid(row=0, column=1, padx=20)
# Create text labels
id_lbl = tk.Label(sub, text="Enter the ID of the household to be updated: ")
id_lbl.grid(row=0, column=0)
# Create Submit Function for Database:
def submit():
try:
# Create the electric.db or connect to it
conn = sqlite3.connect('electric.db')
# Create cursor
c = conn.cursor()
c.execute("PRAGMA foreign_keys = ON")
# Check if area_id exist:
c.execute("""SELECT id FROM households""")
results = c.fetchall()
household_ids = []
for result in results:
household_ids.append(result[0])
if int(id_ent.get()) not in household_ids:
messagebox.showerror(message="Household ID does not exist")
sub.destroy()
self.update_household_information(parent)
else:
sub.destroy()
self.update_household_information_with_id(parent, id_ent.get())
except Error as e:
return e
# Create Submit Button
submit_btn = tk.Button(sub, text="Submit", command=submit)
submit_btn.grid(row=5, column=0, columnspan=2, ipadx=5, ipady=5, pady=5, padx=5)
except Error as e:
return e
def update_household_information_with_id(self, parent, household_id):
try:
sub = tk.Toplevel(master=parent)
sub.title(f"Update household {household_id}")
sub.resizable(height=False, width=False)
# Create text entries
name_ent = tk.Entry(sub, width=30)
name_ent.grid(row=1, column=1, padx=20)
address_ent = tk.Entry(sub, width=30)
address_ent.grid(row=2, column=1, padx=20)
phone_number_ent = tk.Entry(sub, width=30)
phone_number_ent.grid(row=3, column=1, padx=20)
area_ent = tk.Entry(sub, width=30)
area_ent.grid(row=4, column=1, padx=20)
# Create text labels
id_lbl = tk.Label(sub, text=f"Updating Household {household_id}: ")
id_lbl.grid(row=0, column=0, columnspan=2)
name_lbl = tk.Label(sub, text="Owner's name: ")
name_lbl.grid(row=1, column=0)
address_lbl = tk.Label(sub, text="Address: ")
address_lbl.grid(row=2, column=0)
phone_number_lbl = tk.Label(sub, text="Phone number: ")
phone_number_lbl.grid(row=3, column=0)
area_lbl = tk.Label(sub, text="Area: ")
area_lbl.grid(row=4, column=0)
# Create Submit Function for Database:
def submit():
try:
# Create the electric.db or connect to it
conn = sqlite3.connect('electric.db')
# Create cursor
c = conn.cursor()
c.execute("PRAGMA foreign_keys = ON")
# Check if area_id exist:
c.execute("""SELECT id FROM areas""")
results = c.fetchall()
area_ids = []
for result in results:
area_ids.append(result[0])
if int(area_ent.get()) not in area_ids:
messagebox.showerror(message="Area ID does not exist")
sub.destroy()
self.update_household_information_with_id(parent)
# Insert into table
c.execute(
f"""UPDATE households
SET owner_name = :owner_name,
address = :address,
phone_number = :phone_number,
area_id = :area_id
WHERE id = {household_id}""",
{
'owner_name': name_ent.get(),
'address': address_ent.get(),
'phone_number': phone_number_ent.get(),
'area_id': area_ent.get()
}
)
# Commit changes
conn.commit()
# Close connection
conn.close()
except Error as e:
return e
# Create Submit Button
submit_btn = tk.Button(sub, text="Update Record to Database", command=submit)
submit_btn.grid(row=5, column=0, columnspan=2, ipadx=5, ipady=5, pady=5, padx=5)
except Error as e:
return e
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:/Users/Administrator/Desktop/Git/projectpp/Electricity.py", line 389, in submit
self.update_household_information_with_id(parent, id_ent.get())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 3043, in get
return self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".!toplevel.!entry"
你能告诉我为什么会出现这个错误以及如何修复它吗?这个错误要么意味着你破坏了这个小部件,要么是在
Tk
的不同实例中创建的。如果没有一个我们可以运行的合适的程序,就很难更加具体。您尝试获取id\u ent
,它是id\u ent=tk.Entry(sub..)
因此它是sub=tk.Toplevel(master=parent)
的子级,但在执行sub.destroy()之前有一行
-因此sub
不再存在,它还删除了它的子项,如id\u ent
。您必须在sub.destroy()之前获取此值id\u ent.get()