]>
Commit | Line | Data |
---|---|---|
6fa68946 RD |
1 | |
2 | from wxPython.wx import * | |
3 | from wxPython.xrc import * | |
4 | ||
5 | #---------------------------------------------------------------------- | |
6 | ||
7 | resourceText = r'''<?xml version="1.0"?> | |
8 | <resource> | |
9 | ||
10 | <!-- Notice that the class IS a standard wx class, and a custom | |
11 | subclass is specified as "moduleName.ClassName" --> | |
12 | ||
13 | <object class="wxPanel" subclass="wxXmlResourceSubclass.MyBluePanel" name="MyPanel"> | |
14 | <size>200,100</size> | |
15 | <object class="wxStaticText" name="label1" subclass="wxPython.wx.wxPreStaticText"> | |
16 | <label>This blue panel is a class derived from wxPanel | |
17 | and is loaded by a using a subclass attribute of the object tag.</label> | |
18 | <pos>10,10</pos> | |
19 | </object> | |
20 | </object> | |
21 | </resource> | |
22 | ''' | |
23 | ||
24 | #---------------------------------------------------------------------- | |
25 | ||
26 | class MyBluePanel(wxPanel): | |
27 | def __init__(self): | |
28 | #print "MyBluePanel.__init__" | |
29 | p = wxPrePanel() | |
30 | self.this = p.this | |
31 | self.thisown = p.thisown | |
32 | self._setOORInfo(self) | |
33 | ||
34 | EVT_WINDOW_CREATE(self, self.OnCreate) | |
35 | ||
36 | def OnCreate(self, evt): | |
37 | # This is the little bit of customization that we do for this | |
38 | # silly example. It could just as easily have been done in | |
39 | # the resource. We do it in the EVT_WINDOW_CREATE handler | |
40 | # because the window doesn't really exist yet in the __init__. | |
41 | self.SetBackgroundColour("BLUE") | |
42 | self.SetForegroundColour("WHITE") | |
43 | ||
44 | #---------------------------------------------------------------------- | |
45 | ||
46 | ||
47 | class TestPanel(wxPanel): | |
48 | def __init__(self, parent, log): | |
49 | self.log = log | |
50 | wxPanel.__init__(self, parent, -1) | |
51 | ||
52 | # make the components | |
53 | label = wxStaticText(self, -1, "The lower panel was built from this XML:") | |
54 | label.SetFont(wxFont(12, wxSWISS, wxNORMAL, wxBOLD)) | |
55 | ||
56 | text = wxTextCtrl(self, -1, resourceText, | |
57 | style=wxTE_READONLY|wxTE_MULTILINE) | |
58 | text.SetInsertionPoint(0) | |
59 | ||
60 | line = wxStaticLine(self, -1) | |
61 | ||
62 | # Load the resource | |
63 | res = wxEmptyXmlResource() | |
64 | res.LoadFromString(resourceText) | |
65 | ||
66 | # Now create a panel from the resource data | |
67 | panel = res.LoadPanel(self, "MyPanel") | |
68 | ||
69 | # and do the layout | |
70 | sizer = wxBoxSizer(wxVERTICAL) | |
71 | sizer.Add(label, 0, wxEXPAND|wxTOP|wxLEFT, 5) | |
72 | sizer.Add(text, 1, wxEXPAND|wxALL, 5) | |
73 | sizer.Add(line, 0, wxEXPAND) | |
74 | sizer.Add(panel, 1, wxEXPAND|wxALL, 5) | |
75 | ||
76 | self.SetSizer(sizer) | |
77 | self.SetAutoLayout(True) | |
78 | ||
79 | ||
80 | #---------------------------------------------------------------------- | |
81 | ||
82 | def runTest(frame, nb, log): | |
83 | win = TestPanel(nb, log) | |
84 | return win | |
85 | ||
86 | #---------------------------------------------------------------------- | |
87 | ||
88 | ||
89 | ||
90 | overview = """<html><body> | |
91 | <h2><center>wxXmlResourceSubclass</center></h2> | |
92 | ||
93 | Sometimes it is necessary to use custom classes, but you still want | |
94 | them to be created from XRC. The subclass XRC attribute allows you to | |
95 | do that. | |
96 | ||
97 | </body></html> | |
98 | """ | |
99 | ||
100 | ||
101 | ||
102 | if __name__ == '__main__': | |
103 | import sys,os | |
104 | import run | |
105 | run.main(['', os.path.basename(sys.argv[0])]) | |
106 |