Combobox doesn't display the populated values

被刻印的时光 ゝ 提交于 2021-01-29 11:11:10

问题


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']

回答1:


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.



来源:https://stackoverflow.com/questions/65522044/combobox-doesnt-display-the-populated-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!