Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

python - Pyqt5 Change text browser from different module

I want to be able to change gui textbrowser from another module while staying in that other module before exiting back to main loop. Is this possible?

I have three files for testing Pyqt5.

  1. test_main.py -> main loop of code
  2. second_module.py -> a second module that is going to be used as an example module to change gui status.
  3. test_gui.py -> output of designer files for gui setup

Currently all gui changes are done in test_main.py and updated after each function. I want to be able to change text browser output from the second module as the plan is to have a project with multiple modules. The second module will have fucntions to run and wait for feedback and while waiting for next task, I want to update the main gui after each step from the second module function.

test_main.py

from PyQt5 import QtWidgets
from Project_Files.Designer_Files.test_gui import Ui_MainWindow
import second_module


class TestToolMain(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        self.textBrowser.insertPlainText('Start
')
        self.pushButton.clicked.connect(self.print_screen)

    def print_screen(self):
        self.textBrowser.insertPlainText('print screen
')
        sec_mod = second_module.TestClass()
        sec_mod.test_setup()
        self.textBrowser.insertPlainText('End of Test
')


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    MainWindow = TestToolMain()
    MainWindow.show()
    app.exec_()

second_module.py

from Project_Files.Designer_Files.test_gui import Ui_MainWindow
from PyQt5 import QtWidgets


class TestClass(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)

    def test_setup(self):
        self.textBrowser.insertPlainText('From second module!
')

test_gui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test_gui.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(621, 361)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(70, 130, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(300, 60, 256, 192))
        self.textBrowser.setObjectName("textBrowser")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 621, 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"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Output on screen is:

Start

print screen

End of Test

I want "From second module! " to be added also.

question from:https://stackoverflow.com/questions/65890330/pyqt5-change-text-browser-from-different-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...