1

我在定义自己的使用 XRC 描述的对话框时遇到了麻烦。

我已经阅读了http://nebelhom.blogspot.co.nz/2012/04/xrc-and-wxdialogs.html和其他类似的来源告诉我这样做:

class ConfigDialog(wx.Dialog):
    def __init__(self, parent):
            self.res = xrc.XmlResource("config_dialog.xrc")
            pre = wx.PreDialog()
            self.res.LoadOnDialog(pre, parent, "ConfigDlg")
            self.PostCreate(pre)

            #Bind controls to events

但是,对于您如何将控件实际绑定到 ConfigDialog 类中定义的不同方法,我仍然很困惑。

我试过

self.btn_1 = xrc.XRCCTRL(self.frame, 'btn_1')

和 self.btn_1 = xrc.XRCCTRL(self, 'btn_1')

(因为我在这里读到)

PostCreate 方法用于将 pre 的胆量转移到 self 中,因此它的行为就像是 ConfigDialog 的真实实例。

但他们都没有工作。

你能指出我正确的方向吗?

4

1 回答 1

0

我使用此处记录的 2 步创建解决了该问题。

这是一个小例子

# Import both wx main module and the xrc module
import wx
import wx.xrc as xrc

class MyDialog(wx.Dialog):
    """
    This is our dialog. XRC will create it's widgets and all we need to do is
    handle the events.
    """
    def __init__(self, parent, res):

        pre = wx.PreDialog()
        self.PostCreate(pre)

        res.LoadOnDialog(self, None, "MyDialog")

        #Binding events

        self.Bind(wx.EVT_BUTTON, self.on_ok, xrc.XRCCTRL(self, "okButton"))
        self.Bind(wx.EVT_BUTTON, self.on_cancel, xrc.XRCCTRL(self, "cancelButton"))

    def on_ok(self, event):
        """Show a message box informing us we pressed the OK button"""
        msgDlg = wx.MessageDialog(self, "OK pressed", style=wx.OK)
        msgDlg.ShowModal()
        msgDlg.Destroy()
        self.Destroy()

    def on_cancel(self, event):
        """Show a message box informing us we pressed the Cancel button"""
        msgDlg = wx.MessageDialog(self, "Cancel pressed", style=wx.OK)
        msgDlg.ShowModal()
        msgDlg.Destroy()
        self.Destroy()


# Create the simplest wxApp object
app = wx.PySimpleApp()

# Load the XRC resources
res = xrc.XmlResource("example.xrc")

# Show and run the dialog
dlg = MyDialog(None, res)
dlg.ShowModal() 

和 XRC 文件:

<?xml version="1.0" encoding="cp1255"?>
<resource>
  <object class="wxDialog" name="MyDialog">
    <title></title>
    <object class="wxBoxSizer">
      <orient>wxVERTICAL</orient>
      <object class="sizeritem">
        <object class="wxStaticText">
          <label>Just a little bit of text to make
the dialog a little bit less empty
than it has to be for a simple
example.</label>
          <font>
            <size>12</size>
            <family>default</family>
            <style>normal</style>
            <weight>normal</weight>
            <underlined>0</underlined>
          </font>
        </object>
        <flag>wxALL</flag>
        <border>5</border>
      </object>
      <object class="spacer">
        <size>0,20</size>
      </object>
      <object class="sizeritem">
        <object class="wxBoxSizer">
          <orient>wxHORIZONTAL</orient>
          <object class="spacer">
            <option>1</option>
          </object>
          <object class="sizeritem">
            <object class="wxButton" name="okButton">
              <label>OK</label>
            </object>
            <flag>wxALL</flag>
            <border>5</border>
          </object>
          <object class="sizeritem">
            <object class="wxButton" name="cancelButton">
              <label>Cancel</label>
            </object>
            <flag>wxALL</flag>
            <border>5</border>
          </object>
        </object>
        <flag>wxEXPAND</flag>
      </object>
    </object>
  </object>
</resource> 

最后,一个小提示:要结束对话,请使用 EndModal(id)

self.EndModal(wx.ID_OK)
于 2013-02-22T03:35:39.253 回答