Python脚手架系列-PyQt5

记录PyQt模块使用中的一些常常复用的代码

其他

导入界面
import sys

from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow
from UI.MainWindow import Ui_MainWindow  # 导入UI界面的类以供继承


class MyApp(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowTitle('设置窗口title')
        self.setWindowIcon(QIcon('res/news.png'))  # 设置界面左上角图标
        self.initialize()

    def initialize(self):
        # 此处绑定事件等
        pass


if __name__ == "__main__":
    app = QApplication(sys.argv)
    myapp = MyApp()
    myapp.show()
    sys.exit(app.exec_())


.ui文件转化成.py文件
import os
import os.path


def listUiFile():
    list = []
    files = os.listdir("./")
    for filename in files:
        if os.path.splitext(filename)[1] == '.ui':
            list.append(filename)
    return list


def transPyFile(filename):
    return os.path.splitext(filename)[0] + '.py'


def runMain():
    list = listUiFile()
    for uifile in list:
        pyfile = transPyFile(uifile)
        cmd = 'pyuic5 -o {pyfile} {uifile}'.format(pyfile=pyfile, uifile=uifile)
        os.system(cmd)


if __name__ == "__main__":
    runMain()

把这段代码放到和ui文件同级目录下,然后直接运行即可把.ui文件自动编译成py文件

  • 文件结构如下
    在这里插入图片描述

对象操作

QMessageBox操作
  • 用于风险操作的二次确认
questionResult = QMessageBox.question(None, "询问框title", "是否修改XXXXX修改后不可还原!", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if questionResult == QMessageBox.No:
	return None  # 这里可以改成任何取消操作的代码逻辑,在函数中则用return None

在这里插入图片描述

  • 其他常规提示
QMessageBox.information(self, '信息框title', '这是信息内容')
QMessageBox.warning(self, '警告框title', '这是警告内容')
QMessageBox.critical(self, '错误框title', '这是错误内容')

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

JSON格式渲染到tableWidget
import sys

from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem


def fill_table(tableWidget, jsonData):
    # 清空表格
    tableWidget.clearContents()
    tableWidget.setRowCount(0)
    # 设置表头
    header = jsonData[0].keys()
    tableWidget.setColumnCount(len(header))
    tableWidget.setHorizontalHeaderLabels(header)

    # 遍历数据并将其添加到表中
    for row_index, row_data in enumerate(jsonData):
        tableWidget.insertRow(row_index)
        for col_index, col_name in enumerate(header):
            item = QTableWidgetItem(str(row_data[col_name]))
            tableWidget.setItem(row_index, col_index, item)


if __name__ == "__main__":
    json_data = [
        {"BV": "BV1", "播放量": 1000, "点赞": 500, "评论": 200, "转发": 100, "投币": 50, "收藏": 30},
        {"BV": "BV2", "播放量": 1500, "点赞": 700, "评论": 300, "转发": 150, "投币": 70, "收藏": 40},
        {"BV": "BV3", "播放量": 2000, "点赞": 900, "评论": 400, "转发": 200, "投币": 90, "收藏": 50}
    ]

    app = QApplication(sys.argv)
    tableWidget = QTableWidget()
    tableWidget.show()

    fill_table(tableWidget, json_data)

    sys.exit(app.exec_())

注意使用的时候JSON数据格式必须统一,否则可能产生报错

  • 效果
    在这里插入图片描述

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-06-09 22:14:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-09 22:14:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-09 22:14:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-09 22:14:02       20 阅读

热门阅读

  1. Flutter 常见报错记录

    2024-06-09 22:14:02       13 阅读
  2. 解决更新Android Studio后下载Gradle超时

    2024-06-09 22:14:02       11 阅读
  3. 给自己Linux搞个『回收站』,防止文件误删除

    2024-06-09 22:14:02       12 阅读
  4. 前端通用样式调整方法(整理版)

    2024-06-09 22:14:02       9 阅读
  5. web 前端开发培训:深入探索与实战应用

    2024-06-09 22:14:02       14 阅读
  6. 线程+线程池+锁

    2024-06-09 22:14:02       11 阅读
  7. 多维vector定义

    2024-06-09 22:14:02       10 阅读