Skip to content

Instantly share code, notes, and snippets.

@lkraider
Created June 26, 2016 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkraider/237e2d76a66b00334ad3f7fe2dd55b7a to your computer and use it in GitHub Desktop.
Save lkraider/237e2d76a66b00334ad3f7fe2dd55b7a to your computer and use it in GitHub Desktop.
PYculator GTK 2.0 (2007)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.3.4 on Mon Sep 10 16:47:51 2007 -->
<glade-interface>
<widget class="GtkWindow" id="calc">
<property name="width_request">275</property>
<property name="height_request">250</property>
<child>
<widget class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<child>
<widget class="GtkMenuBar" id="menubar1">
<property name="visible">True</property>
<child>
<widget class="GtkMenuItem" id="menuitem2">
<property name="visible">True</property>
<property name="label" translatable="yes">_Edit</property>
<property name="use_underline">True</property>
<child>
<widget class="GtkMenu" id="menu2">
<property name="visible">True</property>
<child>
<widget class="GtkImageMenuItem" id="imagemenuitem6">
<property name="visible">True</property>
<property name="label" translatable="yes">gtk-cut</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</widget>
</child>
<child>
<widget class="GtkImageMenuItem" id="imagemenuitem7">
<property name="visible">True</property>
<property name="label" translatable="yes">gtk-copy</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</widget>
</child>
<child>
<widget class="GtkImageMenuItem" id="imagemenuitem8">
<property name="visible">True</property>
<property name="label" translatable="yes">gtk-paste</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</widget>
</child>
<child>
<widget class="GtkImageMenuItem" id="imagemenuitem9">
<property name="visible">True</property>
<property name="label" translatable="yes">gtk-delete</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</widget>
</child>
</widget>
</child>
</widget>
</child>
<child>
<widget class="GtkMenuItem" id="menuitem3">
<property name="visible">True</property>
<property name="label" translatable="yes">_View</property>
<property name="use_underline">True</property>
</widget>
</child>
<child>
<widget class="GtkMenuItem" id="menuitem4">
<property name="visible">True</property>
<property name="label" translatable="yes">_Help</property>
<property name="use_underline">True</property>
<child>
<widget class="GtkMenu" id="menu3">
<property name="visible">True</property>
<child>
<widget class="GtkImageMenuItem" id="imagemenuitem10">
<property name="visible">True</property>
<property name="label" translatable="yes">gtk-about</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</widget>
</child>
</widget>
</child>
</widget>
</child>
</widget>
<packing>
<property name="expand">False</property>
</packing>
</child>
<child>
<widget class="GtkEntry" id="display">
<property name="visible">True</property>
<property name="editable">False</property>
<property name="xalign">1</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<widget class="GtkHBox" id="hbox1">
<property name="height_request">35</property>
<property name="visible">True</property>
<child>
<widget class="GtkToggleButton" id="memoryStatus">
<property name="width_request">4</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="focus_on_click">False</property>
<property name="response_id">0</property>
<property name="active">True</property>
</widget>
</child>
<child>
<widget class="GtkButton" id="buttonBackspace">
<property name="visible">True</property>
<property name="label" translatable="yes">Backspace</property>
<property name="response_id">0</property>
<accelerator key="BackSpace" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonCE">
<property name="visible">True</property>
<property name="label" translatable="yes">CE</property>
<property name="response_id">0</property>
<accelerator key="Delete" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="position">2</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonClear">
<property name="visible">True</property>
<property name="label" translatable="yes">C</property>
<property name="response_id">0</property>
<accelerator key="Escape" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="position">3</property>
</packing>
</child>
</widget>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="padding">2</property>
<property name="position">2</property>
</packing>
</child>
<child>
<widget class="GtkTable" id="table1">
<property name="visible">True</property>
<property name="n_rows">4</property>
<property name="n_columns">6</property>
<property name="homogeneous">True</property>
<child>
<widget class="GtkButton" id="buttonMinus">
<property name="visible">True</property>
<property name="label" translatable="yes">-</property>
<property name="response_id">0</property>
<signal name="clicked" handler="funcao"/>
<accelerator key="minus" modifiers="" signal="clicked"/>
<accelerator key="KP_Subtract" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">4</property>
<property name="right_attach">5</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonEquals">
<property name="visible">True</property>
<property name="label" translatable="yes">=</property>
<property name="response_id">0</property>
<accelerator key="KP_Enter" modifiers="" signal="clicked"/>
<accelerator key="equal" modifiers="" signal="clicked"/>
<accelerator key="Return" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">5</property>
<property name="right_attach">6</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonInverse">
<property name="visible">True</property>
<property name="label" translatable="yes">1/x</property>
<property name="response_id">0</property>
<accelerator key="R" modifiers="" signal="clicked"/>
<accelerator key="r" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">5</property>
<property name="right_attach">6</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonPercent">
<property name="visible">True</property>
<property name="label" translatable="yes">%</property>
<property name="response_id">0</property>
<accelerator key="percent" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">5</property>
<property name="right_attach">6</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonSquare">
<property name="visible">True</property>
<property name="label" translatable="yes">sqrt</property>
<property name="response_id">0</property>
<accelerator key="at" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">5</property>
<property name="right_attach">6</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonDiv">
<property name="visible">True</property>
<property name="label" translatable="yes">/</property>
<property name="response_id">0</property>
<signal name="clicked" handler="funcao"/>
<accelerator key="KP_Divide" modifiers="" signal="clicked"/>
<accelerator key="slash" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">4</property>
<property name="right_attach">5</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonMult">
<property name="visible">True</property>
<property name="label" translatable="yes">*</property>
<property name="response_id">0</property>
<signal name="clicked" handler="funcao"/>
<accelerator key="asterisk" modifiers="" signal="clicked"/>
<accelerator key="KP_Multiply" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">4</property>
<property name="right_attach">5</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonAdd">
<property name="visible">True</property>
<property name="label" translatable="yes">+</property>
<property name="response_id">0</property>
<signal name="clicked" handler="funcao"/>
<accelerator key="plus" modifiers="" signal="clicked"/>
<accelerator key="KP_Add" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">4</property>
<property name="right_attach">5</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonComma">
<property name="visible">True</property>
<property name="label" translatable="yes">,</property>
<property name="response_id">0</property>
<accelerator key="KP_Decimal" modifiers="" signal="clicked"/>
<accelerator key="comma" modifiers="" signal="clicked"/>
<accelerator key="period" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">3</property>
<property name="right_attach">4</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonSignal">
<property name="visible">True</property>
<property name="label" translatable="yes">+/-</property>
<property name="response_id">0</property>
</widget>
<packing>
<property name="left_attach">2</property>
<property name="right_attach">3</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button0">
<property name="visible">True</property>
<property name="label" translatable="yes">0</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_0" modifiers="" signal="clicked"/>
<accelerator key="0" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonMplus">
<property name="visible">True</property>
<property name="label" translatable="yes">M+</property>
<property name="response_id">0</property>
</widget>
<packing>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonMS">
<property name="visible">True</property>
<property name="label" translatable="yes">MS</property>
<property name="response_id">0</property>
</widget>
<packing>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonMR">
<property name="visible">True</property>
<property name="label" translatable="yes">MR</property>
<property name="response_id">0</property>
</widget>
<packing>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="buttonMC">
<property name="visible">True</property>
<property name="label" translatable="yes">MC</property>
<property name="response_id">0</property>
</widget>
</child>
<child>
<widget class="GtkButton" id="button9">
<property name="visible">True</property>
<property name="label" translatable="yes">9</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_9" modifiers="" signal="clicked"/>
<accelerator key="9" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">3</property>
<property name="right_attach">4</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button8">
<property name="visible">True</property>
<property name="label" translatable="yes">8</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_8" modifiers="" signal="clicked"/>
<accelerator key="8" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">2</property>
<property name="right_attach">3</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button7">
<property name="visible">True</property>
<property name="label" translatable="yes">7</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_7" modifiers="" signal="clicked"/>
<accelerator key="7" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button6">
<property name="visible">True</property>
<property name="label" translatable="yes">6</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_6" modifiers="" signal="clicked"/>
<accelerator key="6" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">3</property>
<property name="right_attach">4</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button5">
<property name="visible">True</property>
<property name="label" translatable="yes">5</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_5" modifiers="" signal="clicked"/>
<accelerator key="5" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">2</property>
<property name="right_attach">3</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button4">
<property name="visible">True</property>
<property name="label" translatable="yes">4</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_4" modifiers="" signal="clicked"/>
<accelerator key="4" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button3">
<property name="visible">True</property>
<property name="label" translatable="yes">3</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_3" modifiers="" signal="clicked"/>
<accelerator key="3" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">3</property>
<property name="right_attach">4</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button2">
<property name="visible">True</property>
<property name="label" translatable="yes">2</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_2" modifiers="" signal="clicked"/>
<accelerator key="2" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">2</property>
<property name="right_attach">3</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button1">
<property name="visible">True</property>
<property name="label" translatable="yes">1</property>
<property name="response_id">0</property>
<signal name="clicked" handler="numero"/>
<accelerator key="KP_1" modifiers="" signal="clicked"/>
<accelerator key="1" modifiers="" signal="clicked"/>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
</widget>
<packing>
<property name="padding">2</property>
<property name="position">3</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import math
class Calculadora(object):
'''Calculadora com operações básicas.'''
def __init__(self):
self.operando = 0.0
self.operador = None
self.memoria = 0.0
self.resultado = None
def limpa(self):
self.operando = 0.0
self.operador = None
self.resultado = None
def salva_memoria(self, valor):
self.memoria = valor
def soma_memoria(self, valor):
self.memoria += valor
def soma(self, valor):
return self.operando + valor
def subtrai(self, valor):
return self.operando - valor
def multiplica(self, valor):
return self.operando * valor
def divide(self, valor):
if valor == 0:
return 'Impossível dividir por zero.'
return self.operando / valor
def raiz(self, valor):
return math.sqrt(valor)
def porcentagem(self, valor):
return (self.operando * valor) / 100
def inverso(self, valor):
return 1 / valor
def inverte_sinal(self, valor):
return valor * -1
def igual(self, valor):
if self.operador:
self.resultado = self.operador(valor)
return self.resultado
return self.operando
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import gtk
from kiwi.ui.gadgets import quit_if_last
from kiwi.ui.delegates import Delegate
from calculadora import Calculadora
class Calc(Delegate):
'''Controle da interface para calculadora simples.'''
gladefile='calc'
def __init__(self):
Delegate.__init__(self)
self.toplevel.connect('delete-event', quit_if_last)
self.calc = Calculadora()
self.mostra_tela(0.0)
self.virgula = False
#Funções tela:
def pega_tela(self):
'''Retorna o valor mostrado na tela.'''
tela = self.display.get_text()
return float(tela)
def pega_tela_partes(self):
'''Retorna as duas partes do valor mostrado na tela.'''
tela = self.display.get_text()
return tela.split('.')
def limpa_tela(self):
'''Apaga o valor mostrado na tela.'''
self.mostra_tela(0.0)
self.virgula = False
def mostra_tela(self, valor):
'''Apresenta um valor na tela.'''
self.display.set_text('%s' % valor)
#Eventos compartilhados:
def numero(self, *args):
'''Quando um número é acionado.'''
valor = args[0].get_label()
tela = self.pega_tela_partes()
if tela[0] == '0':
tela[0] = ''
i = 0
if self.virgula:
i = 1
tela[i] += valor
self.mostra_tela('.'.join(tela))
def funcao(self, *args):
'''Quando uma função de dois operandos é acionada.'''
self.calc.operando = self.pega_tela()
self.limpa_tela()
#Funções com dois operandos:
def on_buttonAdd__clicked(self, *args):
self.calc.operador = self.calc.soma
def on_buttonMinus__clicked(self, *args):
self.calc.operador = self.calc.subtrai
def on_buttonMult__clicked(self, *args):
self.calc.operador = self.calc.multiplica
def on_buttonDiv__clicked(self, *args):
self.calc.operador = self.calc.divide
#Funções imediatas:
def imediata(self, funcao):
'''Uma função imediata simplesmente pega o
valor da tela e imprime o resultado retornado.'''
valor = self.pega_tela()
resultado = getattr(self.calc, funcao)(valor)
self.mostra_tela(resultado)
def on_buttonSquare__clicked(self, *args):
self.imediata('raiz')
def on_buttonPercent__clicked(self, *args):
self.imediata('porcentagem')
def on_buttonInverse__clicked(self, *args):
self.imediata('inverso')
def on_buttonSignal__clicked(self, *args):
self.imediata('inverte_sinal')
def on_buttonEquals__clicked(self, *args):
self.imediata('igual')
#Funções da memória:
def memoria(self, funcao):
valor = self.pega_tela()
if valor != 0:
self.memoryStatus.set_label('M')
getattr(self.calc, funcao)(valor)
def on_buttonMC__clicked(self, *args):
self.memoryStatus.set_label('')
self.calc.memoria = 0.0
def on_buttonMR__clicked(self, *args):
self.mostra_tela(self.calc.memoria)
def on_buttonMS__clicked(self, *args):
self.memoria('salva_memoria')
def on_buttonMplus__clicked(self, *args):
self.memoria('soma_memoria')
#Funções adicionais:
def on_buttonCE__clicked(self, *args):
self.limpa_tela()
def on_buttonClear__clicked(self, *args):
self.calc.limpa()
self.limpa_tela()
def on_buttonBackspace__clicked(self, *args):
tela = self.pega_tela_partes()
i = 0
if self.virgula:
i = 1
if tela[i] != '0':
tela[i] = tela[i][:-1]
self.mostra_tela('.'.join(tela))
def on_buttonComma__clicked(self, *args):
if not self.virgula:
self.virgula = True
tela = self.pega_tela_partes()
if tela[1] == '0':
tela[1] = ''
self.mostra_tela('.'.join(tela))
calc = Calc()
calc.show()
gtk.main()
#-*- coding: utf-8 -*-
import os
import sys
import glob
import shutil
import subprocess
from distutils.core import setup
import py2exe
'''Cria distribuição da calculadora para win32.'''
def py2exe():
#Criando executável:
sys.argv.append('py2exe')
setup(
name = 'calculadora',
description = 'Calculadora simples em GTK',
version = '1.0',
windows = [{'script': 'interface.py'}],
options = {'py2exe': {'packages': 'encodings',
'optimize': 2,
'includes': 'cairo, pango, pangocairo, atk, gobject, kiwi'}},
data_files=['calc.glade']
)
def copy_gtk():
#Variáveis:
gtk_path = 'C:/Arquivos de programas/Arquivos comuns/GTK/2.0/'
destination = './dist/'
#Cria pastas intermediárias:
folders = ['lib', 'glade', 'pixmaps']
for folder in folders:
folder = os.path.realpath(destination + folder)
if not os.path.isdir(folder):
os.makedirs(folder)
#Copia arquivos do GTK:
folders = ['etc', 'lib/gtk-2.0', 'lib/locale/pt_BR', 'share']
for folder in folders:
source = os.path.realpath(gtk_path + folder)
target = os.path.realpath(destination + folder)
if not os.path.isdir(target):
print 'Copiando pasta ', folder
shutil.copytree(source, target)
dest_dir = os.path.realpath(destination)
for dll_file in glob.glob(gtk_path + '/bin/*.dll'):
file_name = os.path.split(dll_file)[-1]
if not os.path.exists(dest_dir + '\\' + file_name):
print 'Copiando arquivo ', file_name
shutil.copy(dll_file, os.path.realpath(destination))
def upx_pack():
#Compactar arquivos com UPX:
command = 'upx'
args = '--best'
path = 'dist\\'
file_types = ['*.pyd', '*.exe', '*.dll']
for file_type in file_types:
subprocess.call([command, args, path + file_type])
if __name__ == '__main__':
py2exe()
copy_gtk()
upx_pack()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment