Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

sqlite - Combobox doesn't display the populated values

I created a combobox to show a list of values out of a sqlite database. If I sent the values to the combobox, the list will shown. The problem is, that the field will not filled with the first value and stay empty until I select on item of out of the drop menu. Could I set a value to be displayed directly?

Here my code snippet:

    self.e_business = ttk.Combobox(address_frame, width = 40)
    self.e_business.grid(row=3, column=1, columnspan=2, padx=(5,20), pady=(15,5), sticky='WE')

The function which send the values:

def show_name_search(self, event):
        ...
        widget = event.widget
        selection = widget.curselection()
        indName = widget.get(selection[0])
        print(indName)
        print("selktierter Wert: {}".format(indName))
        
        self.realName.set(indName)
        
        connection = sqlite3.connect(select_connect_db)
        print('Database connected.')
        with connection: 
            cursor = connection.cursor()
            cursor.execute("SELECT number, type, prio, id, uniqueid FROM numbers WHERE realName='"+indName+"';")
            data = cursor.fetchall()
            print(data)
            cache = []
            for row in data:
                cache.append(row[0])                    
                self.e_business['values'] = cache

The cache list looks like:

CACHE: ['081191310912', '071109131090', '01754123353']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Define the list in the __init__() and then populate inside the function, like:

def __init__(self):
    self.cache = []
... # Rest of codes

def show_name_search(self,event):
....# Rest of codes
    for row in data:
        self.cache.append(row[0])                    

    self.e_business['values'] = self.cache # Set the value to the new list
    self.e_business.current(0) # Set the first item of the list as current item

To set the item to the current item, you can use current() and the index of the item to be set first. Note that the indentation levels will change according to your class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...