Pyqt5--解析json并将内容显示在tableWidget

在开始写本博客的案例之前,梳理下功能点和知识点,方便查找和理解。
功能介绍:
1.生成json数据;
2.解析json数据并将内容显示在tableWidget中;
3.获取lineEdit中输入的内容,并将其显示在tableWidget中。
知识点梳理:
1.json
json是一种易于阅读和编写的数据交换格式。
json.dumps()函数是将字典转化为字符串------jsonStr = json.dumps(strData)
json.loads()函数是将字符串转化为字典--------res = json.loads(jsonStr)
2.Qt Designer界面设计--比较简单
控件,信号和槽的使用可以参考https://blog.csdn.net/maidu_xbd/article/details/85696510
3.tableWidget
【Pyqt5--解析json并将内容显示在tableWidget】self.ui.tableWidget.setColumnCount(5) # 设置表格的列数
self.ui.tableWidget.setRowCount(100) #设置表格的行数
mTitle = QTableWidgetItem(item['name']) #显示数据
mTitle.setTextAlignment(Qt.AlignCenter) # 设置文字显示居中
mTitle.setFlags(Qt.ItemIsEnabled) # 设置表格不可编辑模式
4.Label
self.ui.userName.setText("当前用户为:" + userName) # 设置Label显示内容
案例如下:
1.生成json数据如下:

import jsonstrData = https://www.it610.com/article/{'status':200, 'message':'获取信息成功!', 'data':{ 'userInfo':{ 'name':"麦嘟小布丁", 'sex':'女' }, 'meetInfo':[ {'id': 110, 'name': '人工智能技术会议', 'meet_date': '2019-01-08', 'meet_time': '16:30:00', 'meet_address': '会议室A'}, {'id': 221, 'name': '学习交流会', 'meet_date': '2018-12-19', 'meet_time': '14:30:00', 'meet_address': '会议室B'}, {'id': 226, 'name': '神经网络知识分享会', 'meet_date': '2018-12-19', 'meet_time': '09:00:00', 'meet_address': '会议室C'} ] } }#定义接口,用来获取json数据 def getMydata(): return json.dumps(strData)if __name__ == "__main__": # json.dumps()函数是将字典转化为字符串 jsonStr = json.dumps(strData) #print(jsonStr) # json.loads()函数是将字符串转化为字典 res = json.loads(jsonStr) print(type(res),res)

2.Qt designer完成界面设计meetList.ui
为“添加”按钮添加信号和自定义槽函数。
Pyqt5--解析json并将内容显示在tableWidget
文章图片

编译meetList.ui为Ui_meetList.py
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'c:\Users\lenovo\Desktop\解析Json\meetList.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost!from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(559, 473) MainWindow.setStyleSheet("#MainWindow{background-color: rgb(170, 170, 255); }\n" "") self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.tableWidget = QtWidgets.QTableWidget(self.centralwidget) self.tableWidget.setGeometry(QtCore.QRect(30, 60, 491, 211)) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(5) self.tableWidget.setRowCount(0) item = QtWidgets.QTableWidgetItem() item.setTextAlignment(QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() item.setTextAlignment(QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() item.setTextAlignment(QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() item.setTextAlignment(QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(3, item) item = QtWidgets.QTableWidgetItem() item.setTextAlignment(QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(4, item) self.userName = QtWidgets.QLabel(self.centralwidget) self.userName.setGeometry(QtCore.QRect(400, 30, 141, 20)) self.userName.setObjectName("userName") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(30, 20, 431, 20)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(14) font.setBold(True) font.setWeight(75) self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(50, 300, 181, 16)) font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) self.label_3.setFont(font) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(60, 340, 91, 16)) font = QtGui.QFont() font.setPointSize(11) self.label_4.setFont(font) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(280, 340, 101, 16)) font = QtGui.QFont() font.setPointSize(11) self.label_5.setFont(font) self.label_5.setObjectName("label_5") self.label_6 = QtWidgets.QLabel(self.centralwidget) self.label_6.setGeometry(QtCore.QRect(45, 379, 91, 16)) font = QtGui.QFont() font.setPointSize(11) self.label_6.setFont(font) self.label_6.setObjectName("label_6") self.label_7 = QtWidgets.QLabel(self.centralwidget) self.label_7.setGeometry(QtCore.QRect(280, 380, 71, 16)) font = QtGui.QFont() font.setPointSize(11) self.label_7.setFont(font) self.label_7.setObjectName("label_7") self.addMeetBtn = QtWidgets.QPushButton(self.centralwidget) self.addMeetBtn.setGeometry(QtCore.QRect(450, 300, 75, 23)) self.addMeetBtn.setObjectName("addMeetBtn") self.meetId = QtWidgets.QLineEdit(self.centralwidget) self.meetId.setGeometry(QtCore.QRect(120, 340, 151, 20)) self.meetId.setObjectName("meetId") self.meetTitle = QtWidgets.QLineEdit(self.centralwidget) self.meetTitle.setGeometry(QtCore.QRect(360, 340, 171, 20)) self.meetTitle.setObjectName("meetTitle") self.meetTime = QtWidgets.QLineEdit(self.centralwidget) self.meetTime.setGeometry(QtCore.QRect(120, 380, 151, 20)) self.meetTime.setObjectName("meetTime") self.meetAddress = QtWidgets.QLineEdit(self.centralwidget) self.meetAddress.setGeometry(QtCore.QRect(360, 380, 171, 20)) self.meetAddress.setObjectName("meetAddress") MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow) self.addMeetBtn.clicked.connect(MainWindow.addMeetBtn_clicked) QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "会议id")) item = self.tableWidget.horizontalHeaderItem(1) item.setText(_translate("MainWindow", "会议名称")) item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("MainWindow", "会议时间")) item = self.tableWidget.horizontalHeaderItem(3) item.setText(_translate("MainWindow", "会议地点")) item = self.tableWidget.horizontalHeaderItem(4) item.setText(_translate("MainWindow", "备注")) self.userName.setText(_translate("MainWindow", "当前用户为:张三")) self.label_2.setText(_translate("MainWindow", "会议列表信息")) self.label_3.setText(_translate("MainWindow", "新增会议信息")) self.label_4.setText(_translate("MainWindow", "会议id:")) self.label_5.setText(_translate("MainWindow", "会议名称:")) self.label_6.setText(_translate("MainWindow", "会议时间:")) self.label_7.setText(_translate("MainWindow", "会议地点:")) self.addMeetBtn.setText(_translate("MainWindow", "添加"))

3.解析json,将内容显示在tableWidget中 ------ def loadMeetInfo(self)
定义addMeetBtn_clicked()函数,获取LineEdit内容并将内容显示在tableWidget中,每点击添加按钮一次,往当前行的下一行插入数据。
from meetInfo import getMydata from Ui_meetList import Ui_MainWindow from PyQt5.QtWidgets import * import sys import json from PyQt5.QtCore import *class MainWin(QMainWindow):def __init__(self): super(MainWin, self).__init__() print("mainwin is called!") self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.tableWidget.setColumnCount(5)# 设置表格的列数 # self.ui.tableWidget.setRowCount(100)#设置表格的行数 self.loadMeetInfo() print(f"current rows:{self.ui.tableWidget.currentRow()}")def loadMeetInfo(self): data = https://www.it610.com/article/getMydata() # 获取信息列表 meetData = json.loads(data)['data'] # 获取用户信息 userInfo = meetData['userInfo'] userName = userInfo['name'] self.ui.userName.setText("当前用户为:" + userName)# 获取会议详情 meetInfo = meetData['meetInfo'] # index=0 for i, item in enumerate(meetInfo): # index+=1 # str(item['id'])将整型转换为字符串 self.ui.tableWidget.setRowCount(self.ui.tableWidget.rowCount() + 1) print(self.ui.tableWidget.rowCount()) mId = QTableWidgetItem(str(item['id'])) mId.setTextAlignment(Qt.AlignCenter)# 设置文字显示居中 mId.setFlags(Qt.ItemIsEnabled)# 设置表格不可编辑模式 self.ui.tableWidget.setItem( self.ui.tableWidget.rowCount() - 1, 0, mId)mTitle = QTableWidgetItem(item['name']) mTitle.setTextAlignment(Qt.AlignCenter)# 设置文字显示居中 mTitle.setFlags(Qt.ItemIsEnabled)# 设置表格不可编辑模式 self.ui.tableWidget.setItem( self.ui.tableWidget.rowCount() - 1, 1, mTitle)mTime = QTableWidgetItem(item['meet_time']) mTime.setTextAlignment(Qt.AlignCenter)# 设置文字显示居中 mTime.setFlags(Qt.ItemIsEnabled)# 设置表格不可编辑模式 self.ui.tableWidget.setItem( self.ui.tableWidget.rowCount() - 1, 2, mTime)mAddress = QTableWidgetItem(item['meet_address']) mAddress.setTextAlignment(Qt.AlignCenter)# 设置文字显示居中 mAddress.setFlags(Qt.ItemIsEnabled)# 设置表格不可编辑模式 self.ui.tableWidget.setItem( self.ui.tableWidget.rowCount() - 1, 3, mAddress) # return indexdef addMeetBtn_clicked(self): i = self.ui.tableWidget.rowCount() i += 1 self.ui.tableWidget.setRowCount(i) meetId = self.ui.meetId.text() # print(type(meetId)) mId = QTableWidgetItem(meetId) mId.setTextAlignment(Qt.AlignCenter)# 设置文字显示居中 self.ui.tableWidget.setItem(i - 1, 0, mId)meetTitle = self.ui.meetTitle.text() mTitle = QTableWidgetItem(meetTitle) mTitle.setTextAlignment(Qt.AlignCenter)# 设置文字显示居中 self.ui.tableWidget.setItem(i - 1, 1, mTitle)meetTime = self.ui.meetTime.text() mTime = QTableWidgetItem(meetTime) mTime.setTextAlignment(Qt.AlignCenter)# 设置文字显示居中 self.ui.tableWidget.setItem(i - 1, 2, mTime)meetAddress = self.ui.meetAddress.text() mAddress = QTableWidgetItem(meetAddress) mAddress.setTextAlignment(Qt.AlignCenter)# 设置文字显示居中 self.ui.tableWidget.setItem(i - 1, 3, mAddress)# LineEdit内容清除 self.ui.meetId.setText("") self.ui.meetTitle.setText("") self.ui.meetTime.setText("") self.ui.meetAddress.setText("")if __name__ == "__main__": app = QApplication(sys.argv) main = MainWin() # 显示主界面 main.show()sys.exit(app.exec_())

4.结果展示
前3条会议信息为从解析json获得,第4条会议信息为通过新增会议信息添加。
Pyqt5--解析json并将内容显示在tableWidget
文章图片
Pyqt5--解析json并将内容显示在tableWidget
文章图片

    推荐阅读