【发布时间】:2020-09-14 09:28:08
【问题描述】:
通常,我需要在 QtDesigner 中设计的 PyQt5 GUI 中完成一些任务,这些任务足够小,我想将所有代码保存在一个 .py 文件中。因此,考虑这个 PyQt5 示例:
test3.py:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui, uic
mod_name = vars(sys.modules[__name__])['__package__'] # SO:1389044
code_exec_as = 'module named {}'.format(mod_name) if mod_name else 'script'
cmdline = "{} {}".format( sys.executable, " ".join(sys.argv) )
try: # SO:6038898
reloading
except NameError:
reloading = False # means the module is being imported
else:
reloading = True # means the module is being reloaded
print("Starting: code executed as {}; reloading {}; command line '{}'".format(code_exec_as, reloading, cmdline))
class MyCustomButton(QtWidgets.QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
print("MyCustomButton init!")
class MyMainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyMainWindow, self).__init__()
uic.loadUi("test3.ui", self)
self.show()
def main():
app = QtWidgets.QApplication(sys.argv)
window = MyMainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
test3.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>382</width>
<height>232</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="MyCustomButton" name="pushButton_2">
<property name="text">
<string>CustomPushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>382</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>MyCustomButton</class>
<extends>QPushButton</extends>
<header>test3</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
当我从命令行运行它时,我得到:
$ python3 test3.py
Starting: code executed as script; reloading False; command line 'C:/msys64/mingw64/bin/python3.exe test3.py'
Starting: code executed as script; reloading False; command line 'C:/msys64/mingw64/bin/python3.exe test3.py'
MyCustomButton init!
请注意,“Starting: ...”打印输出已经运行了两次:显然:
- 第一次是由于脚本正常调用
- 第二次是由于
.ui中的pushButton_2被提升为MyCustomButton类,该类被列为在test3.py中定义,因此需要重新加载该文件,以便MyCustomButton类定义被读取
我能否从脚本内部(更具体地说,从导入之后但在任何类定义和 __main__ 调用之前的代码部分)以某种方式检测脚本正在运行的时间?
如你所见,我已经在test3.py 中尝试过一些东西,基于:
- how do I determine whether a python script is imported as module or run as script?
- How to tell if a Python modules I being reload()ed from within the module
...但是这些方法为脚本的两次“运行”提供了相同的输出,因此我无法使用它们来区分脚本是在第一次运行还是在第二次运行。
【问题讨论】:
-
为什么需要“检测”重新加载?
-
谢谢@musicamante - 这是因为我有一些导入在我的一些遵循这种模式的脚本中进行了一些硬件初始化,我宁愿在脚本运行时只运行一次该代码。
标签: python pyqt5 qt-designer