. Con la tecnología de Blogger.

viernes, 30 de enero de 2009

[Python] Calculadora gráfica básica

codigo:

GeSHi (python):
import wx

valor1=0
valor2=0
operacion="0"

class MyFrame(wx.Frame):
#Constructor de la clase, en el cramos los componentes
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.text_ctrl_1 = wx.TextCtrl(self, -1, "")
self.button_1 = wx.Button(self, -1, "1")
self.button_2 = wx.Button(self, -1, "2")
self.button_3 = wx.Button(self, -1, "3")
self.button_4 = wx.Button(self, -1, "+")
self.button_5 = wx.Button(self, -1, "4")
self.button_6 = wx.Button(self, -1, "5")
self.button_7 = wx.Button(self, -1, "6")
self.button_8 = wx.Button(self, -1, "-")
self.button_9 = wx.Button(self, -1, "7")
self.button_10 = wx.Button(self, -1, "8")
self.button_11 = wx.Button(self, -1, "9")
self.button_12 = wx.Button(self, -1, "*")
self.button_13 = wx.Button(self, -1, "CE")
self.button_14 = wx.Button(self, -1, "0")
self.button_15 = wx.Button(self, -1, "=")
self.button_16 = wx.Button(self, -1, "/")

self.__set_properties()
self.__do_layout()

#Cambiamos algunas propiedades del formulario
def __set_properties(self):
self.SetTitle("Calc")
self.SetSize((179, 232))

#Layout, define la posicion y aspecto de los componentes en el formulario
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
grid_sizer_1 = wx.GridSizer(4, 4, 0, 0)
sizer_1.Add(self.text_ctrl_1, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_1, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_2, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_3, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_4, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_5, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_6, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_7, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_8, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_9, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_10, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_11, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_12, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_13, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_14, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_15, 0, wx.ALL|wx.EXPAND, 5)
grid_sizer_1.Add(self.button_16, 0, wx.ALL|wx.EXPAND, 5)

#Seteamos los eventos de los botones
self.button_1.Bind (wx.EVT_BUTTON, self.ClickNumero1)
self.button_2.Bind (wx.EVT_BUTTON, self.ClickNumero2)
self.button_3.Bind (wx.EVT_BUTTON, self.ClickNumero3)
self.button_5.Bind (wx.EVT_BUTTON, self.ClickNumero4)
self.button_6.Bind (wx.EVT_BUTTON, self.ClickNumero5)
self.button_7.Bind (wx.EVT_BUTTON, self.ClickNumero6)
self.button_9.Bind (wx.EVT_BUTTON, self.ClickNumero7)
self.button_10.Bind (wx.EVT_BUTTON, self.ClickNumero8)
self.button_11.Bind (wx.EVT_BUTTON, self.ClickNumero9)
self.button_14.Bind (wx.EVT_BUTTON, self.ClickNumero0)
self.button_4.Bind (wx.EVT_BUTTON, self.ClickSumar)
self.button_8.Bind (wx.EVT_BUTTON, self.ClickRestar)
self.button_12.Bind (wx.EVT_BUTTON, self.ClickMultiplicar)
self.button_16.Bind (wx.EVT_BUTTON, self.ClickDividir)
self.button_15.Bind (wx.EVT_BUTTON, self.ClickIgual)
self.button_13.Bind (wx.EVT_BUTTON, self.ClickCE)

sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
self.Layout()

def ClickNumero1(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"1")
def ClickNumero2(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"2")
def ClickNumero3(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"3")
def ClickNumero4(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"4")
def ClickNumero5(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"5")
def ClickNumero6(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"6")
def ClickNumero7(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"7")
def ClickNumero8(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"8")
def ClickNumero9(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"9")
def ClickNumero0(self, event):
self.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue()+"0")
def ClickCE(self, event):
global valor1
global valor2
global operacion
self.text_ctrl_1.SetValue("")
valor1=0
valor2=0
operacion="0"
def ClickSumar(self, event):
global valor1
global operacion
valor1=self.text_ctrl_1.GetValue()
operacion="+"
self.text_ctrl_1.SetValue("")
def ClickRestar(self, event):
global valor1
global operacion
valor1=self.text_ctrl_1.GetValue()
operacion="-"
self.text_ctrl_1.SetValue("")
def ClickMultiplicar(self, event):
global valor1
global operacion
valor1=self.text_ctrl_1.GetValue()
operacion="*"
self.text_ctrl_1.SetValue("")
def ClickDividir(self, event):
global valor1
global operacion
valor1=self.text_ctrl_1.GetValue()
operacion="/"
self.text_ctrl_1.SetValue("")
def ClickIgual(self, event):
global valor2
global valor1
global operacion
valor2=self.text_ctrl_1.GetValue()
if operacion=="+":
self.text_ctrl_1.SetValue(str(float(valor1)+float(valor2)))
valor1=0
valor2=0
operacion="0"
if operacion=="-":
self.text_ctrl_1.SetValue(str(float(valor1)-float(valor2)))
valor1=0
valor2=0
operacion="0"
if operacion=="*":
self.text_ctrl_1.SetValue(str(float(valor1)*float(valor2)))
valor1=0
valor2=0
operacion="0"
if operacion=="/":
self.text_ctrl_1.SetValue(str(float(float(valor1)/float(valor2))))
valor1=0
valor2=0
operacion="0"

class CalcClass(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
Calc = MyFrame(None, -1, "")
self.SetTopWindow(Calc)
Calc.Show()
return 1


if __name__ == "__main__":
Calc = CalcClass(0)
Calc.MainLoop()
Created by GeSHI 1.0.7.5


Una captura:


Adjunto el archivo fuente, para que no salga la ventana de consola durante la ejecución, cuando compileis el código cambiadle la extensión de .py a .pyw.

Para ejecutar la aplicación es necesario que tengáis instalado Python y wxPython en el pc

Fuente
saludos

0 comentarios: