想让执行完程序后自动关闭窗口,而不用点击右上角叉叉的方法是self.close(),具体应用还是以treewidget为例。
前面我们写了一个treewidget的界面,并且实现了界面代码分离,具体实现不多讲了,直接上代码:
treeview4.py
# -*- coding: utf-8 -*-
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"))
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_())
具体实现效果就是先初始化树结构,等待一秒后改变其中一个的值, 在等待一秒退出窗口。
效果图:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章