Runs only some statements before #39;os.system()#39; is called(仅在调用#39;os.system()#39;之前运行某些语句)
问题描述
我正在编写一个Python程序,该程序具有使用PyQt5的GUI,并且还使用os.system()调用MATLAB文件。但是,我有一个函数决定在调用os.system之前不执行任何与PyQt相关的语句。像"print"这样的正常函数在os.system之前运行,但其他函数不会。但是,它会在调用os.system之后正常运行它们
Ex)
def button_clicked(self):
print("Button Clicked") #works
self.label.hide() #does nothing
os.system() #MATLAB called. works
self.label.hide() #now works
我完全不知道为什么会发生这种情况。真正奇怪的是,程序似乎忽略了os.system之前函数中与PyQt相关的所有内容,因为即使我执行以下操作:
def myFunction(self):
self.label.hide()
self.label2.hide()
print("myFunction Called")
def button_clicked(self):
self.label.hide() #does nothing
self.label2.hide() #does nothing
print("Button Clicked") #works
self.myFunction() #does everything in the function unless it's PyQt-related, in this instance, it prints "myFunction Called" and nothing else
os.system() #MATLAB called. Works
self.label.hide() #now works
self.label2.hide() #now works
os.system总是在函数或其他什么地方首先执行吗?但这并不能解释为什么像"打印"这样的东西是有效的。
推荐答案
os.system()
为阻止。这意味着它不会返回,直到它调用的命令实际返回(例如:程序退出)。
对于Qt这样的事件驱动系统而言,这是一个可怕的错误,因为阻塞函数会阻止事件循环处理所有事件:不仅是来自系统的事件(如键盘或鼠标交互),还包括它自己的事件,最重要的是小工具的绘图。
您的标签实际上对Qt的视点是隐藏的(您可以尝试print(self.label.isVisible())
),但是由于您要在那之后立即调用os.system
,Qt没有时间以可视方式更新小工具以反映这一点。
虽然通常线程化(根据需要,可以使用Python自己的模块或Qt的QThread,甚至可以使用QRunnable)是潜在阻塞操作的首选,但是由于您实际上想启动一个新程序,所以最简单的解决方案是使用Qprocess,也许可以使用它自己的静电方法startDetached()
。
这篇关于仅在调用';os.system()';之前运行某些语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:仅在调用';os.system()';之前运行某些语句


基础教程推荐
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01