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.

Monday, January 21, 2008

Simple Application

This is just another simple example of how to program using wxPython.


#!/usr/bin/env python

import random
import wx
import string

class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,wx.ID_ANY,'Multiply Tester',size=(300,100))
panel = wx.Panel(self,wx.ID_ANY)
self.randans()
self.question = wx.StaticText(panel,wx.ID_ANY,"%d x %d = " % (self.num1,self.num2))
self.answer = wx.TextCtrl(panel,wx.ID_ANY)
button = wx.Button(panel,wx.ID_OK)
button.Bind(wx.EVT_BUTTON,self.doanswer)
self.Bind(wx.EVT_UPDATE_UI,self.Update)

sizer=wx.BoxSizer()
sizer.Add(self.question)
sizer.Add(self.answer)

luar=wx.BoxSizer(wx.VERTICAL)
luar.Add(sizer)
luar.Add(button)
panel.SetSizer(luar)

def doanswer(self,event):
calc=self.num1*self.num2
if string.atoi(self.answer.Value)==calc:
dtext="You are completely right"
else:
dtext="Wrong. The answer is %d" % (calc)
dansw=wx.MessageDialog(None,dtext,'The answer is',wx.ID_OK)
dansw.ShowModal()
self.randans()

def randans(self):
self.num1 = random.randrange(0,99)
self.num2 = random.randrange(0,10)

def Update(self,event):
self.question.SetLabel("%d x %d = " % (self.num1, self.num2))


class MApp(wx.App):

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

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


It will ask you to multiply two completely random number and say whether you guessed it right or wrong.

Saturday, January 12, 2008

Learning wxPython

I've been looking around the web for some help on how to do programming using wxPython. I like the Python language and I am intrigued by wxPython because it seems like it can make program's that look native on whichever computer it runs on (I only run Linux and the occasional Windows 9x/XP anyway). So I wanted to learn more...

Googling immediately came up with the wikipedia entry at http://en.wikipedia.org/wiki/WxPython. And from it I got the proverbial "Hello world" example running. The code is short and sweet:


import wx

class HelloFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, -1, title, pos=(0,0), size=(320,240))
panel = wx.Panel(self, -1)
text = wx.StaticText(panel, -1, "Hello world!", wx.Point(10,5), wx.Size(-1, -1))

class HelloApp(wx.App):
def OnInit(self):
frame = HelloFrame(None, -1, "Hello, world!")
self.SetTopWindow(frame)
frame.Show(True)
return True

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


And it can be understood almost immediately. But keeping on with my search, I couldn't find a very comprehensive and easy to follow reference for wxPython. At most I found the alphabetical listing of the classes available at http://wxwidgets.org/manuals/stable/wx_classref.html#classref. So that is it for now in a sense. If I can get my head wrapped around it I might post some more of my findings. Oh well...

Friday, January 4, 2008

First post

It is customary....

Hello, world!