Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在接口加载完成后立即调用PyQt5中的方法?_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

Python 如何在接口加载完成后立即调用PyQt5中的方法?

Python 如何在接口加载完成后立即调用PyQt5中的方法?,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,我正在Qt5中制作一个仪器接口,它工作正常。唯一的问题是启动速度慢,因为接口\uuuu init\uuu包含用于连接仪器的耗时方法(5-10秒)。目前,几秒钟内没有显示任何内容,然后整个界面显示“成功连接到仪器”消息,该消息已写入其控制台(文本编辑小部件) 我想要的是让界面立即显示出来,只有在显示出来之后,它才应该启动通信协议。我确信这只是移动一条线路的问题,但我想不出来。感谢您的帮助 以下是程序结构的一个最小示例: # =====================================

我正在Qt5中制作一个仪器接口,它工作正常。唯一的问题是启动速度慢,因为接口
\uuuu init\uuu
包含用于连接仪器的耗时方法(5-10秒)。目前,几秒钟内没有显示任何内容,然后整个界面显示“成功连接到仪器”消息,该消息已写入其控制台(文本编辑小部件)

我想要的是让界面立即显示出来,只有在显示出来之后,它才应该启动通信协议。我确信这只是移动一条线路的问题,但我想不出来。感谢您的帮助

以下是程序结构的一个最小示例:

# ================================================
#      Interface management.
# ================================================

class RENAMEMELATER(Ui_renamemetoo, QObject):

     def __init__(self, parent):
        super(Ui_renamemetoo, self).__init__()
        self.ui = Ui_renamemetoo()
        self.ui.setupUi(parent)

        # Redirect IDE console towards GUI console.
        sys.stdout = EmittingStream()
        sys.stdout.textWritten.connect(self.redirect_console_messages)
        sys.stderr = EmittingStream()
        sys.stderr.textWritten.connect(self.redirect_console_messages)

        # Initialize PC/instrument communication (MOVE SOMEWHERE ELSE?)
        self.T = TaborSE5082("USB0::0x168C::0x5082::0000218391::INSTR") # TIME CONSUMING.



   def redirect_console_messages(self, text):
       """All print() from the program are appended on a textEdit
          instead of the IDE console."""

        self.ui.Console_textEdit.append(text.rstrip("\n"))



    def close_program(self):
        """Call those functions after the user clicked on exit."""

        self.T.CLOSE()
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
        print("Program terminated.")

# ================================================
#      Program execution.
# ================================================

if __name__ == "__main__":

    # Define the app.
    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance()

    # Start the interface.
    Form = QtWidgets.QWidget()
    prog = RENAMEMELATER(Form)
    Form.show()

    # Handle what happens at program exit.
    app.setQuitOnLastWindowClosed(True)
    app.aboutToQuit.connect(prog.close_program)

    # Launch.
    app.exec()
大体上,我可以使用
app.aboutToQuit
关闭仪器。也许有某种类型的
应用程序.isDoneLoading
,我可以
。以相同的方式连接到我的仪器初始化


谢谢。

一项耗时5到10秒的任务非常繁重,因此除了不显示GUI之外,您还可以将其冻结,以便解决方案是在另一个线程中运行它:

def __init__(self, parent):

    # ...
    threading.Thread(target=self.callback, daemon=True).start()

def callback(self):
    self.T = TaborSE5082("USB0::0x168C::0x5082::0000218391::INSTR")
    # another code

与以下用户共享Ui\u renamemetoo classtry:
导入线程
..
线程(targetr=TaborSE5082,args=((“USB0::0x168C::0x5082::0000218391::INSTR”),daemon=True)。启动()
自动生成Ui,在测试线程解决方案之后,我将尝试清理并共享它。您是否使用变量self.T的数据更新GUI?查看我更新的代码,您可以访问该对象