kevinw / wxpy

faster leaner wxWidgets Python bindings

This URL has Read+Write access

wxpy / widgettest.py
100644 136 lines (98 sloc) 3.296 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import sys
import weakref
 
import sip
import wx
 
def message(e):
    print 'click!', e.GetEventObject(), id(e.GetEventObject())
 
def prnt(*args):
    print ' '.join(str(a) for a in args)
 
def construct_layout(f):
    b = wx.Button(f, -1, 'test')
    b2 = wx.Button(f, -1, 'test2')
 
    s = f.Sizer = wx.BoxSizer(wx.VERTICAL)
    
    item1 = s.Add(b)
    sip.dump(item1)
    item2 = s.Add(b2)
 
    s2 = f.Sizer = wx.BoxSizer(wx.VERTICAL)
    sip.dump(s)
    
def main():
    a = wx.PySimpleApp()
    f = wx.Frame(None)
    construct_layout(f)
    import gc
    gc.collect()
    f.Fit()
    f.Show()
    a.MainLoop()
 
def main2():
      a = wx.PySimpleApp()
 
      f = wx.Frame(None, -1, u'wxpy frame', (40, 40), (400, 300))
      p = wx.Panel(f)
      p.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
 
      f.Bind(wx.EVT_ACTIVATE, lambda e: prnt('frame activate:', f.IsActive()))
 
      def onclose(e):
          print 'destroying frame', f
          print 'BEFORE: isdeleted?', sip.isdeleted(f)
          print sip.dump(f)
          f.Destroy()
          #sip.setdeleted(f)
          print 'AFTER: isdeleted?', sip.isdeleted(f)
          print sip.dump(f)
 
      f = wx.Frame(None, -1, u'wxpy frame', (40, 40), (400, 300))
      f.Bind(wx.EVT_CLOSE, onclose)
      f.Bind(wx.EVT_ACTIVATE, lambda e: p('frame activate:', f.IsActive()))
 
      s = wx.BoxSizer(wx.VERTICAL)
 
      def show_modal(e):
          diag = wx.Dialog(f, -1, 'test modal dialog', size = (400, 300))
          try:
              diag.ShowModal()
          finally:
              diag.Destroy()
          import gc
          print 'collect', gc.collect()
 
      button = wx.Button(p, -1, 'Show Modal Dialog')
      button.Bind(wx.EVT_BUTTON, show_modal)
 
      print 'button id is', id(button)
 
      def on_paint(e):
          dc = wx.AutoBufferedPaintDC(p)
 
          dc.SetBrush(wx.WHITE_BRUSH)
          dc.SetPen(wx.TRANSPARENT_PEN)
          dc.DrawRectangleRect(p.GetClientRect())
 
          dc.SetBrush(wx.Brush(wx.Colour(255, 0, 0)))
          rect = wx.Rect(20, 20, 60, 70)
 
          if dc.IsOk():
              font = dc.GetFont()
              font.SetPointSize(20)
              font.SetWeight(wx.FONTWEIGHT_BOLD)
              dc.SetFont(font)
 
              dc.DrawText("hello, world!", 130, 100)
 
              b = wx.Brush(wx.Colour(255, 0, 0))
              dc.SetBrush(b)
              b = dc.GetBrush()
 
              dc.DrawRectangle(140, 130, 60, 30)
          else:
              print 'not ok!'
 
      #f.Connect(-1, -1, 10090, on_paint)
      f.Bind(wx.EVT_PAINT, on_paint)
 
      static_text = wx.StaticText(p, -1, 'Static Text')
      checkbox = wx.CheckBox(p, -1, 'Checkbox')
 
      def print_top(e):
          print wx.GetTopLevelWindows()
 
      checkbox.Bind(wx.EVT_CHECKBOX, print_top)
 
      s.Add(static_text, 0, wx.ALL, 6)
      s.Add(button, 0, wx.ALL, 6)
      s.Add(checkbox, 0, wx.ALL, 6)
 
      s.Add(wx.TextCtrl(p, -1, 'Text Control', name = 'Username'))
 
      p.SetSizer(s)
 
      a.Bind(wx.EVT_MENU, lambda e: a.ExitMainLoop(), id = wx.ID_EXIT)
 
 
      a.SetTopWindow(f)
      f.Show()
 
      #
      # sys.a=a
 
      print '--> calling MainLoop'
      import sip
      sip.dump(wx.GetApp())
      a.MainLoop()
      print '--> MainLoop returned'
 
if __name__ == '__main__':
    main()