Python控制台运行结果输出到GUI(PyQt5)

一般情况下,程序运行结果在控制台输出,那如果不希望结果在控制台显示,而是输出到图形化用户界面呢?可以通过PyQt5,将运行结果输出定向到QTextEdit来完成。

环境:python3.7,pychram CE

1.准备工作

  • Qt 
  • sip
  • PyQt5

Qt 5可以在官网下载,选择免费的OpenSource系列,但速度较慢。推荐在清华镜像下载Qt-5.13.0,整个软件较大,大概3.2G。下载安装成功后把Qt5的bin目录加入到path。

brew安装sip, pyqt5

$brew install sip
$brew install pyqt5

用brew安装时,brew updating会耗费一定时间。但install sip 和install pyqt都很快。

我在安装pyqt时发现安装依赖中有qt,而且下载速度比较慢,所以我中断了进程。单独安装qt:

$brew install qt

这次qt的下载速度很快,安装完成后重新brew安装sip和pyqt5。

成功安装:sip-4.19.19_1,pyqt-5.10.1_2

2.在pycharm中配置QtDesigner和pyUIC

参考 Python - Mac下PyCharm&PyQt5环境搭建  完成pycharm中的环境搭建。

进行显示hello world安装成功测试。

3.一些简单的pyqt5例子

为熟悉pyqt5的使用方法,可以尝试一些小例子。

(1)创建一个空白窗口

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

'''
GUI—1:一个空白的窗口
构造窗体控件:w = QWidget()  

'''

if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 每一个GUI程序都有一个QApplication对象,创建时需要传参
    # 因为PyQt可以识别一些自己到参数,比如-geometry和style
    w = QWidget()     # 构造窗体window
    w.resize(250,150)  # 设置宽高
    w.move(300,300)    # 移动到坐标(300,300)位置
    w.setWindowTitle('window title')   # 设置窗口标题
    w.show()  # 向QApplication事件列表中添加新的事件,以请求对特定窗口部件进行绘制
    sys.exit(app.exec_())
    # app.exec_()期待QApplication的事件循环
    # sys.exit()是为了在窗口被关闭时,系统能得到通知

运行结果:

(2)创建一个带label显示helloworld的窗口

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

'''
GUI—2:一个显示hello world的窗口
构造标签控件:label=QLable()

'''
a = str([1,2,3])
if __name__ == '__main__':
    app = QApplication(sys.argv)

    lable = QLabel()
    lable.setText(a)
    lable.setAlignment(Qt.AlignCenter)
    lable.resize(250, 150)
    lable.move(300, 300)
    # 设置窗口标题
    lable.setWindowTitle('window title')
    # 设置flags为闪屏模式可移除标题栏

    lable.show()
    sys.exit(app.exec_())

运行结果:

(3)通过另一种方式创建窗口

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

'''
GUI—3:创建窗口的另一种方式
定义class App(QWidget)  

'''


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'another window'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480

        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

运行结果:

通过这些例子熟悉pyqt5的使用。大多情况下用第二种方法创建窗口,也就是例子(3)中定义class的方式。

4.运行结果输出到GUI

将python控制台输出打印到Qtextedit。

运行以下代码,弹出GUI,单击“run”按钮,显示'Running ...',然后执行函数printhello()输出hello,等待5秒,显示'Done'。

import sys
import time
from PyQt5.QtCore import QObject, pyqtSignal, QEventLoop, QTimer
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QTextEdit
from PyQt5.QtGui import QTextCursor

'''
控制台输出定向到Qtextedit中
'''


class Stream(QObject):
    """Redirects console output to text widget."""
    newText = pyqtSignal(str)

    def write(self, text):
        self.newText.emit(str(text))


class GenMast(QMainWindow):
    """Main application window."""
    def __init__(self):
        super().__init__()

        self.initUI()

        # Custom output stream.
        sys.stdout = Stream(newText=self.onUpdateText)

    def onUpdateText(self, text):
        """Write console output to text widget."""
        cursor = self.process.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText(text)
        self.process.setTextCursor(cursor)
        self.process.ensureCursorVisible()

    def closeEvent(self, event):
        """Shuts down application on close."""
        # Return stdout to defaults.
        sys.stdout = sys.__stdout__
        super().closeEvent(event)

    def initUI(self):
        """Creates UI window on launch."""
        # Button for generating the master list.
        btnGenMast = QPushButton('Run', self)
        btnGenMast.move(450, 50)
        btnGenMast.resize(100, 200)
        btnGenMast.clicked.connect(self.genMastClicked)

        # Create the text output widget.
        self.process = QTextEdit(self, readOnly=True)
        self.process.ensureCursorVisible()
        self.process.setLineWrapColumnOrWidth(500)
        self.process.setLineWrapMode(QTextEdit.FixedPixelWidth)
        self.process.setFixedWidth(400)
        self.process.setFixedHeight(200)
        self.process.move(30, 50)

        # Set window size and title, then show the window.
        self.setGeometry(300, 300, 600, 300)
        self.setWindowTitle('Generate Master')
        self.show()

    def printhello(self):
        print('hello')

    def genMastClicked(self):
        """Runs the main function."""
        print('Running...')
        self.printhello()
        loop = QEventLoop()
        QTimer.singleShot(2000, loop.quit)
        loop.exec_()
        print('Done.')


if __name__ == '__main__':
    # Run the application.
    app = QApplication(sys.argv)
    app.aboutToQuit.connect(app.deleteLater)
    gui = GenMast()
    sys.exit(app.exec_())

运行结果:

简单解析:

    def genMastClicked(self):
        """Runs the main function."""
        print('Running...')
        
        self.printhello()   
        
        loop = QEventLoop()
        QTimer.singleShot(2000, loop.quit)
        loop.exec_()
        
        print('Done.')

我们将要运行的函数放在genMastClicked(self)函数中执行。例如printhello()函数。

泛化使用时,只要像定义printhello()函数一样,定义其他函数,然后在genMastClicked(self)中执行即可。
 

主要参考资料:

https://cloud.tencent.com/developer/ask/201670

https://zhuanlan.zhihu.com/p/35389494

https://blog.csdn.net/weixin_39626452/article/details/86700430

好的,下面是一个Python GUIPyQt5中QComboBox控件的使用方法和实例: QComboBox是PyQt5中的一个下拉列表框控件,可以方便地在图形界面中实现下拉列表框功能。以下是QComboBox控件的详细使用方法和实例: 1. 在PyQt5中导入QComboBox控件: ``` python from PyQt5.QtWidgets import QComboBox ``` 2. 创建一个QComboBox控件对象: ``` python combo_box = QComboBox() ``` 3. 添加下拉列表框选项: 可以通过addItem()方法添加下拉列表框选项,也可以在创建QComboBox控件时直接传入选项列表: ``` python combo_box.addItem('选项1') combo_box.addItem('选项2') combo_box.addItem('选项3') # 或者 options = ['选项1', '选项2', '选项3'] combo_box = QComboBox(self) combo_box.addItems(options) ``` 4. 获取当前选中的选项: 可以通过currentIndex()方法获取当前选中的选项的索引值,也可以通过currentText()方法获取当前选中的选项的文本内容: ``` python index = combo_box.currentIndex() text = combo_box.currentText() ``` 5. 为下拉列表框添加事件: 可以为下拉列表框添加事件,例如选项改变时触发事件。可以通过currentIndexChanged()方法来实现: ``` python combo_box.currentIndexChanged.connect(self.on_combo_box_changed) def on_combo_box_changed(self, index): # index为当前选中的选项的索引值 print(index) ``` 下面是一个完整的例子: ``` python from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox, QVBoxLayout import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('QComboBox控件使用例子') self.setGeometry(300, 300, 300, 200) label = QLabel('请选择一项:', self) combo_box = QComboBox(self) combo_box.addItem('选项1') combo_box.addItem('选项2') combo_box.addItem('选项3') combo_box.currentIndexChanged.connect(self.on_combo_box_changed) layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(combo_box) self.setLayout(layout) self.show() def on_combo_box_changed(self, index): print(index) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 该例子创建了一个窗口,包含一个QLabel控件和一个QComboBox控件,当选项改变时会触发on_combo_box_changed()方法。当选项改变时,会在控制台输出当前选中的选项的索引值。 希望这个例子能够帮助你理解使用PyQt5中的QComboBox控件。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值