Search code examples
pythonvariablescombobox

Trying to pass a variable to CTkComboBox from a sqlite query


I'm a relative newbie with regard to Python, however, I wanted to create a custom budgeting program to gain some more practice and exposure. I have the following class so far -

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)

Once a new_account_entry is entered, I'm calling the following function that checks to see if the name is an existing one in an AccountName table:

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()  

So, now is where I'm struggling. I call the update_account_types() function, which pulls the list of AccountTypes from the AccountType table:

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

so now my self.account_types variable has values in my class above, however, when I click on the new_account_type_combobox, the only value I see is CTkComboBox

I was able to get the combobox to populate correctly before I added the add_account function to check if an account_name already existed in the database.

I can print out the account_types to the screen, however, they are not populating the combo box.


Solution

  • Documentation: CTkComboBox

    GUI will not add it automatically to widget. You have to use .configure(values=...)

    self.new_account_type_combobox.configure(values=["new value 1", "new value 2"])  
    

    In your code it can means values=self.account_types

    self.new_account_type_combobox.configure(values=self.account_types)  
    

    And you have to do it always when you get new values from database.
    So you have to do it in update_account_types()