【问题标题】:PyQt5 insertPlainText from Another Class Python 3.8 [duplicate]PyQt5 insertPlainText 来自另一个类 Python 3.8 [重复]
【发布时间】:2020-12-13 23:32:09
【问题描述】:

单击“otherWindow”上的“OK”按钮应该会导致 MainWindow 的 QTextEdit 插入文本“WORKS!”。
问题是,它确实执行了print("Print Works"),但是当从另一个函数调用时 insertPlainText 似乎什么都不做。
def printText(self, message): 函数本身没有损坏,它按预期工作,您可以通过单击主窗口上的“消息”按钮来验证。

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

【问题讨论】:

    标签: python pyqt5 python-3.8


    【解决方案1】:

    它不起作用,因为您将文本设置在新窗口上(立即关闭)。

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

    当您调用MainWindow() 时,您实际上是在创建MainWindow 的一个NEW 实例,并且该窗口的文本实际上已更新,但您看不到它,因为它立即是垃圾函数返回后立即收集并删除。

    您需要访问现有实例,或者找到一种与之通信的方法(通常使用信号)。

    在以下示例中,我将对主窗口的引用添加到 OtherWindow 构造函数,然后访问它的方法:

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

    注意:我把OtherWindow类名大写,小写的名字只能用于变量和属性。

    【讨论】:

    • 仍然无法正常工作 - imgur.com/a/8K1C6vC (GIF)
    • 用您的代码重写后,我什至无法打印“Print Works”
    • 你实现了其余的代码吗?我只插入了要添加或修改的行。另外,您是否使用了正确的大小写?从 shell/prompt 运行程序,看看会发生什么(“仍然无法让它工作”对我们来说不是很有用),因为如果您使用的是 PyCharm 或其他 IDE,它们的调试器会阻止任何可能的错误的完整追溯。
    • 很好,我没有把“self”放在OtherWindow(self)中,现在可以了,谢谢
    猜你喜欢
    • 2020-08-12
    • 2019-12-15
    • 1970-01-01
    • 2014-11-25
    • 2015-07-19
    • 2018-10-24
    • 1970-01-01
    • 2017-03-21
    • 2011-12-09
    相关资源
    最近更新 更多