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中的语句和语法
Aug 10 Python
python3 读写文件换行符的方法
Apr 09 Python
Python常见内置高效率函数用法示例
Jul 31 Python
如何安装多版本python python2和python3共存以及pip共存
Sep 18 Python
pandas 将索引值相加的方法
Nov 15 Python
Python3实现汉语转换为汉语拼音
Jul 08 Python
利用python-docx模块写批量生日邀请函
Aug 26 Python
快速解决jupyter启动卡死的问题
Apr 10 Python
Python实现画图软件功能方法详解
Jul 28 Python
Python爬虫Scrapy框架CrawlSpider原理及使用案例
Nov 20 Python
python 使用OpenCV进行简单的人像分割与合成
Feb 02 Python
Python利用机器学习算法实现垃圾邮件的识别
Jun 28 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
JAVA/JSP学习系列之六
2006/10/09 PHP
ThinkPHP标签制作教程
2014/07/10 PHP
php算法实例分享
2015/07/14 PHP
PHP实现的memcache环形队列类实例
2015/07/28 PHP
php 反斜杠处理函数addslashes()和stripslashes()实例详解
2016/12/25 PHP
在html页面中包含共享页面的方法
2008/10/24 Javascript
让新消息在网页标题闪烁提示的jQuery代码
2013/11/04 Javascript
使用iframe window的scroll方法控制iframe页面滚动
2014/03/05 Javascript
JS显示下拉列表框内全部元素的方法
2015/03/31 Javascript
JS获取网页图片name属性的方法
2015/04/01 Javascript
浅谈js构造函数的方法与原型prototype
2016/07/04 Javascript
AngularJs页面筛选标签小功能
2016/08/01 Javascript
使用Angular.js实现简单的购物车功能
2016/11/21 Javascript
react-router JS 控制路由跳转实例
2017/06/15 Javascript
Vue 实现展开折叠效果的示例代码
2018/08/27 Javascript
layui实现文件或图片上传记录
2018/08/28 Javascript
深入浅出了解Node.js Streams
2019/05/27 Javascript
[44:37]完美世界DOTA2联赛PWL S3 Forest vs access 第一场 12.11
2020/12/13 DOTA
[54:17]DOTA2-DPC中国联赛定级赛 RNG vs iG BO3第二场 1月10日
2021/03/11 DOTA
Python对象体系深入分析
2014/10/28 Python
python 读写txt文件 json文件的实现方法
2016/10/22 Python
解决python os.mkdir创建目录失败的问题
2018/10/16 Python
查看python安装路径及pip安装的包列表及路径
2019/04/03 Python
Pytorch 中的optimizer使用说明
2021/03/03 Python
Tirendo比利时:在线购买轮胎
2018/10/22 全球购物
数字漫画:comiXology
2020/06/13 全球购物
介绍一下Linux内核的排队自旋锁
2014/01/04 面试题
应用心理学个人的求职信
2013/12/08 职场文书
学校消防安全责任书
2014/07/23 职场文书
个人党性分析总结
2015/03/05 职场文书
公务员廉洁从政心得体会
2016/01/19 职场文书
2016年度师德标兵先进事迹材料
2016/02/26 职场文书
2016年幼儿园教研活动总结
2016/04/05 职场文书
干货:企业内部人才推荐奖励方案!
2019/07/09 职场文书
MySQL CHAR和VARCHAR该如何选择
2021/05/31 MySQL
将MySQL的表数据全量导入clichhouse库中
2022/03/21 MySQL