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

Revisão 6e 2006-04-25 17:26:06

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