PyQt5 GUI从另一个类在TextEdit中写入

时间:2018-12-10 16:48:15

标签: python-3.x user-interface pyqt5 qt-designer

我刚开始使用Python(Anaconda和Spyder),所以是的,我确实是一个初学者。 我想使用GUI,为此,我使用QtDesigner构建了GUI,使用命令pyuic5 file.ui –o file.py并将其添加到main.py中。到目前为止效果很好。但是现在我陷入了GUI的TextEdit的困境。我只是想从另一个类(TestClass)中写入它。从App类调用函数test和print v进行得很顺利。另一种方法不起作用,或者在我尝试创建无限循环的其他事情上。有人可以帮我吗?我是否应该做一些原则上不同的事情,例如不同地添加design.py?谢谢您抽出宝贵的时间!

import sys
from PyQt5 import QtWidgets, QtCore, QtGui, uic
from PyQt5.QtCore import pyqtSlot

import inspect
import LED_design

class App(QtWidgets.QMainWindow, LED_design.Ui_MainWindow):
    def __init__(self, parent=None):
        print("app")
        # super: it allows to access variables and methods in the design.py file
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)  # This is defined in design.py file automatically
                            # It sets up layout and widgets that are defined

        self.textEdit.setReadOnly(True)
        self.textEdit.append('tt')

    def poweron_CH1(self):
        if self.radioButton_CH1.isChecked() ==True:
            print("ON")
            i=TestClass()       #Here I'm giving a 3 to the test funciton and it works
            i.test(v='3')

class TestClass():
  #  def __init__(self):
        #super().__init__(self)
        #self.textEdit.append('txt')

    def test(self, v):

        print(v, flush=True)    #flush=True prints immediatly
       # print("works %s" %v)

        j = App()       
        j.textEdit.append('txt')    #here nothing happens

def main():
    app = QtWidgets.QApplication(sys.argv)   # A new instance of QApplication
    form = App()                             # set the form to be the App (design)
    form.show()                              # Show the form
    app.exec_()                              # and execute the app


if __name__ == '__main__':              # if running file directly and not importing it
    main()   

                       # run the main function

0 个答案:

没有答案