【发布时间】:2022-01-07 20:10:23
【问题描述】:
我在一个 python 模块中创建了一个主窗口。该模块还包含一个类定义,用于创建名为 my_settings 的对象。
我还有一个单独的模块,它创建了另一个窗口(由于各种原因,它应该在不同的模块中)。此窗口允许各种输入,例如打开文件并在单击按钮后存储一些设置 blaa blaa。
然后我要做的是修改 my_settings 的属性。代码的简化版本是:
主要模块
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
QMainWindow.__init__(self)
uic.loadUi(self.some_directory + "arc_custom_main.ui", self)
self.some_button.clicked.connect(self.open_some_widget)
def open_some_widget(self):
widget = widget_in_other_module(self)
widget_in_other_module.exec_()
app = QApplication(sys.argv)
my_settings=settings()
_mainWindow = MainWindow()
_mainWindow.show()
独立模块
class widget_in_other_module(QDialog):
def __init__(self, parent):
my_settings.temppath = my_settings.OutputDir
QDialog.__init__(self)
self.parent = parent
my_settings.some_attribute= foo
uic.loadUi("some.ui", self)
self.pushButtonOpenMain.clicked.connect(self.openMain)
def openMain(self):
my_settings.some_other_attibute=bar
我遇到的问题是在widget_in_other_module 的openMain 方法中找不到访问my_settings 的方法。我对 qt 有点陌生,我一生都无法弄清楚我应该在哪里传递 my_settings。由于各种原因,我需要将 widget_in_other_module 的所有功能都放在主模块之外(主要与可读性和未来计划对主模块的更改有关)。我已经尽我所能尝试了,例如将其作为参数包含在 self.pushButtonOpenMain.clicked.connect(self.openMain,my_settings) 行中,但这似乎是不允许的。我在这里缺少一些基本的东西吗?
【问题讨论】: