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_())

 

  • 12
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
好的,我可以为你解答这个问题。 在Python中,我们可以使用PyQt5库来进行多层界面设计。PyQt5是一个用于Python编程语言的GUI工具包,它允许我们创建各种GUI应用程序,包括窗口应用程序、控制台应用程序和游戏等。 要实现多层界面设计,我们需要使用PyQt5中的QWidget类和QStackedWidget类。QWidget是PyQt5中所有控件的基类,而QStackedWidget是一种特殊的QWidget,它可以在多个QWidget之间进行切换。 首先,我们需要创建一个主窗口,并在主窗口中添加一个QStackedWidget作为主窗口的中心控件。然后,我们可以在QStackedWidget中添加多个QWidget作为子控件,并在每个QWidget中放置我们想要显示的控件。 下面是一个简单的示例代码,它演示了如何创建一个具有两个QWidget的QStackedWidget,并在每个QWidget中添加一个QPushButton: ```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QStackedWidget class MainWindow(QMainWindow): def __init__(self): super().__init__() # 创建主窗口 self.setWindowTitle("多层界面设计示例") self.setGeometry(100, 100, 400, 300) # 创建QStackedWidget并添加QWidget self.stacked_widget = QStackedWidget(self) self.setCentralWidget(self.stacked_widget) widget1 = QWidget() button1 = QPushButton("界面1中的按钮", widget1) layout1 = QVBoxLayout(widget1) layout1.addWidget(button1) self.stacked_widget.addWidget(widget1) widget2 = QWidget() button2 = QPushButton("界面2中的按钮", widget2) layout2 = QVBoxLayout(widget2) layout2.addWidget(button2) self.stacked_widget.addWidget(widget2) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 接下来,我们需要实现相应的添加文件功能。我们可以在一个QWidget中添加一个QPushButton,当用户单击该按钮时,我们将弹出一个文件对话框,让用户选择要添加的文件。然后,我们可以将选定的文件添加到我们的应用程序中。 下面是一个简单的示例代码,它演示了如何在一个QWidget中添加一个QPushButton,并在单击该按钮时打开文件对话框: ```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QFileDialog class MainWindow(QMainWindow): def __init__(self): super().__init__() # 创建主窗口 self.setWindowTitle("添加文件示例") self.setGeometry(100, 100, 400, 300) # 创建QWidget并添加QPushButton widget = QWidget(self) button = QPushButton("添加文件", widget) button.clicked.connect(self.add_file) layout = QVBoxLayout(widget) layout.addWidget(button) self.setCentralWidget(widget) def add_file(self): file_dialog = QFileDialog(self) file_dialog.exec_() selected_files = file_dialog.selectedFiles() print("已选文件:", selected_files) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 在这个示例代码中,我们创建了一个QWidget,并在其中添加了一个QPushButton。当用户单击该按钮时,我们调用add_file()方法。在add_file()方法中,我们创建了一个QFileDialog,并调用它的exec_()方法以显示文件对话框。当用户选择一个或多个文件后,我们可以使用selectedFiles()方法获取所选文件的路径,并将其打印到控制台中。 这就是利用Python和PyQt5实现多层界面设计并实现相应添加文件功能的基本思路。当然,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值