PYQT5实现控制台显示功能的方法


Posted in Python onJune 25, 2019

界面文件 Ui_ControlBoard.py

# -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'Ui_ControlBoard.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
 
from PyQt5 import QtCore, 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.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
    self.textBrowser.setGeometry(QtCore.QRect(50, 180, 591, 171))
    self.textBrowser.setObjectName("textBrowser")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(450, 390, 93, 28))
    self.pushButton.setObjectName("pushButton")
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
    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.pushButton.setText(_translate("MainWindow", "PushButton"))

逻辑文件 Call_ControlBoard.py

版本一

#!/usr/bin/env python3
# -*- coding:utf-8 -*- 
from PyQt5 import QtCore, QtGui
import sys
from PyQt5.QtCore import QEventLoop, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow
 
from Ui_ControlBoard import Ui_MainWindow
 
class EmittingStr(QtCore.QObject):
    textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号
    def write(self, text):
      self.textWritten.emit(str(text))
 
 
class ControlBoard(QMainWindow, Ui_MainWindow):
  def __init__(self):
    super(ControlBoard, self).__init__()
    self.setupUi(self)
    # 下面将输出重定向到textBrowser中
    sys.stdout = EmittingStr(textWritten=self.outputWritten)
    sys.stderr = EmittingStr(textWritten=self.outputWritten)
 
 
    self.pushButton.clicked.connect(self.bClicked)
 
  def outputWritten(self, text):
    cursor = self.textBrowser.textCursor()
    cursor.movePosition(QtGui.QTextCursor.End)
    cursor.insertText(text)
    self.textBrowser.setTextCursor(cursor)
    self.textBrowser.ensureCursorVisible()
 
  def bClicked(self):
    """Runs the main function."""
    print('Begin')
    loop = QEventLoop()
    QTimer.singleShot(1000, loop.quit)
    loop.exec_()
    self.printABCD()
    loop = QEventLoop()
    QTimer.singleShot(1000, loop.quit)
    loop.exec_()
    print("End")
 
  def printABCD(self):
    print("aaaaaaaaaaaaaaaa")
    print("bbbbbbbbbbbbbbbb")
    print("cccccccccccccccc")
    print("dddddddddddddddd")
 
 
 
 
if __name__ == "__main__":
  app = QApplication(sys.argv)
  win = ControlBoard()
  win.show()
  sys.exit(app.exec_())

PYQT5实现控制台显示功能的方法

版本二

#!/usr/bin/env python3
# -*- coding:utf-8 -*- 
from PyQt5 import QtCore, QtGui
import sys
from PyQt5.QtCore import QEventLoop, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow
 
from Ui_ControlBoard import Ui_MainWindow
 
class EmittingStr(QtCore.QObject):
    textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号
    def write(self, text):
      self.textWritten.emit(str(text))
      loop = QEventLoop()
      QTimer.singleShot(1000, loop.quit)
      loop.exec_()
 
 
class ControlBoard(QMainWindow, Ui_MainWindow):
  def __init__(self):
    super(ControlBoard, self).__init__()
    self.setupUi(self)
    # 下面将输出重定向到textBrowser中
    sys.stdout = EmittingStr(textWritten=self.outputWritten)
    sys.stderr = EmittingStr(textWritten=self.outputWritten)
 
 
    self.pushButton.clicked.connect(self.bClicked)
 
  def outputWritten(self, text):
    cursor = self.textBrowser.textCursor()
    cursor.movePosition(QtGui.QTextCursor.End)
    cursor.insertText(text)
    self.textBrowser.setTextCursor(cursor)
    self.textBrowser.ensureCursorVisible()
 
  def bClicked(self):
    """Runs the main function."""
    print('Begin')
 
    self.printABCD()
 
    print("End")
 
  def printABCD(self):
    print("aaaaaaaaaaaaaaaa")
    print("bbbbbbbbbbbbbbbb")
    print("cccccccccccccccc")
    print("dddddddddddddddd")
 
 
 
 
if __name__ == "__main__":
  app = QApplication(sys.argv)
  win = ControlBoard()
  win.show()
  sys.exit(app.exec_())

PYQT5实现控制台显示功能的方法

版本三

#!/usr/bin/env python3
# -*- coding:utf-8 -*- 
from PyQt5 import QtCore, QtGui
import sys
from PyQt5.QtCore import QEventLoop, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow
 
from Ui_ControlBoard import Ui_MainWindow
 
class EmittingStr(QtCore.QObject):
    textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号
    def write(self, text):
      self.textWritten.emit(str(text))
 
 
class ControlBoard(QMainWindow, Ui_MainWindow):
  def __init__(self):
    super(ControlBoard, self).__init__()
    self.setupUi(self)
    # 下面将输出重定向到textBrowser中
    sys.stdout = EmittingStr(textWritten=self.outputWritten)
    sys.stderr = EmittingStr(textWritten=self.outputWritten)
 
 
    self.pushButton.clicked.connect(self.bClicked)
 
  def outputWritten(self, text):
    cursor = self.textBrowser.textCursor()
    cursor.movePosition(QtGui.QTextCursor.End)
    cursor.insertText(text)
    self.textBrowser.setTextCursor(cursor)
    self.textBrowser.ensureCursorVisible()
 
  def bClicked(self):
    """Runs the main function."""
    print('Begin')
 
    self.printABCD()
 
    print("End")
 
  def printABCD(self):
    print("aaaaaaaaaaaaaaaa")
    print("bbbbbbbbbbbbbbbb")
    print("cccccccccccccccc")
    print("dddddddddddddddd")
 
 
 
 
if __name__ == "__main__":
  app = QApplication(sys.argv)
  win = ControlBoard()
  win.show()
  sys.exit(app.exec_())

PYQT5实现控制台显示功能的方法

以上这篇PYQT5实现控制台显示功能的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python基于smtplib实现异步发送邮件服务
May 28 Python
Python中py文件引用另一个py文件变量的方法
Apr 29 Python
利用Python实现在同一网络中的本地文件共享方法
Jun 04 Python
Python远程视频监控程序的实例代码
May 05 Python
python切片的步进、添加、连接简单操作示例
Jul 11 Python
Python递归求出列表(包括列表中的子列表)的最大值实例
Feb 27 Python
使用 Python 读取电子表格中的数据实例详解
Apr 17 Python
Python能做什么
Jun 02 Python
解决keras backend 越跑越慢问题
Jun 18 Python
Visual Studio Code搭建django项目的方法步骤
Sep 17 Python
用Python爬虫破解滑动验证码的案例解析
May 06 Python
Python函数对象与闭包函数
Apr 13 Python
Pandas之Dropna滤除缺失数据的实现方法
Jun 25 #Python
PyQT5 QTableView显示绑定数据的实例详解
Jun 25 #Python
Pandas之ReIndex重新索引的实现
Jun 25 #Python
Python中使用__new__实现单例模式并解析
Jun 25 #Python
pyQt5实时刷新界面的示例
Jun 25 #Python
Pandas之MultiIndex对象的示例详解
Jun 25 #Python
Python+threading模块对单个接口进行并发测试
Jun 25 #Python
You might like
PHP 之Section与Cookie使用总结
2012/09/14 PHP
destoon实现会员商铺中指定会员或会员组投放广告的方法
2014/08/21 PHP
php安装ssh2扩展的方法【Linux平台】
2016/07/20 PHP
jquery解析json格式数据的方法(对象、字符串)
2015/11/24 Javascript
js实现select下拉框菜单
2015/12/08 Javascript
javascript实现九宫格相加数值相等
2020/05/28 Javascript
JS显示日历和天气的方法
2016/03/01 Javascript
jquery插件格式实例分析
2016/06/16 Javascript
AngularJS中关于ng-class指令的几种实现方式详解
2016/09/17 Javascript
javascript self对象使用详解
2016/10/18 Javascript
jQuery查找和过滤_动力节点节点Java学院整理
2017/07/04 jQuery
javascript 中事件冒泡和事件捕获机制的详解
2017/09/01 Javascript
详解react-native WebView 返回处理(非回调方法可解决)
2018/02/27 Javascript
JS实现可针对算术表达式求值的计算器功能示例
2018/09/04 Javascript
基于游标的分页接口实现代码示例
2018/11/12 Javascript
JQuery实现简单的复选框树形结构图示例【附源码下载】
2019/07/16 jQuery
JavaScript中layim之整合右键菜单的示例代码
2021/02/06 Javascript
python实现k均值算法示例(k均值聚类算法)
2014/03/16 Python
python将图片文件转换成base64编码的方法
2015/03/14 Python
python任务调度实例分析
2015/05/19 Python
详解Python中的type()方法的使用
2015/05/21 Python
举例详解Python中yield生成器的用法
2015/08/05 Python
Python实现小数转化为百分数的格式化输出方法示例
2017/09/20 Python
Python对象属性自动更新操作示例
2018/06/15 Python
对pandas写入读取h5文件的方法详解
2018/12/28 Python
Python OpenCV利用笔记本摄像头实现人脸检测
2020/08/20 Python
python全栈要学什么 python全栈学习路线
2019/06/28 Python
numpy库reshape用法详解
2020/04/19 Python
2021年值得向Python开发者推荐的VS Code扩展插件
2021/01/25 Python
Reebok俄罗斯官方网上商店:购买锐步运动服装和鞋子
2016/09/26 全球购物
大学生职业生涯规划书范文
2014/01/14 职场文书
会计员岗位职责
2014/03/15 职场文书
迎新晚会主持词
2014/03/24 职场文书
政风行风评议整改方案
2014/09/15 职场文书
2014年卫生保健工作总结
2014/12/08 职场文书
检讨书范文500字
2015/01/28 职场文书