我对 Python 来说是一个相对新手,但是,我想创建一个自定义预算程序以获得更多练习和接触。到目前为止我有以下课程 -
class NewAccountEntryFrame(CTkFrame):
def __init__(self, master, cursor):
super().__init__(master=master)
self.cursor = cursor
self.account_types = []
print("self.account_types", self.account_types) #debugging line to make sure the self.account_types is empty at this point
self.frame_label = CTkLabel(master=self, text="New Account Entry", font=("Arial", 30))
self.frame_label.pack(pady=10)
self.new_account_label = CTkLabel(master=self, text="Enter New Account Name:")
self.new_account_label.pack(pady=5)
self.new_account_entry = CTkEntry(master=self, width=30)
self.new_account_entry.pack(pady=5)
self.new_account_entry.bind("<Return>", self.add_account)
self.new_account_type_label = CTkLabel(master=self, text="Enter New Account Type:")
self.new_account_type_label.pack(pady=5)
self.new_account_type_combobox = CTkComboBox(master=self, width=30)
self.new_account_type_combobox.pack(pady=5)
self.new_account_type_combobox.bind("<<ComboboxSelected>>", self.on_click)
self.new_account_add_button = CTkButton(master=self, text="Add")
self.new_account_add_button.pack(pady=10)
输入 new_account_entry 后,我将调用以下函数来检查该名称是否是
AccountName
表中的现有名称:
def add_account(self, event):
new_account_name = self.new_account_entry.get()
if not new_account_name:
messagebox.showerror("Error", "Please enter an account name.")
return
self.cursor.execute("SELECT * FROM Account WHERE AccountName = ?", (new_account_name,))
existing_account = self.cursor.fetchone()
if existing_account:
messagebox.showerror("Error", "Account already exists.")
else:
messagebox.showinfo("Success", "Account is not in the database. Please select an account type.")
self.update_account_types()
所以,现在就是我挣扎的地方。我调用
update_account_types()
函数,该函数从 AccountTypes
表中提取 AccountType
列表:
def update_account_types(self):
print("Fetching Account Types...")
account_types = [row[0] for row in self.cursor.execute("SELECT AccountType FROM AccountType ORDER BY AccountType")]
print ("account types", account_types) #Prints out the account_types from the database as expected
self.account_types = account_types
print("self.account_types", self.account_types) #prints out the account_types from the database as expected
所以现在我的
self.account_types
变量在上面的类中有值,但是,当我单击 new_account_type_combobox
时,我看到的唯一值是 CTkComboBox
在添加
combobox
函数来检查数据库中是否已存在 add_account
之前,我能够正确填充 account_name
。
我可以将
account_types
打印到屏幕上,但是,它们没有填充组合框。
文档:CTkComboBox
GUI 不会自动将其添加到小部件中。你必须使用
.configure(values=...)
self.new_account_type_combobox.configure(values=["new value 1", "new value 2"])
在您的代码中它可能意味着
values=self.account_types
self.new_account_type_combobox.configure(values=self.account_types)