返回一个值并关闭对话框。

时间:2021-03-15 23:01:18

I'm working on a user interface in PyQt, and I'm running into a few problems trying to use QDialog. Essentially I have a main widget and a sub-widget, saved in separate .py files; I would like the sub-widget to open when I click a certain button in the main widget. This seems to be opening fine.

我正在PyQt的一个用户界面上工作,我遇到了一些试图使用QDialog的问题。本质上,我有一个主小部件和一个子小部件,保存在单独的.py文件中;当我在主小部件中单击某个按钮时,我希望子小部件打开。这似乎是打开的好。

The issue comes with returning and closing. I have a "submit" button on my sub-widget - when the user clicks this button, I would like to return a value (a dictionary made from their input) to the main widget, and close the sub-widget. I can't seem to do either of these things with the code I have right now.

问题来了,回来和结束。我在子小部件上有一个“提交”按钮——当用户单击这个按钮时,我希望将一个值(由它们输入的字典)返回到主小部件,并关闭子小部件。我似乎不能用我现在的代码来做这些事情。

Applicable bits of code in the main widget (can add more to make it self-contained if the problem isn't obvious):

在主小部件中可以使用的代码片段(如果问题不明显,可以添加更多的代码使其自包含):

import SGROIWidget_ui

def retranslateUi(self, ROIGUI):
    #ShowGroupROI is a push-button
    self.ShowGroupROI.clicked.connect(self.ShowGroupROIFunction)

def ShowGroupROIFunction(self):
    dialog = QDialog()
    dialog.ui = SGROIWidget_ui.Ui_ShowGroupWidget()
    dialog.ui.setupUi(dialog)
    dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
    if dialog.exec_():
        roiGroups=dialog.Submitclose()
        print(roiGroups)
        dialog.accept()

I never seem to hit the code after the if-statement.

我似乎从来没有在if-statement之后打过代码。

The applicable code from my sub-widget (will include a bit more here):

来自我的子小部件的适用代码(将包含更多内容):

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_ShowGroupWidget(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, ShowGroupWidget):
        #sets up Submit button

    def retranslateUi(self, ShowGroupWidget):
        self.Submit.clicked.connect(self.Submitclose)

     def Submitclose(self):
        roiGroups={}
        #roiGroups gets set up here as a dictionary
        #It prints nicely from here so I know it's not the issue

        return roiGroups 
        #I don't know if I can just do a return statement like this?
        self.close()*

*I have tried ex.close() here as well but ex is not recognized when this widget is run as a dialog. It doesn't seem like it should get to this line because of the return statement, but I don't know how else to close this widget after the user hits "submit". Or should the dialog.accept() in my main widget handle that?

我已经尝试过ex.close(),但是在这个小部件作为一个对话框运行时,ex并没有被识别。由于返回语句的原因,它似乎不应该到达这一行,但是我不知道在用户点击“submit”之后,如何关闭这个小部件。还是应该在我的主小部件中接受()?

One last thing - do I need this in my sub-widget at all, since it's being run through my main widget instead?

最后一件事——我在子小部件中是否需要它,因为它正在通过我的主小部件运行?

if __name__=='__main__':
    app=QtGui.QApplication(sys.argv)
    ex=Ui_ShowGroupWidget()
    ex.show()
    sys.exit(app.exec_())

Thanks in advance! I am pretty new to PyQt so hopefully this is somewhat legible.

提前谢谢!我是PyQt的新手,希望这是可以理解的。

1 个解决方案

#1


12  

There are a few issues. The if dialog.exec_(): line will only succeed if the dialog is exited with accept(). Are you working with QDesigner? If so, check this for a different way of working. If Ui_ShowGroupWidget just contain code you write, you should make it inherit QDialog instead of QWidget. Then, instead of closing it with self.close(), you close it with self.accept(). You can't return a diccionary, but you can save it as an object attribute. Once the dialog.exec_() returns, you can access that attribute.

有几个问题。如果对话框被接受()退出,则该对话框只会成功。你和QDesigner一起工作吗?如果是这样的话,检查一下是否有不同的工作方式。如果Ui_ShowGroupWidget只包含你写的代码,你应该让它继承QDialog而不是QWidget。然后,与其用self。close()关闭它,不如用self。accept()来关闭它。您不能返回一个dicci,但您可以将其保存为对象属性。一旦dialog.exec_()返回,您就可以访问该属性。

It could be something like this:

可能是这样的:

def ShowGroupROIFunction(self):
    dialog = SGROIWidget_ui.Ui_ShowGroupWidget()
    if dialog.exec_():
        print(dialog.roiGroups)

The other one:

另一个:

...

class Ui_ShowGroupWidget(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.roiGroups = {}
        self.Submit.clicked.connect(self.submitclose)

    def setupUi(self, ShowGroupWidget):
        #sets up Submit button

    def submitclose(self):
        #do whatever you need with self.roiGroups    
        self.accept()

At last, if __name__=='__main__': means "if this file is executed as the main file, then..", which is not the case as you are including and using it from another one. So you can remove it, however, the idea is that you can run python ui_mywidget.py in order to test it or see the Ui defined on that file

最后,如果__name__=='__main__':表示“如果该文件作为主文件执行”。,这并不是你的情况,而是你从另一个案例中使用它。因此,您可以删除它,但是,这个想法是您可以运行python ui_mywidget。py为测试它或查看该文件上定义的Ui。

#1


12  

There are a few issues. The if dialog.exec_(): line will only succeed if the dialog is exited with accept(). Are you working with QDesigner? If so, check this for a different way of working. If Ui_ShowGroupWidget just contain code you write, you should make it inherit QDialog instead of QWidget. Then, instead of closing it with self.close(), you close it with self.accept(). You can't return a diccionary, but you can save it as an object attribute. Once the dialog.exec_() returns, you can access that attribute.

有几个问题。如果对话框被接受()退出,则该对话框只会成功。你和QDesigner一起工作吗?如果是这样的话,检查一下是否有不同的工作方式。如果Ui_ShowGroupWidget只包含你写的代码,你应该让它继承QDialog而不是QWidget。然后,与其用self。close()关闭它,不如用self。accept()来关闭它。您不能返回一个dicci,但您可以将其保存为对象属性。一旦dialog.exec_()返回,您就可以访问该属性。

It could be something like this:

可能是这样的:

def ShowGroupROIFunction(self):
    dialog = SGROIWidget_ui.Ui_ShowGroupWidget()
    if dialog.exec_():
        print(dialog.roiGroups)

The other one:

另一个:

...

class Ui_ShowGroupWidget(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.roiGroups = {}
        self.Submit.clicked.connect(self.submitclose)

    def setupUi(self, ShowGroupWidget):
        #sets up Submit button

    def submitclose(self):
        #do whatever you need with self.roiGroups    
        self.accept()

At last, if __name__=='__main__': means "if this file is executed as the main file, then..", which is not the case as you are including and using it from another one. So you can remove it, however, the idea is that you can run python ui_mywidget.py in order to test it or see the Ui defined on that file

最后,如果__name__=='__main__':表示“如果该文件作为主文件执行”。,这并不是你的情况,而是你从另一个案例中使用它。因此,您可以删除它,但是,这个想法是您可以运行python ui_mywidget。py为测试它或查看该文件上定义的Ui。