2013-02-07 69 views
0

我一直無法定義使用XRC描述自己的對話框。使用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(個體, 'btn_1')

(因爲我讀here在於)

的PostCreate方法是 用於將pre的膽量轉化爲自我,因此它的行爲就像是ConfigDialog的真實實例 。

但他們都沒有工作。

你能指出我正確的方向嗎?

+1

這可能有所幫助:http://wxpython-users.1045709.n5.nabble.com/XRC-Simple-Dialog-td2348939.html –

+0

謝謝。它幫助了我! –

回答

0

我解決了這個問題,使用2步創建記錄here

這裏的一個小例子

# 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)