Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.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 跨多个窗口访问项目和属性_Python_Qt_Pyside2 - Fatal编程技术网

Python 跨多个窗口访问项目和属性

Python 跨多个窗口访问项目和属性,python,qt,pyside2,Python,Qt,Pyside2,我有一个主GUI窗口,在该窗口中,我用按钮单击打开一个新窗口(FCT弹出窗口): class MainWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.ui = Ui_MainWindow() # sets ui = to the main window from the ui-file self.ui.setupUi(self)

我有一个主GUI窗口,在该窗口中,我用按钮单击打开一个新窗口(FCT弹出窗口):

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()  # sets ui = to the main window from the ui-file
        self.ui.setupUi(self)
        [...]
    def enter_fct_results(self):
        self.FCTpopup = FCT_Window()
        self.FCTpopup.show()
在窗口中,我有一个可填充的QTable和一个用于提交数据并关闭窗口的按钮:

    class FCT_Window(QMainWindow):
    
     def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_FCT_Window()
        self.ui.setupUi(self)
        [...]

        self.ui.pushButton_submitFCT.clicked.connect(lambda: MainWindow.store_fct_data(MainWindow, self.on_submit()[0]))

     def on_submit(self):  # event when user clicks
                  fct_nparray = np.zeros((self.ui.tableFCTinputs.rowCount(), self.ui.tableFCTinputs.columnCount()))
                      for j in range(self.ui.tableFCTinputs.columnCount()):
                            for i in range(self.ui.tableFCTinputs.rowCount()):
                                fct_nparray[i, j] = float(self.ui.tableFCTinputs.item(i, j).text())
                  return  fct_nparray, lambda: self.close()
    
    self.ui.pushButton_submitFCT.clicked.connect(lambda: MainWindow.store_fct_data(MainWindow, self.on_submit()[0]))
主窗口中的接收功能如下所示:

def store_fct_data(self, data):
    self.fct_data = data
现在我只想了解如何使主窗口或打开第二个窗口的按钮被禁用。在内部禁用enter_fct_results()是可行的,但如果我想使用store_fct_数据或on_submit再次启用它,则会出现如下错误:

self.ui.pushButton_FCTresults.setEnabled(1)
self.ui.pushButton_submitFCT.clicked.connect(lambda: MainWindow.store_fct_data(MainWindow, self.on_submit()[0]))
AttributeError: type object 'MainWindow' has no attribute 'ui'
我想我还不明白如何处理多个窗口和其他东西。例如,如何使用window2中的按钮更改主窗口中按钮的颜色。如何访问小部件?如果我在同一扇窗户里,我很容易就能做到

self.ui.pushbutton.setText("New Text")
我不知道如何跨窗口访问项目和属性:(你能帮我吗?

访问另一个实例的属性 在
enter_fct_results
中禁用第二个窗口的按钮与在lambda中尝试的操作有根本区别:在第一种情况下,您访问的是实例属性(例如,
self.FCTpopup.ui.butdown
),而在第二种情况下,您尝试访问的是类属性

self.ui
对象是在
\uuuuu init\uuuuu
中创建的(创建类实例时),因此实例将具有
ui
属性,而不是类:

class Test:
    def __init__(self):
        self.value = True

test = Test()
print(test.value)

>>> True

print(Test.value)

>>> AttributeError: type object 'Test' has no attribute 'value'
提供参考 简单的解决方案是为第二个窗口创建第一个窗口实例的引用:

   def enter_fct_results(self):
        self.FCTpopup = FCT_Window(self)
        self.FCTpopup.show()

class FCT_Window(QMainWindow):
    def __init__(self, mainWindow):
        QMainWindow.__init__(self)
        self.mainWindow = mainWindow
        self.ui.pushButton_submitFCT.clicked.connect(self.doSomething)

    def doSomething(self):
        # ...
        self.mainWindow.ui.pushButton.setEnabled(True)

如上所述,必须使用QDialog(而不是QMainWindow)在designer中重新创建ui。您只需创建一个新的界面,并从原始界面拖放小部件即可。

我终于发现了我的错误:它是信号连接的地方。必须在第二个窗口打开之前:

def enter_fct_results(self):
    self.FCTpopup = Dialog(self.fct_data)
    self.FCTpopup.submitted.connect(self.store_fct_data)
    self.FCTpopup.exec_()
现在,我可以将存储的数据从主窗口发送到打开的窗口,导入到表中,编辑并在提交时发送回主窗口。

请提供
def enter_fct_results(self):
    self.FCTpopup = Dialog(self.fct_data)
    self.FCTpopup.submitted.connect(self.store_fct_data)
    self.FCTpopup.exec_()