#!/usr/bin/python
#
# main.py
#
# calculadora is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# calculadora is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
try:
import gtk
except ImportError:
sys.exit("pygtk not found.")
#Comment the first line and uncomment the second before installing
#or making the tarball (alternatively, use project variables)
UI_FILE = "data/calculadora.ui"
#UI_FILE = "/usr/local/share/calculadora/ui/calculadora.ui"
class GUI:
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file(UI_FILE)
self.window = self.builder.get_object("window1")
self.label1 = self.builder.get_object("label1")
self.entry1 = self.builder.get_object("entry1")
self.label2 = self.builder.get_object("label2")
self.entry2 = self.builder.get_object("entry2")
self.combobox = self.builder.get_object("combobox1")
self.button1 = self.builder.get_object ("button1")
self.label3 = self.builder.get_object("label3")
self.builder.connect_signals(self)
def cargarCombo(self):
#instancia creada con el listStore y se pude colocar numero por cada columna
#eso se haria asi gtk.ListStore (int,str) para poner un numero por cada opcion
listaElementos=gtk.ListStore(str)
#se pone en la lista de seleccion los datos si fuera con el numero quedaria
#de la siguiente forma listaElementos.append([0,"sumar"])
listaElementos.append(["Sumar"])
listaElementos.append(["Restar"])
listaElementos.append(["Multiplicar"])
listaElementos.append(["Dividir"])
#ponemos la lista de elementos al combobox que ya ha sido creado anteriormente
self.combobox.set_model(listaElementos)
render=gtk.CellRendererText()
self.combobox.pack_start(render, True)
self.combobox.add_attribute(render, 'text', 0)
#supuestamente con esta linea se activa el evento change
self.combobox.set_active(0)
def calcular (self, *args):
#obtener los valores de las cajas de texto
#numero1 = self.entry1.set_text(str(eval(self.entry1.get_text())))
#numero1= self.entry1.set_text(str(self.entry1.get_text))
numero1 = eval(self.entry1.get_text())
self.entry1.set_text(int(numero1))
numero2 = self.entry2.set_text(str(eval(self.entry2.get_text())))
#obtener el valor del checkbox
#self.combobox1.set_text(str(eval(self.combobox1.get_text())))
#pitonpy.blogpost.com/2010/04/como-usar-combobox.html
model = self.combobox.get_model()
activo = self.combobox.get_active()
estado = model[activo][0]
if estado == "Sumar":
resultado = numero1+numero2
if estado == "Restar":
resultado = numero1-numero2
if estado == "Multiplicar":
resultado = numero1*numero2
if estado == "Dividir":
resultado = numero1/numero2
print resultado
def on_boton_clicked(self, widget, data=None):
model = self.combobox.get_model()
activo = self.combobox.get_active()
if activo>=0:
self.button1.set_label(model[activo][0])
def change_text(self, widget, *event):
self.label.set_text("Hello, pygtk world!")
def quit(self, widget, *event):
gtk.main_quit()
def main():
app = GUI()
app.window.show()
app.cargarCombo()
gtk.main()
if __name__ == "__main__":
sys.exit(main()