zoukankan      html  css  js  c++  java
  • Python基础之PyQt5关闭界面

    想让执行完程序后自动关闭窗口,而不用点击右上角叉叉的方法是self.close(),具体应用还是以treewidget为例。

    前面我们写了一个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_run.py

    from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
    from PyQt5 import QtCore, QtGui, QtWidgets
    import treeview4
    import treeview_data
    import sys
    import threading
    import time
    
    
    class MainCode(QMainWindow, treeview4.Ui_MainWindow):
        list_result_update = QtCore.pyqtSignal([int, int, str])
    
        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()
            # 用列表触发槽函数
            self.list_result_update.connect(self.modified_treewidget)
            # 线程函数
            self.run()
    
        def run(self):
            th = threading.Thread(target=self.go_update)
            th.setDaemon(True)
            th.start()
    
        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))
    
        def modified_treewidget(self, num_one, num_two, str_one):
            _translate = QtCore.QCoreApplication.translate
            brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
            brush.setStyle(QtCore.Qt.SolidPattern)
            self.treeWidget.topLevelItem(num_one).child(num_two).setBackground(1, brush)
            self.treeWidget.topLevelItem(num_one).child(num_two).setText(1, _translate(
                "MainWindow",
                str_one))
    
        def go_update(self):
            time.sleep(1)
            # 更新treeview
            self.list_result_update.emit(1, 1, "aaaaaaa")
            time.sleep(1)
            # 关闭窗口
            self.close()
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        md = MainCode()
        md.show()
        app.exec_()
        # sys.exit(app.exec_())
    View Code

    具体实现效果就是先初始化树结构,等待一秒后改变其中一个的值, 在等待一秒退出窗口。

    效果图:

  • 相关阅读:
    linux编译C
    Windows下ARP协议的工作原理简介
    窥探QQ基础数据库架构演变史
    JAVA培训—线程同步--卖票问题
    SQLite、MySQL和PostgreSQL 三种关系数据库哪个好?
    再论谭浩强《C语言程序设计》
    JPG加入RAR文件原理详解
    如何区分一个程序员是“老手“还是“新手“?
    AutoITx3.DLL所有函数及说明
    windows索引服务
  • 原文地址:https://www.cnblogs.com/smart-zihan/p/12187982.html
Copyright © 2011-2022 走看看