Receita: BoaConstructor eventos e grid
Um simples uso do wx com boa-constructor que mostra evento do mouse, mudança de conteúdo de caixas de textos e utilização de grids.
módulo principal da aplicação
A maioria dos códigos já são gerados automaticamente pelo boa, porém existem algumas incompatibilidades que vou comentar adiante.
1 #Boa:App:BoaApp
2
3 import wx
4
5 import Frame1
6 #o frame2 em modules foi adicionado mas não possui
7 #muita finalidade
8 modules ={'Frame1': [1, 'Main frame of Application', 'Frame1.py'],
9 'Frame2': [0, 'Grid', 'Frame2.py']}
10
11 class BoaApp(wx.App):
12 def OnInit(self):
13 wx.InitAllImageHandlers()
14 self.main = Frame1.create(None)
15 self.main.Show()
16 self.SetTopWindow(self.main)
17 return True
18
19 def main():
20 application = BoaApp(0)
21 application.MainLoop()
22
23 if __name__ == '__main__':
24 main()
Primeiro Frame
1 #Boa:Frame:Frame1
2
3 import wx
4 #importar o 2ºFrame
5 import Frame2
6
7 def create(parent):
8 return Frame1(parent)
9
10 [wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1PANEL1,
11 wxID_FRAME1STATICTEXT1, wxID_FRAME1STATICTEXT2, wxID_FRAME1TEXT1,
12 wxID_FRAME1TEXT2,
13 ] = [wx.NewId() for _init_ctrls in range(8)]
14
15 class Frame1(wx.Frame):
16 def _init_ctrls(self, prnt):
17 # generated method, don't edit
18 wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
19 pos=wx.Point(427, 265), size=wx.Size(289, 182),
20 style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
21 self.SetClientSize(wx.Size(281, 155))
22
23 self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
24 pos=wx.Point(0, 0), size=wx.Size(281, 148),
25 style=wx.TAB_TRAVERSAL)
26
27 self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
28 label='label1', name='staticText1', parent=self.panel1,
29 pos=wx.Point(48, 24), size=wx.Size(28, 13), style=0)
30
31 self.staticText2 = wx.StaticText(id=wxID_FRAME1STATICTEXT2,
32 label='label2', name='staticText2', parent=self.panel1,
33 pos=wx.Point(168, 24), size=wx.Size(28, 13), style=0)
34
35 self.text2 = wx.TextCtrl(id=wxID_FRAME1TEXT2, name='text2',
36 parent=self.panel1, pos=wx.Point(160, 40), size=wx.Size(100, 21),
37 style=0, value='DEF')
38
39 self.text1 = wx.TextCtrl(id=wxID_FRAME1TEXT1, name='text1',
40 parent=self.panel1, pos=wx.Point(40, 40), size=wx.Size(100, 21),
41 style=0, value='abc')
42
43 self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='executar',
44 name='button1', parent=self.panel1, pos=wx.Point(48, 96),
45 size=wx.Size(75, 23), style=0)
46 self.button1.SetToolTipString('executar')
47 self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
48 id=wxID_FRAME1BUTTON1)
49
50 self.button2 = wx.Button(id=wxID_FRAME1BUTTON2, label='Grid',
51 name='button2', parent=self.panel1, pos=wx.Point(160, 96),
52 size=wx.Size(75, 23), style=0)
53 self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button,
54 id=wxID_FRAME1BUTTON2)
55
56 def __init__(self, parent):
57 self._init_ctrls(parent)
58
59 #ao clicar no botão 1
60 def OnButton1Button(self, event):
61 a = self.text1.GetValue()
62 b = self.text2.GetValue()
63 self.text1.SetValue(b)
64 self.text2.SetValue(a)
65
66 #ao clicar no botão 2
67 def OnButton2Button(self, event):
68 frm2 = Frame2.create(None)
69 frm2.Show()
segundo frame
1 #Boa:Frame:Frame2
2
3 import wx
4 import wx.grid
5 import wx.calendar
6
7 def create(parent):
8 return Frame2(parent)
9
10 [wxID_FRAME2, wxID_FRAME2CALENDARCTRL1, wxID_FRAME2GRID1, wxID_FRAME2PANEL1,
11 ] = [wx.NewId() for _init_ctrls in range(4)]
12
13 class Frame2(wx.Frame):
14 def _init_ctrls(self, prnt):
15 # generated method, don't edit
16 wx.Frame.__init__(self, id=wxID_FRAME2, name='', parent=prnt,
17 pos=wx.Point(70, 40), size=wx.Size(892, 685),
18 style=wx.DEFAULT_FRAME_STYLE, title='Grid')
19 self.SetClientSize(wx.Size(884, 658))
20
21 self.panel1 = wx.Panel(id=wxID_FRAME2PANEL1, name='panel1', parent=self,
22 pos=wx.Point(0, 0), size=wx.Size(884, 658),
23 style=wx.TAB_TRAVERSAL)
24
25 self.calendarCtrl1 = wx.calendar.CalendarCtrl(date=wx.DateTime.Now(),
26 id=wxID_FRAME2CALENDARCTRL1, name='calendarCtrl1',
27 parent=self.panel1, pos=wx.Point(16, 16), size=wx.Size(172, 137),
28 style=wx.calendar.CAL_SHOW_HOLIDAYS)
29
30 self.grid1 = wx.grid.Grid(name='grid1', parent=self.panel1,
31 pos=wx.Point(16, 168), size=wx.Size(100, 100), style=1)
32
33 def __init__(self, parent):
34
35 self._init_ctrls(parent)
36 self.grid1.CreateGrid(1, 1, self.grid1.wxGridSelectCells)
37 dados = self.dados()
38 self.grid1.AppendCols(len(dados[1])-1)
39 self.grid1.AppendRows(len(dados)-1)
40 for registro in range(len(dados)):
41 for dado in range(len(dados[registro])):
42 self.grid1.SetCellValue(registro , dado, dados[registro][dado])
43 self.grid1.SetCellValue(0 , 0, 'Testing')
44 self.grid1.SetColLabelValue(0,'label')
45 self.grid1.AutoSize()
46
47 def dados(self):
48 import seuJeitoDeObterDados.obterDados as obterDados
49 #retorna uma lista de listas.Ex:
50 #[['Josivaldo','22','M','aux.adm.'],['Marlene','19', 'F','secretária'],['Simone','20','F','Enc.DP']]
51 _dados = str(obterDados.obter())
52 return _dados
Volta para CookBook.
Sávio Rangel Sabino