【发布时间】:2019-01-10 10:34:20
【问题描述】:
我在 Qt5 中制作了一个仪器接口,它工作正常。唯一的问题是启动很慢,因为接口__init__包含用于连接仪器的耗时方法(5-10秒)。目前,几秒钟内什么都没有显示,然后整个界面显示“成功连接到仪器”消息已经写入其控制台(一个 textEdit 小部件)。
我想要立即显示界面,并且只有在显示后才应该启动通信协议。我确信这只是移动一条线的问题,但是我想不通。任何帮助表示赞赏。
这是程序结构的一个最小示例:
# ================================================
# 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 来关闭仪器。也许有某种app.isDoneLoading 我可以用同样的方式对我的仪器初始化.connect?
谢谢。
【问题讨论】:
-
分享你的Ui_renamemetoo类
-
试试:
import threading...threading.Thread(targer=TaborSE5082, args=("USB0::0x168C::0x5082::0000218391::INSTR", ) daemon=True ).start() -
Ui是自动生成的,等我测试线程解决方案后会尝试清理并分享。
-
您是否使用变量 self.T 的数据更新 GUI?
-
看我更新的代码,可以访问对象
标签: python python-3.x pyqt pyqt5