Friday, March 5, 2010

create a empty window with wx.python

this was the basic exercise in GUI programming, in here i make a empty window using wxpython





























import wx



class aplikasi(wx.App):

def OnInit(self):

self.frame=wx.Frame(None)

self.frame.Show()

self.SetTopWindow(self.frame)

return True



AplikasiKu=aplikasi()

AplikasiKu.MainLoop




nanti hasilnya seperti gambar di bawah ini




windowpy.GIF

simple GUI calculator with wxpython

here was an wxpython calculator taht i've copied from http://pythonwise.blogspot.com/2006/05/wxpython-calculator-in-50-lines-of.html , and written by Miki Tebeka































from __future__ import division



__author__ = "Miki Tebeka

"

__version__ = "0.0.2"



# Calculator GUI:



# ___________v

# [7][8][9][/]

# [4][5][6][*]

# [1][2][3][-]

# [0][.][C][+]

# [ = ]



import wx

# So we can evaluate "sqrt(8)"

from math import *



class Calculator(wx.Dialog):

'''Main calculator dialog'''

def __init__(self):

title = "Calculator version %s" % __version__

wx.Dialog.__init__(self, None, -1, title)

sizer = wx.BoxSizer(wx.VERTICAL) # Main vertical sizer



# ____________v

self.display = wx.ComboBox(self, -1) # Current calculation

sizer.Add(self.display, 0, wx.EXPAND) # Add to main sizer



# [7][8][9][/]

# [4][5][6][*]

# [1][2][3][-]

# [0][.][C][+]

gsizer = wx.GridSizer(4, 4)

for row in (("7", "8", "9", "/"),

("4", "5", "6", "*"),

("1", "2", "3", "-"),

("0", ".", "C", "+")):

for label in row:

b = wx.Button(self, -1, label)

gsizer.Add(b)

self.Bind(wx.EVT_BUTTON, self.OnButton, b)

sizer.Add(gsizer, 1, wx.EXPAND)



# [ = ]

b = wx.Button(self, -1, "=")

self.Bind(wx.EVT_BUTTON, self.OnButton, b)

sizer.Add(b, 0, wx.EXPAND)

self.equal = b



# Set sizer and center

self.SetSizer(sizer)

sizer.Fit(self)

self.CenterOnScreen()



def OnButton(self, evt):

'''Handle button click event'''

# Get title of clicked button

label = evt.GetEventObject().GetLabel()



if label == "=": # Calculate

try:

compute = self.display.GetValue()

# Ignore empty calculation

if not compute.strip():

return



# Calculate result

result = eval(compute)



# Add to history

self.display.Insert(compute, 0)



# Show result

self.display.SetValue(str(result))

except Exception, e:

wx.LogError(str(e))

return



elif label == "C": # Clear

self.display.SetValue("")



else: # Just add button text to current calculation

self.display.SetValue(self.display.GetValue() + label)

self.equal.SetFocus() # Set the [=] button in focus



if __name__ == "__main__":

# Run the application

app = wx.PySimpleApp()

dlg = Calculator()

dlg.ShowModal()

dlg.Destroy()






calc.jpg

Saturday, February 27, 2010

simple python age counter

here is a super simple (easy to understand) age counter, which ask for username input, birthday, and recent date. i'm adding some commentation function in the end of the code











'''program penghitung umur'''

#username input name = raw_input ("who are you?")
#birth date from user input in number
tbirth = input ("birthday date")
bbirth = input ("birth month")
dbirth = input ("birth year")
birth = tbirth + (bbirth * 30) + (dbirth * 365)

#recent date
trecent = input ("recent day")
brecent = input ("recent month")
drecent = input ("recent year")
recent = trecent + (brecent * 30) + (drecent * 365)

#begin calculation process
year = (recent - birth) / 365
month = ((recent - birth) % 365) / 30
day = ((recent - birth) % 365) % 30

#tampilkan hasil
print "hallo", name, "your age is :",
print day, "days", months, "months", year, "years"

'''commentation'''
if year <= 5:
print "you're still a baby"
elif year <= 17:
print "you're still a teenager"
elif year <= 45:
print "you're an adult"
elif year <= 99:
print "you're old enough"
else :
print "are you serious?"