zoukankan      html  css  js  c++  java
  • Python基础之实现界面和代码分离

    第一步:用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那一节的代码是一样的,不过进行了分离处理而已。

  • 相关阅读:
    (转)社会青年应如何自学英语?
    WEB标准学习路程之"CSS":13.声明,单位
    8大致命生活毛病(转)
    VS2008小Bug??
    【细嚼慢咽大数据】第一章——数据挖掘基本概念,邦弗朗尼原理,IF.IDF指标,哈希函数
    【Linux操作系统分析】定时测量——RTC,TSC,PIT,jiffies,计时体系结构,延迟函数
    【Git】Git上传本地项目的命令以及nonfastforward updates were rejected的解决办法
    【Linux操作系统分析】Ubuntu12.04内核升级和添加系统调用
    【折腾ubuntu】Ubuntu12.04安装windows版本的福昕阅读器
    【二】zTree checkbox
  • 原文地址:https://www.cnblogs.com/smart-zihan/p/12180051.html
Copyright © 2011-2022 走看看