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

Revisão 9e 2006-04-25 17:50:32

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", \
  55                          fg="blue",justify="right")
  56         self.visor.insert(INSERT,0) 
  57 
  58         """Frame geral: Escreve um titulo na primeira linha 
  59         do programa""" 
  60 
  61         self.titulo=Label(self.geral,text="  CALCULADORA \
  62                           PYTHON  ",bg="gray",fg="black",
  63                           font= ('Helvetica','10','bold')) 
  64 
  65         """ Insere a cixa de Texto que seá utilizada para 
  66 
  67         fazer a fita de impressão"""
  68         self.ffita=Frame(self.geral)
  69         self.ffita.pack(side="right",padx=15)
  70         self.sb=Scrollbar(self.ffita)
  71         
  72         self.fita=Text(self.ffita,width=23,height=13,\
  73                        bg="white",yscrollcommand=self.sb.set)
  74         self.sb["command"] = self.fita.yview
  75         self.fita.pack(side="left",padx=0)
  76         self.sb.pack(side="right",fill=Y)
  77         
  78         
  79         ## WIDGETS NO FRAME ZERO ########################### 
  80 
  81         #Botao para reiniciar a calculadora
  82         
  83         self.c=Button(self.fzero,text="C",bg="white",\
  84                       width=4,command=self.zera)
  85        
  86         #Label para organizar o programa
  87         self.space1=Label(self.fzero,bg="gray",\
  88                          text="                       ")
  89         #Botao igual 
  90         
  91         self.igual=Button(self.fzero,text="=",bg="white",\
  92                           width=4,command=lambda sin="": \
  93                           self.op(sin))
  94         
  95         #####################################################
  96 
  97        
  98         ## WIDGETS NO FRAME UM ##################
  99         self.sete=Button(self.fum,text="7",width=4,\
 100                          command=lambda n=7: \
 101                          self.write_visor(n))
 102         self.oito=Button(self.fum,text="8",width=4,\
 103                          command=lambda n=8: \
 104                          self.write_visor(n))
 105         self.nove=Button(self.fum,text="9",width=4,\
 106                          command=lambda n=9: \
 107                          self.write_visor(n))
 108         self.div=Button(self.fum,text="/",fg="red",\
 109                         width=4, command=lambda sin='/': \
 110                         self.op(sin))
 111         
 112         ######################################################
 113 
 114         ## WIDGETS NO FRAME DOIS #############################
 115         self.quatro=Button(self.fdois,text="4",width=4,\
 116                           command=lambda n=4: \
 117                            self.write_visor(n))
 118         self.cinco=Button(self.fdois,text="5",width=4,\
 119                          command=lambda n=5: \
 120                          self.write_visor(n))
 121         self.seis=Button(self.fdois,text="6",width=4,\
 122                         command=lambda n=6: \
 123                         self.write_visor(n))
 124         self.mult=Button(self.fdois,text="*",fg="red",\
 125                          width=4 ,command=lambda sin='*': \
 126                          self.op(sin))
 127         
 128 ###################################################################################
 129 
 130         ## WIDGETS NO FRAME TREIS 
 131 ##########################################################
 132         self.um=Button(self.ftreis,text="1",width=4,command=lambda n=1: 
 133 self.write_visor(n) )
 134         self.dois=Button(self.ftreis,text="2",width=4,command=lambda 
 135 n=2: self.write_visor(n))
 136         self.tres=Button(self.ftreis,text="3",width=4,command=lambda 
 137 n=3: self.write_visor(n))
 138         self.menos=Button(self.ftreis,text="-",fg="red",width=4, 
 139 command=lambda sin='-': self.op(sin) )
 140         
 141 ###################################################################################
 142 
 143         ## WIDGETS NO FRAME 
 144 QUATRO#########################################################
 145         self.zero=Button(self.fquatro,text="0",width=4,command=lambda 
 146 n=0: self.write_visor(n))
 147         self.virg=Button(self.fquatro,text=",",width=4,command=lambda 
 148 n='.': self.write_visor(n))
 149         self.maismenos=Button(self.fquatro,fg="red",text="+/-",width=4, 
 150 command=lambda n=-1: self.write_visor(n))
 151         self.mais=Button(self.fquatro,fg="red",text="+",width=4, 
 152 command=lambda sin='+': self.op(sin))
 153         
 154 ###################################################################################
 155 
 156         #Frame fgeral   
 157         self.titulo.pack()
 158          
 159         #Frame fexibir   
 160         self.fexibir.pack()
 161        
 162         self.visor.pack(ipadx="3")
 163    
 164         #Frame fzero  
 165         self.fzero.pack()       
 166        
 167         self.c.pack(side=LEFT,fill=X,pady="5",padx="2")
 168         self.igual.pack(side=LEFT,fill=X,pady="5",padx="2")
 169         self.space1.pack()
 170 
 171         #Frame fum   
 172         self.fum.pack()
 173    
 174         self.sete.pack(side="left",padx=2,pady=2)
 175         self.oito.pack(side="left",padx=2,pady=2)
 176         self.nove.pack(side="left",padx=2,pady=2)
 177         self.div.pack(side="left",padx=2,pady=2)
 178         
 179         #Frame fdois   
 180         self.fdois.pack()
 181         self.seis.pack(side="left",padx=2,pady=2)
 182         self.cinco.pack(side="left",padx=2,pady=2)
 183         self.quatro.pack(side="left",padx=2,pady=2)
 184         self.mult.pack(side="left",padx=2,pady=2)
 185 
 186         #Frame ftreis 
 187         self.ftreis.pack()
 188         self.tres.pack(side="left",padx=2,pady=2)
 189         self.dois.pack(side="left",padx=2,pady=2)
 190         self.um.pack(side="left",padx=2,pady=2)
 191         self.menos.pack(side="left",padx=2,pady=2)
 192 
 193         #Frame fquatro   
 194         self.fquatro.pack()
 195         self.maismenos.pack(side="left",padx=2,pady=2)
 196         self.zero.pack(side="left",padx=2,pady=2)
 197         self.virg.pack(side="left",padx=2,pady=2)
 198         self.mais.pack(side="left",padx=2,pady=2)
 199         
 200         #  Variais da classe 
 201         self.z=0
 202         self.w=1
 203         self.atual=0
 204         self.i=0
 205         self.ty=0
 206         self.num=0
 207         self.prim=0
 208         self.h=0
 209         self.sa=0
 210        
 211         
 212 
 213 
 214     ###------------------------------Função para realizar as operações 
 215 aritiméticas --------------------------###
 216         
 217     def op(self,sin):
 218         if (self.w==0):
 219             self.num=float(self.visor.get())
 220             print self.num
 221             self.sinal=sin
 222             self.w=1
 223             self.z=1
 224             self.i=0 
 225             self.ty=0
 226             self.divi=0
 227             if self.prim==0:
 228                 self.ty=1
 229                 
 230         else :   
 231             if self.ty==0:
 232                 self.atual=float(self.visor.get())
 233                 
 234             if self.sa==1 and self.ty==1:
 235                 self.num=float(self.visor.get())
 236                 self.sa=0
 237                 
 238             if self.sinal=='+':
 239                 self.num=self.num + self.atual
 240             elif  self.sinal=='-':
 241                 self.num=self.num - self.atual
 242             elif  self.sinal=='/' and self.ty!=1 and self.atual!=0:
 243                 self.num=self.num / self.atual
 244             elif  self.sinal=='/' and self.atual==0 and self.ty!=1:
 245                 self.visor.delete(0,END)
 246                 self.visor.insert(INSERT,"Imposivel dividir")
 247                 self.divi=1
 248                 self.prim=1
 249             elif  self.sinal=='*' and self.ty!=1 and self.prim==1:
 250                 self.num=self.num * self.atual
 251             
 252             if self.divi==0:
 253                 self.visor.delete(0,END)
 254                 self.visor.insert(END,self.num)
 255                 self.prim=1
 256             if self.divi==0 and self.ty==0:
 257                 self.fita.insert(INSERT,"\n"+self.sinal)
 258                 self.fita.insert(INSERT,"\n"+str(self.atual))
 259                 self.fita.insert(INSERT,"\n"+"=")
 260                 self.fita.insert(INSERT,"\n"+str(self.num))
 261             
 262                                          
 263             self.atual=0
 264             self.ty=1
 265             self.i=0
 266             self.w=1
 267             self.z=1
 268             self.div=0 
 269             self.sinal=sin
 270     ###---------------------------------------- Fim da funçõa 
 271 aritmética op --------------------------------------###
 272 
 273 
 274 
 275 
 276     ###-------------------------------- Função para escrever os numeros 
 277 no visor ---------------------------------###       
 278     def write_visor(self,n):
 279         if (self.z==1) and n!=-1:
 280             if self.i==0: 
 281                  self.visor.delete(0,END)         
 282           
 283             self.visor.insert(INSERT,n)
 284             self.w=1
 285             self.z=1
 286             self.i=1
 287             self.ty=0
 288             self.prim=1
 289        
 290         
 291             
 292         elif self.z==0 and n!=-1:
 293             if self.i==0:
 294                 self.visor.delete(0,END)         
 295             self.visor.insert(INSERT,n)
 296             self.fita.insert(INSERT,n)
 297             self.w=0
 298             self.i=1
 299             self.ty=0
 300         if  n==-1:
 301             self.la=float(self.visor.get()) * (-1)
 302             self.visor.delete(0,END)
 303             self.visor.insert(END,self.la)
 304             self.fita.insert(INSERT,"\n"+"*(-1)")
 305             self.fita.insert(INSERT,"\n"+"=")
 306             self.fita.insert(INSERT,"\n"+str(self.la))
 307             self.sa=1
 308            
 309     ###---------------------------------------- Fim da funçõa 
 310 write_visor --------------------------------------###
 311        
 312 
 313 
 314     ###---------------------------------- Função para reiniciar a calculadora ----------------------------------###        
 315     def zera(self):
 316         self.num=0
 317         self.z=0
 318         self.w=1
 319         self.atual=0
 320         self.i=0
 321         self.ty=0
 322         self.prim=0
 323         self.visor.delete(0,END)
 324         self.visor.insert(INSERT,0)
 325         self.fita.delete('1.0','90.0')
 326     ###------------------------------------------- Fim da função 
 327 zera-------------------------------------------### 
 328        
 329         
 330 
 331             
 332 # root Intacia-se pela classe Tk() e inicia a aplicação       
 333 root=Tk()
 334 aplicativo=Calculadora(root)
 335 root.mainloop()