|
| 1 | +# Inspirado no programa 'clac1.py' do livro "Python and Tkinter Programming" de John E.Grayson |
| 2 | +from tkinter import * |
| 3 | + |
| 4 | +def frame(root, side): |
| 5 | + w = Frame(master=root) |
| 6 | + w.pack(side=side, expand=True, fill=BOTH, padx=(5, 0)) |
| 7 | + return w |
| 8 | + |
| 9 | +def button(root, text, side, command=None, **kw): |
| 10 | + w = Button(master=root, text=text, |
| 11 | + command=command, relief=RIDGE, **kw) |
| 12 | + w.pack(side=side, expand=True, fill=BOTH) |
| 13 | + return w |
| 14 | + |
| 15 | +root = Frame() |
| 16 | +display = StringVar() |
| 17 | +frame_top = Frame(root,) |
| 18 | +frame_top.pack(side=TOP, fill=BOTH, expand=YES) |
| 19 | +Entry(master=frame_top, relief=SUNKEN, |
| 20 | + textvariable=display).pack(side=LEFT, expand=YES, fill=BOTH, padx=5, pady=2) |
| 21 | +Button(frame_top, text="C", |
| 22 | + command=lambda w=display: w.set(''), |
| 23 | + bg="red", fg="white", relief=RIDGE).pack(side=RIGHT, expand=False, padx=2) |
| 24 | + |
| 25 | + |
| 26 | +for i in "987+ 654- 321* 0d./".split(): |
| 27 | + frame2buttons = frame(root, TOP) |
| 28 | + for char in i: |
| 29 | + if char == "d": |
| 30 | + button(frame2buttons, "00", LEFT, |
| 31 | + lambda w=display: w.set(w.get()+"00")) |
| 32 | + else: |
| 33 | + |
| 34 | + button(frame2buttons,char,LEFT, |
| 35 | + lambda w=display, c='%s' % char: w.set(w.get()+c)) |
| 36 | + |
| 37 | + |
| 38 | +def result(w, e=None): |
| 39 | + s = w.get() |
| 40 | + if '%' in s: |
| 41 | + index = list(s).index('%') |
| 42 | + list_s = list(s) |
| 43 | + list_s.insert(0, '(') |
| 44 | + list_s.insert(index+1, ')/100') |
| 45 | + list_s[index+2] = '*' |
| 46 | + s = ''.join(list_s) |
| 47 | + |
| 48 | + w.set(eval(s)) |
| 49 | + |
| 50 | +frame_bottom = frame(root, BOTTOM) |
| 51 | +button(frame_bottom, "=", LEFT, command=lambda w=display: result(w), bg="red", fg="white") |
| 52 | +Button(master=frame_bottom, text="%", relief=RIDGE, command=lambda w=display: w.set(w.get()+"%")).pack(side=LEFT, expand=False) |
| 53 | + |
| 54 | +root.pack(side=TOP, fill=BOTH, expand=True) |
| 55 | +root.mainloop() |
0 commit comments