Wednesday, January 30, 2008

A little bit more complex

I've been trying out to add more control in an wxPython application. So far it has gone very well. My progress is slow and my code is a big jumbled up mess. But here's what I've got so far.


#!/usr/bin/env python

import wx

class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,wx.ID_ANY,'Item Tracker',size=(400,550))
panel = wx.Panel(self, wx.ID_ANY)
mainsizer=wx.BoxSizer()
leftsizer=wx.BoxSizer(wx.VERTICAL)
rightsizer=wx.BoxSizer(wx.VERTICAL)

itemlist = wx.ListBox(panel,wx.ID_ANY,choices = ['Baju','Seluar','Almari'])
itemlist.Bind(wx.EVT_LISTBOX,self.SelectedItem)

addbutton = wx.Button(panel,wx.ID_ANY,"Add Item")

leftsizer.Add(itemlist,1,wx.EXPAND|wx.ALL,5)
leftsizer.Add(addbutton)
leftsizer.RecalcSizes()

savebutton = wx.Button(panel,wx.ID_ANY,"Save")

namelabel=wx.StaticText(panel,wx.ID_ANY,"Name :")
self.nametxt=wx.TextCtrl(panel,wx.ID_ANY)
datelabel=wx.StaticText(panel,wx.ID_ANY,"Date :")
self.datetxt=wx.DatePickerCtrl(panel,wx.ID_ANY)
rightform=wx.GridSizer(2,2)
rightform.AddMany(((namelabel),(self.nametxt,3,wx.EXPAND|wx.ALL|wx.SHAPED,2),(datelabel),(self.datetxt)))
rightsizer.Add(rightform,1,wx.EXPAND|wx.ALL,2)
rightsizer.Add(savebutton)

mainsizer.Add(leftsizer,1,wx.EXPAND|wx.ALL,2)
mainsizer.Add(rightsizer,2,wx.EXPAND|wx.ALL,2)

panel.SetSizer(mainsizer)

def SelectedItem(self,event):
dd=wx.MessageDialog(None,"Selected","The one",wx.ID_OK)
dd.ShowModal()

class App(wx.App):

def OnInit(self):
frame=MainFrame()
frame.Show()
return True

if __name__ == '__main__':
app = App()
app.MainLoop()


It doesn't do much of anything yet. Just a bunch of controls. The new thing that I've learned is to use AddMany to the sizer. It is quite convenient that way.

0 comments: