【发布时间】:2021-12-30 19:11:56
【问题描述】:
我正在尝试使用 Python 在 PyQt5 中的 QComboBox 中添加项目。我在从每行的 SQL 查询中添加数据时遇到问题。
cursor = cnx.cursor()
query = "SELECT buyerID, lastName, firstName, middleName FROM buyer ORDER BY id DESC LIMIT 5"
cursor.execute(query)
data = cursor.fetchall()
item = list()
for a, b, c, d in data:
row = "{} | {}, {} {}".format(a, b, c, d)
item.append(row)
self.customerID.addItem(str(item))
这导致只有一个项目添加到组合框中:
100000 | lastName, firstName middleName, 100000 | lastName, firstName middleName, 100000 | lastName, firstName middleName...etc.
我想在ComboBox中发生的事情是这样的(在Combo Box中一共添加5个项目)
100001 | lastName, firstName middleName
100002 | lastName, firstName middleName
100003 | lastName, firstName middleName
100004 | lastName, firstName middleName
100005 | lastName, firstName middleName
编辑:
cursor = cnx.cursor()
query = "SELECT buyerID, lastName, firstName, middleName FROM buyer ORDER BY id DESC LIMIT 5"
cursor.execute(query)
data = cursor.fetchall()
item = list()
for a, b, c, d in data:
row = "{} | {}, {} {}".format(a, b, c, d)
item.append(row)
self.customerID.addItem(str(item)) <------- I just moved this line of code into the FOR loop statement to add the item per loop.
同样的问题:
追加的Item依然是所有数据行归一。
【问题讨论】:
-
只需使用
addItems(item)(注意最后的“s”)。
标签: python python-3.x pyqt5 mysql-python qcombobox