通过单击另一个类中的按钮,从面板中的事件处理程序关闭wx.Dialog

时间:2018-04-27 21:14:23

标签: event-handling wxpython

我无法在以下ButtonsPanel类中获取按钮事件(保存,关闭)以在AddModFormContractors类中触发它们的事件处理程序。

以下命令:

pnlButtons = ButtonsPanel(parent=self.frm_pnl)

正在传递一个PANEL作为ButtonsPanel的父节点,如果我绑定到“parent.onClose”,它会在运行时失败绑定。试图传递“自我”根本不会在框架上呈现任何内容。在下面的代码中运行它会抛出一个异常,即onClose缺少第二个(事件)参数。

我怎么能让它工作呢?

class ButtonsPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        btnSizer = wx.BoxSizer(wx.HORIZONTAL)

        okBtn = wx.Button(self, label="Save")
        okBtn.Bind(wx.EVT_BUTTON, AddModFormContractors.onSaveRecord)
        btnSizer.Add(okBtn, 0, wx.ALL, 5)
        cancelBtn = wx.Button(self, label="Close")
        cancelBtn.Bind(wx.EVT_BUTTON, AddModFormContractors.onClose)
        btnSizer.Add(cancelBtn, 0, wx.ALL, 5)

        self.SetSizer(btnSizer)
        self.Layout()



class AddModFormContractors(wx.Dialog):

    def __init__(self, targetObject, row=None, titlePrefix="Add", titleLabel="Record", addRecord=True):

        wx.Dialog.__init__(self, None, title="{} {}".format(titlePrefix, titleLabel), size=(730, -1))

        self.targetObject = targetObject
        self.addRecord = addRecord
        self.selectedRow = row

        ... DOING RECORD STUFF HERE ...

        #Construct the form
        self.frm_pnl = wx.Panel(self)
        self.frm_pnl.BackgroundColour = (235, 230, 220)  # BEIGE

        font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)
        lbl = wx.StaticText(self.frm_pnl, label="{} {}".format(titlePrefix, titleLabel))
        lbl.SetFont(font)

        pnlCompany = CompanyPanel(parent=self.frm_pnl, record=recordToUpdate)
        pnlRecord = RecordPanel(parent=self.frm_pnl, record=recordToUpdate)
        pnlButtons = ButtonsPanel(parent=self.frm_pnl)

        self.frmPnl_vertSizer = wx.BoxSizer(wx.VERTICAL)

        ... BUILDING THE SIZER HERE ...

        self.frmPnl_vertSizer.Layout()
        self.frm_pnl.SetSizer(self.frmPnl_vertSizer)


        wx.CallAfter(self.SetFrameSize)

    def onClose(self, event):

        print("Closing")
        self.Destroy()


    def onSaveRecord(self, event=None):
        if self.Validate():
            if self.addRecord:
                dictRecordValues = getValuesFromForm(self, self.olv_dict)
                forms_controller.insertRecord(self.targetObject, dictRecordValues)
            else:
                dictRecordValues = getValuesFromForm(self, self.olv_dict, False)
                forms_controller.updateRecordById(self.targetObject, self.recordId, dictRecordValues)
            # self.titleTxt.SetFocus()

            self.Destroy()

1 个答案:

答案 0 :(得分:0)

我最终将事件处理程序的类对象作为参数传递给持有按钮的容器(buttonsPanel),在那里我可以引用父框架中的事件处理程序。这里传递的参数名称是dialogFrame:

class ButtonsPanel(wx.Panel):
    """ Just a button panel """

    def __init__(self, parent, dialogFrame):
        wx.Panel.__init__(self, parent)
        self.parent = parent
        self.dialogFrame = dialogFrame

        """
        Since the events affect another class frame(dialog), we pass the dialogFrame to this function.
        We demonstrate two binding/invokation ways here:
        okBtn - biding to the handler in dialogFrame class.
        cancelBtn - binding to this class handler, closing the frame by referencing frameDialog
        """

        btnSizer = wx.BoxSizer(wx.HORIZONTAL)

        okBtn = wx.Button(self, label="Save")
        okBtn.Bind(wx.EVT_BUTTON, self.dialogFrame.onSaveRecord)
        btnSizer.Add(okBtn, 0, wx.ALL, 5)
        self.cancelBtn = wx.Button(self, label="Close")
        self.Bind(wx.EVT_BUTTON, self.onClose)
        btnSizer.Add(self.cancelBtn, 0, wx.ALL, 5)

        self.SetSizer(btnSizer)
        self.Layout()

    def onClose(self, event):
        """
        Cancel the dialog
        """
        print("Closing")
        self.dialogFrame.Destroy()