PYQT5實(shí)現(xiàn)控制臺(tái)顯示功能的方法
界面文件 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) #定義一個(gè)發(fā)送str的信號(hào)
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) #定義一個(gè)發(fā)送str的信號(hào)
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) #定義一個(gè)發(fā)送str的信號(hào)
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實(shí)現(xiàn)控制臺(tái)顯示功能的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python集合union()函數(shù)使用實(shí)例詳解
union()方法的工作原理是:返回多個(gè)集合(集合的數(shù)量大于等于2)的并集,即結(jié)果集合包含了所有被合并集合中的所有元素,因?yàn)榧现械脑夭豢芍貜?fù),所以各個(gè)集合中重復(fù)的元素在結(jié)果集合中只會(huì)出現(xiàn)一次,本文將簡(jiǎn)單介紹一下Python union()函數(shù)使用方法2023-07-07
解決python中os.listdir()函數(shù)讀取文件夾下文件的亂序和排序問(wèn)題
今天小編就為大家分享一篇解決python中os.listdir()函數(shù)讀取文件夾下文件的亂序和排序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Python+streamlit實(shí)現(xiàn)輕松創(chuàng)建人事系統(tǒng)
streamlit 是 基于 Python 的一個(gè)非常強(qiáng)大的 web 構(gòu)建系統(tǒng),通過(guò)該類庫(kù),我們可以實(shí)現(xiàn)不需要編寫一行前端代碼而構(gòu)建一個(gè)完整的 Web 應(yīng)用。下面我們就來(lái)編寫一個(gè)簡(jiǎn)單的人事系統(tǒng)吧2023-02-02
pydantic-resolve嵌套數(shù)據(jù)結(jié)構(gòu)生成LoaderDepend管理contextvars
這篇文章主要為大家介紹了pydantic-resolve解決嵌套數(shù)據(jù)結(jié)構(gòu)生成LoaderDepend管理contextvars的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-04-04
tensorflow通過(guò)模型文件,使用tensorboard查看其模型圖Graph方式
今天小編就為大家分享一篇tensorflow通過(guò)模型文件,使用tensorboard查看其模型圖Graph方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python 實(shí)現(xiàn)在文件中的每一行添加一個(gè)逗號(hào)
下面小編就為大家分享一篇Python 實(shí)現(xiàn)在文件中的每一行添加一個(gè)逗號(hào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
python得到一個(gè)excel的全部sheet標(biāo)簽值方法
今天小編就為大家分享一篇python得到一個(gè)excel的全部sheet標(biāo)簽值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

