开发者

How do You get a third script to handle information of two other scripts related to GUIs in pyqt5?

开发者 https://www.devze.com 2022-12-07 22:07 出处:网络
I have recreated a problem I am encountering as a minimal example below. The situation: I have two Qt Designer generated GUI, each being called by their own separated scripts. A third script is meant

I have recreated a problem I am encountering as a minimal example below.

The situation: I have two Qt Designer generated GUI, each being called by their own separated scripts. A third script is meant to collect information from the first script upon the click of a button on the second script. I does not, yet there is no errors.

I have also attempted to solve this by using signals, but these does not seem to communicate between scripts. I provided a simpler version here that doesn't use signals per se.

My question is: How do You get a third script to handle information of two other scripts related to GUIs in pyqt5 ?

Here is the minimal example:

The first GUI script:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(504, 223)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.TypeHere = QtWidgets.QTextEdit(self.centralwidget)
        self.TypeHere.setObjectName("TypeHere")
        self.verticalLayout.addWidget(self.TypeHere)
        self.HelloButton = QtWidgets.QPushButton(self.centralwidget)
        self.HelloButton.setObjectName("HelloButton")
        self.verticalLayout.addWidget(self.HelloButton)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 504, 22))
        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.HelloButton.setText(_translate("MainWindow", "Say hello"))


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

The second GUI script:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(282, 392)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton01 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton01.setObjectName("pushButton01")
        self.verticalLayout.addWidget(self.pushButton01)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 282, 22))
        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.pushButton01.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_())

JustSomeTextv01, the script of the first GUI:

from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess, QThreadPool
from TypingUIv01 import Ui_MainWindow
import JustSomeButtonsv01 as JSB
import sys

class Window(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi(self)
        self.HelloButton.pressed.connect(self.openButtons)
        self.Display = JSB.Window()
        self.ButtonsThread()
        
    def openButtons(self):
        self.Display.show()
    def ButtonsThread(self):
        self.threadpoolbutt = QThreadPool()
        self.threadpoolbutt.start(self.runButtons)
    def runButtons(self):
        self.butt = QProcess()
        print("Buttons Running")
        self.butt.execute('python',['JustSomeButtonsv01.py'])
        

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())

JustSomeButtonsv01, the script of the second GUI:

from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess, QThreadPool
from ButtonsUIv01 import Ui_MainWindo开发者_开发技巧w
# import JustSomeRecordv01 as JSR
import sys

class Window(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setupUi(self)
        self.RecordThread()
    
    def RecordThread(self):
        self.threadpoolrec = QThreadPool()
        self.threadpoolrec.start(self.runRecord)
    def runRecord(self):
        self.rec = QProcess()
        print("Record Running")
        self.rec.execute('python',['JustSomeRecordv01.py'])


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    # window.show()
    sys.exit(app.exec())

And finally, JustSomeRecordv01, the third script trying to interact with the other two:

from PyQt5 import QtWidgets
import sys
from TypingUIv01 import Ui_MainWindow as JSTWin
from ButtonsUIv01 import Ui_MainWindow as ButtWin

class Record(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        # self.setupUi(self)
        app2 = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        self.Win = JSTWin()
        self.Win.setupUi(MainWindow)
        self.Text = self.Win.TypeHere.toPlainText()
        print("Running")
        self.Butt = ButtWin()
        self.Butt.setupUi(MainWindow)
        self.Butt.pushButton01.pressed.connect(self.PrintIT)
    def PrintIT(self):
        print("Texting")
        print(self.Text)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Record()
    # window.show()
    sys.exit(app.exec())

How to reproduce the problem: You execute the JustSomeTextv01 script. Press the "Hello Button" and a second window will show up. You type anything in the QTextEdit of the first window and then click the button of the second window. The intent is that this second button would print what You wrote, but it doesn't work.

Thank You for your time,

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号