使用相同的功能访问子接口值

时间:2019-01-25 20:05:10

标签: python

我有两个类-主接口和子接口

有一个按钮会调出子界面,我试图直接(或几乎立即)获得子工具界面的输出,以便可以在按钮功能中使用它。

在我的代码中,如果执行以下操作:

  • 点击“点击我”
  • 选中2个选项,然后在子界面中点击“应用于选定的项目”
  • “我的字典值”的打印行仍然为空

除非我创建另一个函数get_results,然后在其中正确显示self.my_dict

如此,我该如何编码,一旦按下“应用...”按钮,self.my_dict将被更新而无需创建另一个函数?还是我只是想得太多?

class SubMenuWindow(QtGui.QWidget):
    def __init__(self, menu_items, parent=None):
        super(SubMenuWindow, self).__init__(parent)
        self.my_lyt = QtGui.QVBoxLayout()
        self.checked_options = []
        self.sel = {}

        for menu_name, submenu_name in menu_items.items():
            # Set the main menu item name
            self.groupbox = QtGui.QGroupBox(self)
            self.groupbox.setTitle(menu_name)
            self.groupbox.setLayout(QtGui.QVBoxLayout())
            self.my_lyt.addWidget(self.groupbox)

            if submenu_name:
                sub_txt = [action for action in submenu_name]
                for s in sub_txt:
                    sub_chk = QtGui.QCheckBox(s)
                    self.checked_options.append(sub_chk)
                    self.groupbox.layout().addWidget(sub_chk)

        apply_tag_btn = QtGui.QPushButton('Apply to selected item')
        apply_tag_btn.clicked.connect(self.get_checked_options)

        self.my_lyt.addWidget(apply_tag_btn)
        self.my_lyt.addStretch()

        self.setLayout(self.my_lyt)
        self.show()

    def get_checked_options(self):
        for f in self.checked_options:
            if f.isChecked():
                self.sel[f.parent().title()] = f.text()

class MainWin(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWin, self).__init__(parent)

        self.my_dict = {}

        btnA = QtGui.QPushButton('Click Me')
        btnA.clicked.connect(self.get_options)

        btnB = QtGui.QPushButton('Get results')
        btnB.clicked.connect(self.get_results)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(btnA)
        layout.addWidget(btnB)
        self.setLayout(layout)

    def get_options(self):
        sample_dict = {'GrpA' : ['John', 'Zack'], 'GrpB' : ['Alice', 'Phan']}
        self.subWin = SubMenuWindow(sample_dict)

        # I had want to get the values from subWin as soon as User has hit on
        # the 'Apply to selected item' button
        self.my_dict = self.subWin.sel
        print ">>> my dict values : ", self.my_dict
        # do something else from here thereafter...

    def get_results(self):
        print self.subWin.sel

1 个答案:

答案 0 :(得分:1)

创建新窗口不会阻塞,因此您的打印语句将在用户选择任何内容之前执行。您可以传递回调以在用户更改选择时通知调用窗口小部件。

例如:

class SubMenuWindow(QtWidgets.QWidget):
    def __init__(self, menu_items, parent=None, callback=None):
        super(SubMenuWindow, self).__init__(parent)
        self.callback = callback
        [...]

    def get_checked_options(self):
        for f in self.checked_options:
            if f.isChecked():
                self.sel[f.parent().title()] = f.text()
        if self.callback:
            self.callback()

并传递回调:

    def get_options(self):
        sample_dict = {'GrpA' : ['John', 'Zack'], 'GrpB' : ['Alice', 'Phan']}
        self.subWin = SubMenuWindow(sample_dict, callback=self.get_results)
        [...]

这样,只要用户单击get_result中的Apply按钮,就会调用SubMenuWindow方法。