Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #-------------------------------Python----------------------calcola.py--#
- # #
- # Calcolatrice #
- # #
- #---Daniele Zambelli---------------------------------------------2003---#
- from Tkinter import *
- from math import *
- class Calcolatrice:
- def __init__(self, master):
- self.max=14
- self.dec=0
- self.v=StringVar(); self.v.set('0')
- self.x=DoubleVar(); self.x.set(0)
- self.y=DoubleVar(); self.y.set(0)
- self.m=DoubleVar(); self.m.set(0)
- self.stato=StringVar(); self.stato.set('=')
- self.op=StringVar(); self.op.set('')
- self.disegna(master)
- def disegna(self, master):
- visu=[[('M:', 'maroon', self.m, 19, 3)],
- [('Y:', 'orange', self.y, 19, 3)],
- [('X:', 'orange', self.x, 19, 3)],
- [('st:', 'blue', self.stato, 2, 1),
- ('op:', 'blue', self.op, 2, 1)],
- [('V:', '#5C4033', self.v, 19, 3)]]
- self.dvisu(master, visu, 0, 0)
- tasti=[[None, None,
- ('CE','red',self.canc),('CA','red',self.canc)],
- [('M+','green',self.mem),('M-','green',self.mem),
- ('RM','green',self.mem),('CM','green',self.canc)],
- [('x^2','blue',self.opun),('RQ','blue',self.opun),
- ('1/x','blue',self.opun),('/','blue',self.opbin)],
- [('7','red',self.cifra),('8','red',self.cifra),
- ('9','red',self.cifra),('*','blue',self.opbin)],
- [('4','red',self.cifra),('5','red',self.cifra),
- ('6','red',self.cifra),('-','blue',self.opbin)],
- [('1','red',self.cifra),('2','red',self.cifra),
- ('3','red',self.cifra),('+','blue',self.opbin)],
- [('0','red',self.cifra),('.','red',self.cifra),
- ('+/-','blue',self.opun),('=','green',self.uguale)]
- ]
- self.dtasti(master, tasti, 6, 0)
- def dtasti(self, master, tasti, r, c):
- c0=c
- for rt in tasti:
- for t in rt:
- if t:
- b = Button(master, text=t[0], fg=t[1])
- b.grid(row=r, column=c0)
- b.bind("<1>", t[2])
- c0+=1
- r+=1; c0=c
- def dvisu(self, master, visu, r, c):
- c0=c
- for rv in visu:
- for v in rv:
- if v:
- l=Label(master, text=v[0], justify=RIGHT)
- l.grid(row=r, column=c0)
- vis = Entry(master, fg=v[1], textvariable=v[2],
- state=DISABLED, justify=RIGHT, width=v[3])
- vis.grid(row=r, column=c0+1, columnspan=v[4])
- c0+=2
- r+=1; c0=c
- def cifra(self, e):
- stato=self.stato.get()
- if stato=='E': return
- c=e.widget['text']
- if stato=='=' or stato=='M':
- self.v.set(''); self.op.set(''); self.ins(c)
- elif stato=='N': self.ins(c)
- elif stato=='O': self.v.set(''); self.ins(c)
- self.stato.set('N')
- def opbin(self, e):
- stato=self.stato.get()
- self.stato.set('O')
- if stato=='E': return
- op=e.widget['text']
- if stato=='=':
- self.op.set(op); self.y.set(float(self.v.get()))
- elif stato=='N' or stato=='M':
- if self.op.get():
- self.x.set(float(self.v.get())); self.esebin(self.op.get())
- else:
- self.y.set(float(self.v.get()))
- self.op.set(op)
- elif stato=='O': self.op.set(op)
- def uguale(self, e):
- stato=self.stato.get()
- if stato=='E': return
- if not self.op.get(): return
- op=self.op.get()
- if stato=='=': self.esebin(op)
- elif stato=='N': self.x.set(float(self.v.get())); self.esebin(op)
- elif stato=='O': self.x.set(float(self.v.get())); self.esebin(op)
- self.stato.set('=')
- def canc(self,e):
- c=e.widget['text']
- if c=='CM': self.m.set(0)
- else:
- self.v.set('0')
- self.x.set(0)
- if c=='CA': self.y.set(0); self.op.set('')
- self.stato.set('=')
- def opun(self, e):
- stato=self.stato.get()
- self.stato.set('=')
- self.x.set(float(self.v.get()))
- if stato=='E': return
- self.eseun(op=e.widget['text'])
- def mem(self, e):
- stato=self.stato.get()
- if stato=='E': return
- m=e.widget['text']
- if stato=='=' or stato=='M':
- self.esemem(m)
- elif stato=='N':
- if self.op.get():
- op=self.op.get()
- self.x.set(float(self.v.get())); self.esebin(op)
- self.esemem(m)
- else:
- self.esemem(m)
- elif stato=='O': self.esemem(m)
- def ins(self, c):
- if len(self.v.get())<self.max+self.dec:
- if c==".":
- if self.dec: return
- else: self.dec=1
- self.v.set(self.v.get() + c)
- def esebin(self, op):
- y=self.y.get(); x=self.x.get()
- if op=='+': self.y.set(y+x)
- elif op=='-': self.y.set(y-x)
- elif op=='*': self.y.set(y*x)
- elif op=='/':
- if x: self.y.set(y/x)
- else:
- self.v.set('Err: div zero'); self.stato.set('E')
- return
- self.v.set(str(self.y.get()))
- def esemem(self, m):
- if m=='M+':
- self.m.set(self.m.get()+float(self.v.get()))
- self.stato.set('=')
- elif m=='M-':
- self.m.set(self.m.get()-float(self.v.get()))
- self.stato.set('=')
- elif m=='RM':
- self.v.set(str(self.m.get()))
- self.stato.set('M')
- def eseun(self, op):
- x=self.x.get()
- if op=='+/-': self.x.set(-x)
- elif op=='x^2': self.x.set(x*x)
- elif op=='1/x': self.x.set(1/x)
- elif op=='RQ':
- if x>=0: self.x.set(sqrt(x))
- else:
- self.v.set('non reale'); self.stato.set('E')
- return
- self.v.set(str(self.x.get()))
- root = Tk()
- calc = Calcolatrice(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement