PYQT5实现控制台显示功能的方法
作者:晚安丶 发布时间:2023-08-08 11:11:53
标签:PYQT5,控制台,显示
界面文件 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_())
版本二
#!/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_())
版本三
#!/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_())
来源:https://blog.csdn.net/william_munch/article/details/89425038


猜你喜欢
- 经常开发asp但对于细致的说法,真实不太清楚,这里简单的介绍下。一般情况下读取数据都是用rs.open sql,conn,1,1修改数据:r
- 这篇文章主要介绍了如何基于python实现脚本加密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- gojson是快速解析json数据的一个golang包,你使用它可以快速的查找json内的数据安装 go get github.com/wi
- Array可以使用下标,Map和Set不能使用下标,ES6引入了iterable类型,Array,Map,Set都属于iterable类型,
- 很早就在这里看到过解决方案,与嗷嗷讨论后发现这个方案还是很可靠的。当然,唯一的缺点就是每一个属性都要去Hack,但我在很多实践中,只用‘修正
- 代码如下:var obj = document.getElementById("name"
- 1. 索引及切片数组中的元素可以通过索引以及切片的手段进行访问或者修改,和列表的切片操作一样。下面直接使用代码进行实现,具体操作方式以及意义
- golang1.16也在今天正式发布了。原定计划是2月1号年前发布的,不过迟到也是golang的老传统了,正好也趁着最后的假期快速预览一下g
- Python中将列表转换成为数据框有两种情况:第一种是两个不同列表转换成一个数据框,第二种是一个包含不同子列表的列表转换成为数据框。第一种:
- 微信小程序可谓是9月21号之后最火的一个名词了,一经出现真是轰炸了整个开发人员,当然很多App开发人员有了一个担心,微信小程序的到来会不会让
- 本文实例为大家分享了Python KNN分类算法的具体代码,供大家参考,具体内容如下KNN分类算法应该算得上是机器学习中最简单的分类算法了,
- 安装的 MySQL 5.1.48 或是 MySQL 5.5.8,配置好最后点击 Execute 按钮了,但是进行不到 Start servi
- 解决MySql 数据库 提示:1045 access denied for user 'root'@'localho
- 本教程使用的是python 3 和django为事例打开pycharmfile -> new
- 今天接到一个小需求,就是想在windows环境下,上传压缩文件到linux指定的目录位置并且解压出来,然后我想了一下,这个可以用python
- 使用Python+OpenCV实现鼠标画框的代码,供大家参考,具体内容如下# -*-coding: utf-8 -*-""
- 目录 一、环境配置 二、ASP对Excel的基本操作 三、ASP操作Excel生成数据表 四、ASP操作Excel生成Chart图 五、服务
- 话不多说,直接上代码import copyimport cv2import numpy as npWIN_NAME = 'draw_
- 环境系统:win10cpu:i7-6700HQgpu:gtx965mpython : 3.6pytorch :0.3数据下载来源自Sasan
- 编译原理老师要求写一个java的词法分析器,想了想决定用python写一个。目标能识别出变量,数字,运算符,界符和关键字,用excel表打印