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脚本来获取mp3文件tag信息的教程
May 04 Python
python中利用zfill方法自动给数字前面补0
Apr 10 Python
Python基于Floyd算法求解最短路径距离问题实例详解
May 16 Python
对pandas将dataframe中某列按照条件赋值的实例讲解
Nov 29 Python
python读取xlsx的方法
Dec 25 Python
Python+PyQt5实现美剧爬虫可视工具的方法
Apr 25 Python
python-tkinter之按钮的使用,开关方法
Jun 11 Python
Python range、enumerate和zip函数用法详解
Sep 11 Python
Python while循环使用else语句代码实例
Feb 07 Python
在PyCharm中遇到pip安装 失败问题及解决方案(pip失效时的解决方案)
Mar 10 Python
Python远程方法调用实现过程解析
Jul 28 Python
通过Python pyecharts输出保存图片代码实例
Nov 25 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对字符串的递增运算分析
2010/08/08 PHP
推荐一款MAC OS X 下php集成开发环境mamp
2014/11/08 PHP
YII2.0之Activeform表单组件用法实例
2016/01/09 PHP
PHP读取大文件的多种方法介绍
2016/04/04 PHP
JavaScript 异步调用框架 (Part 2 - 用例设计)
2009/08/03 Javascript
JS 控制非法字符的输入代码
2009/12/04 Javascript
一行代码告别document.getElementById
2012/06/01 Javascript
jQuery中ajax的load()方法用法实例
2014/12/26 Javascript
Javascript无参数和有参数类继承问题解决方法
2015/03/02 Javascript
JavaScript中getUTCSeconds()方法的使用详解
2015/06/11 Javascript
JavaScript实现点击按钮切换网页背景色的方法
2015/10/17 Javascript
jquery实现简单的banner轮播效果【实例】
2016/03/30 Javascript
JS上传组件FileUpload自定义模板的使用方法
2016/05/10 Javascript
使用Ajax生成的Excel文件并下载的实例
2016/11/21 Javascript
80%应聘者都不及格的JS面试题
2017/03/21 Javascript
Angular 2父子组件数据传递之@Input和@Output详解(下)
2017/07/05 Javascript
jQuery实现鼠标点击处心形漂浮的炫酷效果示例
2018/04/12 jQuery
基于JS实现web端录音与播放功能
2019/04/17 Javascript
Python虚拟环境Virtualenv使用教程
2015/05/18 Python
Python中用altzone()方法处理时区的教程
2015/05/22 Python
在Python中使用AOP实现Redis缓存示例
2017/07/11 Python
机器学习10大经典算法详解
2017/12/07 Python
Python机器学习logistic回归代码解析
2018/01/17 Python
python中利用zfill方法自动给数字前面补0
2018/04/10 Python
Python实现去除图片中指定颜色的像素功能示例
2019/04/13 Python
Python django框架应用中实现获取访问者ip地址示例
2019/05/17 Python
Django的models中on_delete参数详解
2019/07/16 Python
python进阶之自定义可迭代的类
2019/08/20 Python
python 穷举指定长度的密码例子
2020/04/02 Python
保安的辞职报告怎么写
2014/01/20 职场文书
贷款担保申请书
2014/05/20 职场文书
学习教师法的心得体会
2014/09/03 职场文书
完整版商业计划书
2014/09/15 职场文书
趣味运动会加油词
2015/07/18 职场文书
Python循环之while无限迭代
2022/04/30 Python
2022年显卡天梯图(6月更新)
2022/06/17 数码科技