代码
'''
对QListWidget列表进行数据绑定
'''
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(397, 190)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(0, 0, 401, 192))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "对QListWidget列表进行数据绑定"))
from collections import OrderedDict
dict=OrderedDict({'诸葛维奇':'格雷格·波波维奇','石佛':'蒂姆·邓肯','妖刀':'马努·吉诺比利',
'法国跑车':'托尼·帕克','海军上将':'大卫·罗宾逊','冰人':'乔治·格文',
'三叔':'布鲁斯·鲍文','小将军':'埃弗里·约翰逊','超人':'肖恩·埃利奥特'})
for key,value in dict.items():
self.item = QtWidgets.QListWidgetItem(self.listWidget)
self.item.setText(key+'——'+value)
self.item.setToolTip(value)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
效果
