WxPython FAQ Using xml resource files

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(Новая: The idea behind xml resources is to separate the interface from the code of an application. Several GUI builders use this concept for creating interfaces. For example the famous Glade. ...)
(Удалено по требованию автора...)
 
(1 промежуточная версия не показана)
Строка 1: Строка 1:
-
The idea behind xml resources is to separate the interface from the code of an application. Several GUI builders use this concept for creating interfaces. For example the famous Glade.
 
-
In our example we create a simple frame window with one button. We load resources from a file, load a panel and bind an event to a button.
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
# xml.py
 
-
 
-
import  wx
 
-
import  wx.xrc  as  xrc
 
-
 
-
 
-
class Xml(wx.Frame):
 
-
    def __init__(self, parent, id, title):
 
-
        wx.Frame.__init__(self, parent, id, title)
 
-
 
-
        res = xrc.XmlResource('resource.xrc')
 
-
        res.LoadPanel(self, 'MyPanel')
 
-
 
-
        self.Bind(wx.EVT_BUTTON, self.OnClose,  id=xrc.XRCID('CloseButton'))
 
-
        self.Center()
 
-
        self.Show(True)
 
-
 
-
 
-
    def OnClose(self, event):
 
-
        self.Close()
 
-
 
-
app = wx.App()
 
-
Xml(None, -1, 'xml.py')
 
-
app.MainLoop()
 
-
</source>
 
-
 
-
This is resource file resource.xrc It is a xml file, where we define our widgets and their patterns. In thid file, we use tags like <pre>&lt;object&gt;&lt;/object&gt;, &lt;item&gt;&lt;/item&gt;</source> etc.
 
-
 
-
<source lang="python">
 
-
&lt;?xml version="1.0" ?&gt;
 
-
&lt;resource&gt;
 
-
  &lt;object class="wxPanel" name="MyPanel"&gt;
 
-
  &lt;object class="wxButton" name="CloseButton"&gt;
 
-
    &lt;label&gt;Close&lt;/label&gt;
 
-
 
-
    &lt;pos&gt;15,10&lt;/pos&gt;
 
-
  &lt;/object&gt;
 
-
  &lt;/object&gt;
 
-
&lt;/resource&gt;
 
-
</source>
 
-
We use these two calls for working with widgets:
 
-
 
-
* XRCID(resource_name) - gives us the id of a button or menu item
 
-
* XRCCTRL(resource_name) - gives us the handlers of our widgets defined in resource file
 
-
 
-
[[Категория:wxWidgets]]
 
-
[[Категория:Python]]
 

Текущая версия на 12:00, 7 апреля 2009