站长网_站长创业_站长主页_站长之家_易采站长站

会员投稿 投稿指南 站长资讯通告: PyQt5事件处理之定时在控件上显示信息的代码
搜索:
您的位置: 主页 > 教程 > 脚本教程 > python > » 正文

PyQt5事件处理之定时在控件上显示信息的代码

来源: 易采站长站

有时候为了体现延时效果,或者是多事件处理,需要在窗口的文本编辑框或者表格等控件中,延迟几秒或每隔几秒显示输出一段数据,又或者可以说是每隔几秒执行下一行代码!要实现这种效果,关键的两个方法就是time.sleep()processEvents(),具体看如下代码:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *
import time
class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
  MainWindow.setObjectName("MainWindow")
  MainWindow.resize(390, 500)
  self.centralwidget = QtWidgets.QWidget(MainWindow)
  self.centralwidget.setObjectName("centralwidget")
  self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
  self.gridLayout_2.setObjectName("gridLayout_2")
  self.gridLayout = QtWidgets.QGridLayout()
  self.gridLayout.setObjectName("gridLayout")
  self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
  self.textEdit.setObjectName("textEdit")
  self.gridLayout.addWidget(self.textEdit, 1, 1, 1, 1)
  self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
  self.tableWidget.setObjectName("tableWidget")
  self.tableWidget.setColumnCount(3)
  self.tableWidget.setRowCount(4)
  item = QtWidgets.QTableWidgetItem()
  self.tableWidget.setVerticalHeaderItem(0, item)
  item = QtWidgets.QTableWidgetItem()
  self.tableWidget.setVerticalHeaderItem(1, item)
  item = QtWidgets.QTableWidgetItem()
  self.tableWidget.setVerticalHeaderItem(2, item)
  item = QtWidgets.QTableWidgetItem()
  self.tableWidget.setVerticalHeaderItem(3, item)
  item = QtWidgets.QTableWidgetItem()
  self.tableWidget.setHorizontalHeaderItem(0, item)
  item = QtWidgets.QTableWidgetItem()
  self.tableWidget.setHorizontalHeaderItem(1, item)
  item = QtWidgets.QTableWidgetItem()
  self.tableWidget.setHorizontalHeaderItem(2, item)
  self.gridLayout.addWidget(self.tableWidget, 0, 1, 1, 1)
  self.pushButton = QtWidgets.QPushButton(self.centralwidget)
  self.pushButton.setMaximumSize(QtCore.QSize(200, 16777215))
  self.pushButton.setObjectName("pushButton")
  self.gridLayout.addWidget(self.pushButton, 2, 1, 1, 1)
  self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
  MainWindow.setCentralWidget(self.centralwidget)
  self.menubar = QtWidgets.QMenuBar(MainWindow)
  self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 18))
  self.menubar.setObjectName("menubar")
  MainWindow.setMenuBar(self.menubar)
  self.statusbar = QtWidgets.QStatusBar(MainWindow)
  self.statusbar.setObjectName("statusbar")
  MainWindow.setStatusBar(self.statusbar)
  # 表格属性设置
  self.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
  # 【开始】按钮
  self.pushButton.clicked.connect(self.pushButton_Clicked)
  self.retranslateUi(MainWindow)
  QtCore.QMetaObject.connectSlotsByName(MainWindow)
 # 设置每隔几秒输出数据
 def pushButton_Clicked(self):
  self.textEdit.setText("获取基金数据中...")
  # 刷新页面
  QApplication.processEvents()
  item = [["110003", "易方达上证50", "-1.97%"], ["005918", "天弘沪深300", "-3.15%"], ["001513", "易方达信息产业", "-4.69%"]]
  for i in range(3):
   # 每隔两秒执行一次以下代码,直到循环结束
   time.sleep(2)
   self.textEdit.append("获取基金" + item[i][1] + "\n...")
   for j in range(3):
    self.tableWidget.setItem(i, j, QtWidgets.QTableWidgetItem(item[i][j]))
   # 刷新页面
   QtWidgets.QApplication.processEvents()
 def retranslateUi(self, MainWindow):
  _translate = QtCore.QCoreApplication.translate
  MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  item = self.tableWidget.verticalHeaderItem(0)
  item.setText(_translate("MainWindow", "1"))
  item = self.tableWidget.verticalHeaderItem(1)
  item.setText(_translate("MainWindow", "2"))
  item = self.tableWidget.verticalHeaderItem(2)
  item.setText(_translate("MainWindow", "3"))
  item = self.tableWidget.verticalHeaderItem(3)
  item.setText(_translate("MainWindow", "4"))
  item = self.tableWidget.horizontalHeaderItem(0)
  item.setText(_translate("MainWindow", "基金编号"))
  item = self.tableWidget.horizontalHeaderItem(1)
  item.setText(_translate("MainWindow", "基金名称"))
  item = self.tableWidget.horizontalHeaderItem(2)
  item.setText(_translate("MainWindow", "基金净值"))
  self.pushButton.setText(_translate("MainWindow", "开始"))

if __name__ == "__main__":
 app = QApplication(sys.argv)
 MainWindow = QtWidgets.QMainWindow()
 ui = Ui_MainWindow()
 ui.setupUi(MainWindow)
 MainWindow.show()
 sys.exit(app.exec_())
            
最新图文资讯
1 2 3 4 5 6
易采站长站 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助 -