associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

Revisão 8e 2006-04-25 17:39:03

Excluir mensagem

CalculadoraComercial

   1 #!/usr/bin/python
   2 # -*- coding: cp1252 -*-
   3 
   4 """
   5 Autor:               Vinicius Rodrigues da Cunha Perallis
   6 Data:                19/04/2006
   7                      20/04/2006
   8 Projeto:             Calculadora Comercial 
   9 
  10 Finalidade:          Calculadora que opera semelhante a calculadora
  11                      do windows, alem de conter uma caixa de 
  12                      texto que imita uma fita de impressão
  13 
  14 Interpretador:       Python 2.4.1
  15 Sistema Operacional: Linux, Windows
  16 
  17 Email:perallis@rc.unesp.br
  18 
  19 
  20 """
  21 
  22 
  23 #Importa todas funcoes do modulo Tkinter
  24 from Tkinter import *
  25 
  26 
  27 #Classe Principal que executa o aplicativo Calculadora
  28 class Calculadora:
  29     
  30     def __init__(self,parent):
  31         self.myapp=parent
  32         self.myapp.title("Calculadora - Phy")
  33         #Cria o frame principa
  34         self.geral=Frame(self.myapp,bg="gray")
  35         self.geral.pack()
  36         
  37 
  38         #Cria os frames nescessarios para a disposição dos \ 
  39         widgets no programa
  40 
  41         #Cria os frames nescessarios para a disposição dos \ 
  42         widgets no programa
  43 
  44         self.fexibir=Frame(self.geral,bg="gray")
  45         self.fzero=Frame(self.geral,bg="gray")
  46         self.fum=Frame(self.geral,bg="gray")
  47         self.fdois=Frame(self.geral,bg="gray")
  48         self.ftreis=Frame(self.geral,bg="gray")
  49         self.fquatro=Frame(self.geral,bg="gray")
  50         
  51 
  52         #Caixa de texto, que servirá para mostrar os numeros
  53         
  54         self.visor=Entry(self.fexibir,width=23,bg="white",fg="blue",justify="right")
  55         self.visor.insert(INSERT,0) 
  56         # Frame geral: Escreve um titulo na primeira linha do programa 
  57         self.titulo=Label(self.geral,text="  CALCULADORA PYTHON  
  58 ",bg="gray",fg="black",font=('Helvetica','10','bold'))
  59         # Insere a cixa de Texto que seá utilizada para fazer a fita de 
  60 impressão
  61         self.ffita=Frame(self.geral)
  62         self.ffita.pack(side="right",padx=15)
  63         self.sb=Scrollbar(self.ffita)
  64         
  65 self.fita=Text(self.ffita,width=23,height=13,bg="white",yscrollcommand=self.sb.set)
  66         self.sb["command"] = self.fita.yview
  67         self.fita.pack(side="left",padx=0)
  68         self.sb.pack(side="right",fill=Y)
  69         
  70         
  71         ## WIDGETS NO FRAME ZERO 
  72 #########################################################
  73         #Botao para reiniciar a calculadora
  74         
  75 self.c=Button(self.fzero,text="C",bg="white",width=4,command=self.zera)
  76         #Label para organizar o programa
  77         self.space1=Label(self.fzero,bg="gray",text="                       
  78 ")
  79         #Botao igual 
  80         
  81 self.igual=Button(self.fzero,text="=",bg="white",width=4,command=lambda sin="": self.op(sin))
  82         
  83 ###################################################################################
  84 
  85        
  86         ## WIDGETS NO FRAME UM 
  87 ############################################################
  88         self.sete=Button(self.fum,text="7",width=4,command=lambda n=7: 
  89 self.write_visor(n))
  90         self.oito=Button(self.fum,text="8",width=4,command=lambda n=8: 
  91 self.write_visor(n))
  92         self.nove=Button(self.fum,text="9",width=4,command=lambda n=9: 
  93 self.write_visor(n))
  94         self.div=Button(self.fum,text="/",fg="red",width=4, 
  95 command=lambda sin='/': self.op(sin))
  96         
  97 ###################################################################################
  98 
  99         ## WIDGETS NO FRAME DOIS 
 100 ############################################################
 101         self.quatro=Button(self.fdois,text="4",width=4,command=lambda 
 102 n=4: self.write_visor(n))
 103         self.cinco=Button(self.fdois,text="5",width=4,command=lambda 
 104 n=5: self.write_visor(n))
 105         self.seis=Button(self.fdois,text="6",width=4,command=lambda 
 106 n=6: self.write_visor(n))
 107         self.mult=Button(self.fdois,text="*",fg="red",width=4 
 108 ,command=lambda sin='*': self.op(sin))
 109         
 110 ###################################################################################
 111 
 112         ## WIDGETS NO FRAME TREIS 
 113 ##########################################################
 114         self.um=Button(self.ftreis,text="1",width=4,command=lambda n=1: 
 115 self.write_visor(n) )
 116         self.dois=Button(self.ftreis,text="2",width=4,command=lambda 
 117 n=2: self.write_visor(n))
 118         self.tres=Button(self.ftreis,text="3",width=4,command=lambda 
 119 n=3: self.write_visor(n))
 120         self.menos=Button(self.ftreis,text="-",fg="red",width=4, 
 121 command=lambda sin='-': self.op(sin) )
 122         
 123 ###################################################################################
 124 
 125         ## WIDGETS NO FRAME 
 126 QUATRO#########################################################
 127         self.zero=Button(self.fquatro,text="0",width=4,command=lambda 
 128 n=0: self.write_visor(n))
 129         self.virg=Button(self.fquatro,text=",",width=4,command=lambda 
 130 n='.': self.write_visor(n))
 131         self.maismenos=Button(self.fquatro,fg="red",text="+/-",width=4, 
 132 command=lambda n=-1: self.write_visor(n))
 133         self.mais=Button(self.fquatro,fg="red",text="+",width=4, 
 134 command=lambda sin='+': self.op(sin))
 135         
 136 ###################################################################################
 137 
 138         #Frame fgeral   
 139         self.titulo.pack()
 140          
 141         #Frame fexibir   
 142         self.fexibir.pack()
 143        
 144         self.visor.pack(ipadx="3")
 145    
 146         #Frame fzero  
 147         self.fzero.pack()       
 148        
 149         self.c.pack(side=LEFT,fill=X,pady="5",padx="2")
 150         self.igual.pack(side=LEFT,fill=X,pady="5",padx="2")
 151         self.space1.pack()
 152 
 153         #Frame fum   
 154         self.fum.pack()
 155    
 156         self.sete.pack(side="left",padx=2,pady=2)
 157         self.oito.pack(side="left",padx=2,pady=2)
 158         self.nove.pack(side="left",padx=2,pady=2)
 159         self.div.pack(side="left",padx=2,pady=2)
 160         
 161         #Frame fdois   
 162         self.fdois.pack()
 163         self.seis.pack(side="left",padx=2,pady=2)
 164         self.cinco.pack(side="left",padx=2,pady=2)
 165         self.quatro.pack(side="left",padx=2,pady=2)
 166         self.mult.pack(side="left",padx=2,pady=2)
 167 
 168         #Frame ftreis 
 169         self.ftreis.pack()
 170         self.tres.pack(side="left",padx=2,pady=2)
 171         self.dois.pack(side="left",padx=2,pady=2)
 172         self.um.pack(side="left",padx=2,pady=2)
 173         self.menos.pack(side="left",padx=2,pady=2)
 174 
 175         #Frame fquatro   
 176         self.fquatro.pack()
 177         self.maismenos.pack(side="left",padx=2,pady=2)
 178         self.zero.pack(side="left",padx=2,pady=2)
 179         self.virg.pack(side="left",padx=2,pady=2)
 180         self.mais.pack(side="left",padx=2,pady=2)
 181         
 182         #  Variais da classe 
 183         self.z=0
 184         self.w=1
 185         self.atual=0
 186         self.i=0
 187         self.ty=0
 188         self.num=0
 189         self.prim=0
 190         self.h=0
 191         self.sa=0
 192        
 193         
 194 
 195 
 196     ###------------------------------Função para realizar as operações 
 197 aritiméticas --------------------------###
 198         
 199     def op(self,sin):
 200         if (self.w==0):
 201             self.num=float(self.visor.get())
 202             print self.num
 203             self.sinal=sin
 204             self.w=1
 205             self.z=1
 206             self.i=0 
 207             self.ty=0
 208             self.divi=0
 209             if self.prim==0:
 210                 self.ty=1
 211                 
 212         else :   
 213             if self.ty==0:
 214                 self.atual=float(self.visor.get())
 215                 
 216             if self.sa==1 and self.ty==1:
 217                 self.num=float(self.visor.get())
 218                 self.sa=0
 219                 
 220             if self.sinal=='+':
 221                 self.num=self.num + self.atual
 222             elif  self.sinal=='-':
 223                 self.num=self.num - self.atual
 224             elif  self.sinal=='/' and self.ty!=1 and self.atual!=0:
 225                 self.num=self.num / self.atual
 226             elif  self.sinal=='/' and self.atual==0 and self.ty!=1:
 227                 self.visor.delete(0,END)
 228                 self.visor.insert(INSERT,"Imposivel dividir")
 229                 self.divi=1
 230                 self.prim=1
 231             elif  self.sinal=='*' and self.ty!=1 and self.prim==1:
 232                 self.num=self.num * self.atual
 233             
 234             if self.divi==0:
 235                 self.visor.delete(0,END)
 236                 self.visor.insert(END,self.num)
 237                 self.prim=1
 238             if self.divi==0 and self.ty==0:
 239                 self.fita.insert(INSERT,"\n"+self.sinal)
 240                 self.fita.insert(INSERT,"\n"+str(self.atual))
 241                 self.fita.insert(INSERT,"\n"+"=")
 242                 self.fita.insert(INSERT,"\n"+str(self.num))
 243             
 244                                          
 245             self.atual=0
 246             self.ty=1
 247             self.i=0
 248             self.w=1
 249             self.z=1
 250             self.div=0 
 251             self.sinal=sin
 252     ###---------------------------------------- Fim da funçõa 
 253 aritmética op --------------------------------------###
 254 
 255 
 256 
 257 
 258     ###-------------------------------- Função para escrever os numeros 
 259 no visor ---------------------------------###       
 260     def write_visor(self,n):
 261         if (self.z==1) and n!=-1:
 262             if self.i==0: 
 263                  self.visor.delete(0,END)         
 264           
 265             self.visor.insert(INSERT,n)
 266             self.w=1
 267             self.z=1
 268             self.i=1
 269             self.ty=0
 270             self.prim=1
 271        
 272         
 273             
 274         elif self.z==0 and n!=-1:
 275             if self.i==0:
 276                 self.visor.delete(0,END)         
 277             self.visor.insert(INSERT,n)
 278             self.fita.insert(INSERT,n)
 279             self.w=0
 280             self.i=1
 281             self.ty=0
 282         if  n==-1:
 283             self.la=float(self.visor.get()) * (-1)
 284             self.visor.delete(0,END)
 285             self.visor.insert(END,self.la)
 286             self.fita.insert(INSERT,"\n"+"*(-1)")
 287             self.fita.insert(INSERT,"\n"+"=")
 288             self.fita.insert(INSERT,"\n"+str(self.la))
 289             self.sa=1
 290            
 291     ###---------------------------------------- Fim da funçõa 
 292 write_visor --------------------------------------###
 293        
 294 
 295 
 296     ###---------------------------------- Função para reiniciar a calculadora ----------------------------------###        
 297     def zera(self):
 298         self.num=0
 299         self.z=0
 300         self.w=1
 301         self.atual=0
 302         self.i=0
 303         self.ty=0
 304         self.prim=0
 305         self.visor.delete(0,END)
 306         self.visor.insert(INSERT,0)
 307         self.fita.delete('1.0','90.0')
 308     ###------------------------------------------- Fim da função 
 309 zera-------------------------------------------### 
 310        
 311         
 312 
 313             
 314 # root Intacia-se pela classe Tk() e inicia a aplicação       
 315 root=Tk()
 316 aplicativo=Calculadora(root)
 317 root.mainloop()