Python基础之实现界面和代码分离

时间:2024-02-22 19:31:02

第一步:用QT Designer画一个TreeWidget,存为treeview4.ui,这个处理前面TreeWidget那一节讲过,这里不细讲

treeview4.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file \'treeview4.ui\'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
        self.treeWidget.setGeometry(QtCore.QRect(150, 80, 411, 341))
        self.treeWidget.setObjectName("treeWidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.treeWidget.headerItem().setText(0, _translate("MainWindow", "Key"))
        self.treeWidget.headerItem().setText(1, _translate("MainWindow", "Value"))
View Code

 

第二步:代码分离。新建一个treeview4_test.py

from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5 import QtCore, QtGui, QtWidgets
import treeview4
import treeview_data
import sys


class MainCode(QMainWindow, treeview4.Ui_MainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        treeview4.Ui_MainWindow.__init__(self)
        self.setupUi(self)
        # 设置列宽
        self.treeWidget.setColumnWidth(0, 200)

        _translate = QtCore.QCoreApplication.translate
        self.initial_tree_view(_translate)
        self.treeWidget.expandAll()

    def initial_tree_view(self, _translate):
        list_objects = []
        test1 = treeview_data.DataCollection()
        test1.set_module({"Test1": "waiting"})
        test1.add_item({"a1": "waiting"})
        test1.add_item({"b1": "waiting"})
        test1.add_item({"c1": "waiting"})
        list_objects.append(test1)

        test2 = treeview_data.DataCollection()
        test2.set_module({"Test2": "waiting"})
        test2.add_item({"a2": "waiting"})
        test2.add_item({"b2": "waiting"})
        test2.add_item({"c2": "waiting"})
        test2.add_item({"d2": "waiting"})
        list_objects.append(test2)

        test3 = treeview_data.DataCollection()
        test3.set_module({"Test3": "waiting"})
        test3.add_item({"a2": "waiting"})
        test3.add_item({"b2": "waiting"})
        test3.add_item({"c2": "waiting"})
        test3.add_item({"d2": "waiting"})
        list_objects.append(test3)

        for object_num, object_module in enumerate(list_objects):
            item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget)
            brush = QtGui.QBrush(QtGui.QColor(176, 165, 172))
            brush.setStyle(QtCore.Qt.SolidPattern)
            item_0.setBackground(0, brush)
            for module_name, module_result in object_module.module.items():
                brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
                brush.setStyle(QtCore.Qt.SolidPattern)
                item_0.setBackground(1, brush)
                self.treeWidget.topLevelItem(object_num).setText(0, _translate("MainWindow", module_name))
                self.treeWidget.topLevelItem(object_num).setText(1, _translate("MainWindow", module_result))
            for item_num, item_dic in enumerate(object_module.items):
                for item_name, item_result in item_dic.items():
                    item_1 = QtWidgets.QTreeWidgetItem(item_0)
                    self.treeWidget.topLevelItem(object_num).child(item_num).setText(0, _translate("MainWindow",
                                                                                                   item_name))
                    self.treeWidget.topLevelItem(object_num).child(item_num).setText(1, _translate("MainWindow",
                                                                                                   item_result))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    md = MainCode()
    md.show()
    app.exec_()
    # sys.exit(app.exec_())
View Code

 

其实这些代码和之前讲TreeWidget那一节的代码是一样的,不过进行了分离处理而已。