-
Notifications
You must be signed in to change notification settings - Fork 21
/
PassArgsToCallback.py
executable file
·80 lines (64 loc) · 2.69 KB
/
PassArgsToCallback.py
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
#!/usr/bin/env python
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.SetBackgroundColour(wx.WHITE)
NumButtons = 6
MainSizer = wx.BoxSizer(wx.HORIZONTAL)
## The lambda trick:
ButtonSizer = wx.StaticBoxSizer(wx.StaticBox(self,label="lambda trick"),
wx.VERTICAL)
bdr = 10
for i in range(NumButtons):
b = wx.Button(self, label=str(i))
b.Bind(wx.EVT_BUTTON, lambda evt, num=i: self.OnClick(evt, num))
ButtonSizer.Add(b, 0, wx.EXPAND | wx.ALL, bdr)
MainSizer.Add(ButtonSizer, 0, wx.ALL, bdr)
## Using the label:
ButtonSizer = wx.StaticBoxSizer(wx.StaticBox(self,label="Using the label"),
wx.VERTICAL)
for i in range(NumButtons):
b = wx.Button(self, label=str(i))
b.Bind(wx.EVT_BUTTON, self.OnClick2)
ButtonSizer.Add(b, 0, wx.EXPAND | wx.ALL, bdr)
MainSizer.Add(ButtonSizer, 0, wx.ALL, bdr)
## Storing the ID:
ButtonSizer = wx.StaticBoxSizer(wx.StaticBox(self,label="Storing the ID"),
wx.VERTICAL)
self.ButtonIDs = {}
for i in range(NumButtons):
b = wx.Button(self, label=str(i))
self.ButtonIDs[b.Id] = i
b.Bind(wx.EVT_BUTTON, self.OnClick3)
ButtonSizer.Add(b, 0, wx.EXPAND | wx.ALL, bdr)
MainSizer.Add(ButtonSizer, 0, wx.ALL, bdr)
## Using FindWinowById():
ButtonSizer = wx.StaticBoxSizer(wx.StaticBox(self,label="FindWinowById()"),
wx.VERTICAL)
for i in range(NumButtons):
b = wx.Button(self, label=str(i))
b.Bind(wx.EVT_BUTTON, self.OnClick4)
# add an arbitrary attribute
b.myData = "This is button %i" %i
ButtonSizer.Add(b, 0, wx.EXPAND | wx.ALL, bdr)
MainSizer.Add(ButtonSizer, 0, wx.ALL, bdr)
self.SetSizerAndFit(MainSizer)
sz = self.GetSize()
self.SetSizeHints(sz[0], sz[1], sz[0], sz[1])
def OnClick(self, evt, num):
print("Button %i Clicked" %num)
def OnClick2(self, evt):
but = evt.GetEventObject()
print("Button %s Clicked" %but.Label)
def OnClick3(self, evt):
but = self.ButtonIDs[evt.Id]
print("Button %s Clicked" %but)
def OnClick4(self, evt):
but = wx.FindWindowById(evt.Id)
print("Button %s Clicked" %but.myData)
if __name__ == '__main__':
App = wx.App(False)
F = MyFrame(None, title="Test Frame")
F.Show()
App.MainLoop()