组合框不显示填充值 [英] Combobox doesn't display the populated values

查看:107
本文介绍了组合框不显示填充值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个组合框来显示来自 sqlite 数据库的值列表.如果我将值发送到组合框,列表将显示.问题是,该字段不会填充第一个值并保持为空,直到我从下拉菜单中选择项目.我可以设置一个值直接显示吗?

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?

这是我的代码片段:

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

发送值的函数:

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

缓存列表如下所示:

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

推荐答案

__init__() 中定义列表,然后填充到函数内部,如:

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

要将item设置为当前item,可以使用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.

这篇关于组合框不显示填充值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆