0

Clicking button "OK" on "otherWindow" should cause the MainWindow's QTextEdit to insert text "WORKS!".
Problem is, it does execute print("Print Works"), but insertPlainText seems to do nothing when called from another function.
The def printText(self, message): function itself isn't broken, it works as it's supposed to, as you can verify by clicking on the "Message" button on Main Window.

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QPushButton

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.myLayout = QVBoxLayout()
        self.status = QTextEdit()
        self.status.setStyleSheet("QTextEdit {min-width:500px;min-height:200px;}")
        self.status.insertPlainText("test")

        self.btnYes = QPushButton("other window")
        self.btnPrint = QPushButton("Message")

        self.btnYes.clicked.connect(self.showOtherWindow)
        self.btnPrint.clicked.connect(self.btnPrintClick)
        self.myLayout.addWidget(self.btnPrint)
        self.myLayout.addWidget(self.btnYes)
        self.myLayout.addWidget(self.status)
        self.setLayout(self.myLayout)

    def setMainText(self, message):
        self.status.insertPlainText("test")
    
    def showOtherWindow(self):
        self.otherWindow = otherWindow()
        self.otherWindow.show()

    def btnPrintClick(self):
        self.printText("button clicked")

    def printText(self, message):
        self.status.insertPlainText("\n" + message)
        print("Print Works")


class otherWindow(QWidget):
    def __init__(self):
        super(otherWindow, self).__init__()
        self.button = QPushButton("OK")
        self.layout2 = QVBoxLayout()
        self.button.clicked.connect(self.btnClick)
        self.layout2.addWidget(self.button)

        self.setLayout(self.layout2)
        self.setFixedSize(200,150)

    def btnClick(self):
        MainWindow().printText("WORKS!")
        self.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

1 Answers1

1

It doesn't work because you're setting the text on a new window (which is closed immediately).

def btnClick(self):
    MainWindow().printText("WORKS!")

When you call MainWindow(), you're actually creating a NEW instance of MainWindow, and the text is actually updated for that window, but you can't see it as it is immediately garbage collected and deleted right after the function returns.

You need to access the existing instance, or find a way to communicate with it (usually using signals).

In the following example, I'm adding a reference to the main window to the OtherWindow constructor, and then access it's method afterwards:

class MainWindow(QWidget):
    # ...
    def showOtherWindow(self):
        self.otherWindow = OtherWindow(self)
        self.otherWindow.show()


class OtherWindow(QWidget):
    def __init__(self, mainWindow=None):
        super(OtherWindow, self).__init__()
        self.mainWindow = mainWindow
        # ...

    def btnClick(self):
        if self.mainWindow:
            self.mainWindow.printText("WORKS!")
        self.close()

Note: I capitalized the OtherWindow class name, lower case names should be only used for variables and attributes.

musicamante
  • 41,230
  • 6
  • 33
  • 58