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
#Boa:App:BoaApp import wx import Frame1 modules ={'Frame1': [1, 'Main frame of Application', 'Frame1.py'], 'Frame2': [0, 'Grid', 'Frame2.py']} class BoaApp(wx.App): def OnInit(self): wx.InitAllImageHandlers() self.main = Frame1.create(None) self.main.Show() self.SetTopWindow(self.main) return True def main(): application = BoaApp(0) application.MainLoop() if __name__ == '__main__': main()
Primeiro Frame
#Boa:Frame:Frame1 import wx import Frame2 def create(parent): return Frame1(parent) [wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1PANEL1, wxID_FRAME1STATICTEXT1, wxID_FRAME1STATICTEXT2, wxID_FRAME1TEXT1, wxID_FRAME1TEXT2, ] = [wx.NewId() for _init_ctrls in range(8)] class Frame1(wx.Frame): def _init_ctrls(self, prnt): # generated method, don't edit wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt, pos=wx.Point(427, 265), size=wx.Size(289, 182), style=wx.DEFAULT_FRAME_STYLE, title='Frame1') self.SetClientSize(wx.Size(281, 155)) self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self, pos=wx.Point(0, 0), size=wx.Size(281, 148), style=wx.TAB_TRAVERSAL) self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1, label='label1', name='staticText1', parent=self.panel1, pos=wx.Point(48, 24), size=wx.Size(28, 13), style=0) self.staticText2 = wx.StaticText(id=wxID_FRAME1STATICTEXT2, label='label2', name='staticText2', parent=self.panel1, pos=wx.Point(168, 24), size=wx.Size(28, 13), style=0) self.text2 = wx.TextCtrl(id=wxID_FRAME1TEXT2, name='text2', parent=self.panel1, pos=wx.Point(160, 40), size=wx.Size(100, 21), style=0, value='DEF') self.text1 = wx.TextCtrl(id=wxID_FRAME1TEXT1, name='text1', parent=self.panel1, pos=wx.Point(40, 40), size=wx.Size(100, 21), style=0, value='abc') self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='executar', name='button1', parent=self.panel1, pos=wx.Point(48, 96), size=wx.Size(75, 23), style=0) self.button1.SetToolTipString('executar') self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button, id=wxID_FRAME1BUTTON1) self.button2 = wx.Button(id=wxID_FRAME1BUTTON2, label='Grid', name='button2', parent=self.panel1, pos=wx.Point(160, 96), size=wx.Size(75, 23), style=0) self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button, id=wxID_FRAME1BUTTON2) def __init__(self, parent): self._init_ctrls(parent) #ao clicar no botão 1 def OnButton1Button(self, event): a = self.text1.GetValue() b = self.text2.GetValue() self.text1.SetValue(b) self.text2.SetValue(a) #ao clicar no botão 2 def OnButton2Button(self, event): frm2 = Frame2.create(None) frm2.Show()
segundo frame
#Boa:Frame:Frame2 import wx import wx.grid import wx.calendar def create(parent): return Frame2(parent) [wxID_FRAME2, wxID_FRAME2CALENDARCTRL1, wxID_FRAME2GRID1, wxID_FRAME2PANEL1, ] = [wx.NewId() for _init_ctrls in range(4)] class Frame2(wx.Frame): def _init_ctrls(self, prnt): # generated method, don't edit wx.Frame.__init__(self, id=wxID_FRAME2, name='', parent=prnt, pos=wx.Point(70, 40), size=wx.Size(892, 685), style=wx.DEFAULT_FRAME_STYLE, title='Grid') self.SetClientSize(wx.Size(884, 658)) self.panel1 = wx.Panel(id=wxID_FRAME2PANEL1, name='panel1', parent=self, pos=wx.Point(0, 0), size=wx.Size(884, 658), style=wx.TAB_TRAVERSAL) self.calendarCtrl1 = wx.calendar.CalendarCtrl(date=wx.DateTime.Now(), id=wxID_FRAME2CALENDARCTRL1, name='calendarCtrl1', parent=self.panel1, pos=wx.Point(16, 16), size=wx.Size(172, 137), style=wx.calendar.CAL_SHOW_HOLIDAYS) self.grid1 = wx.grid.Grid(name='grid1', parent=self.panel1, pos=wx.Point(16, 168), size=wx.Size(100, 100), style=1) def __init__(self, parent): self._init_ctrls(parent) self.grid1.CreateGrid(1, 1, self.grid1.wxGridSelectCells) dados = self.dados() self.grid1.AppendCols(len(dados[1])-1) self.grid1.AppendRows(len(dados)-1) for registro in range(len(dados)): for dado in range(len(dados[registro])): self.grid1.SetCellValue(registro , dado, dados[registro][dado]) self.grid1.SetCellValue(0 , 0, 'Testing') self.grid1.SetColLabelValue(0,'label') self.grid1.AutoSize() def dados(self): import seuJeitoDeObterDados.obterDados as obterDados #retorna uma lista de listas.Ex: #[['Josivaldo','22','M','aux.adm.'],['Marlene','19', 'F','secretária'],['Simone','20','F','Enc.DP']] _dados = str(obterDados.obter()) return _dados
Volta para CookBook.
Sávio Rangel Sabino