|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from tkinter import Tk, END, Entry, N, E, S, W, Button |
| 3 | +from tkinter import font |
| 4 | +from tkinter import Label |
| 5 | +from functools import partial |
| 6 | + |
| 7 | + |
| 8 | +def get_input(entry, argu): |
| 9 | + entry.insert(END, argu) |
| 10 | + |
| 11 | + |
| 12 | +def backspace(entry): |
| 13 | + input_len = len(entry.get()) |
| 14 | + entry.delete(input_len - 1) |
| 15 | + |
| 16 | + |
| 17 | +def clear(entry): |
| 18 | + entry.delete(0, END) |
| 19 | + |
| 20 | + |
| 21 | +def calc(entry): |
| 22 | + input_info = entry.get() |
| 23 | + try: |
| 24 | + output = str(eval(input_info.strip())) |
| 25 | + except ZeroDivisionError: |
| 26 | + popupmsg() |
| 27 | + output = "" |
| 28 | + clear(entry) |
| 29 | + entry.insert(END, output) |
| 30 | + |
| 31 | + |
| 32 | +def popupmsg(): |
| 33 | + popup = Tk() |
| 34 | + popup.resizable(0, 0) |
| 35 | + popup.geometry("120x100") |
| 36 | + popup.title("Alert") |
| 37 | + label = Label(popup, text="Cannot divide by 0 ! \n Enter valid values") |
| 38 | + label.pack(side="top", fill="x", pady=10) |
| 39 | + B1 = Button(popup, text="Okay", bg="#DDDDDD", command=popup.destroy) |
| 40 | + B1.pack() |
| 41 | + |
| 42 | + |
| 43 | +def cal(): |
| 44 | + root = Tk() |
| 45 | + root.title("Calc") |
| 46 | + root.resizable(0, 0) |
| 47 | + |
| 48 | + entry_font = font.Font(size=15) |
| 49 | + entry = Entry(root, justify="right", font=entry_font) |
| 50 | + entry.grid(row=0, column=0, columnspan=4, |
| 51 | + sticky=N + W + S + E, padx=5, pady=5) |
| 52 | + |
| 53 | + cal_button_bg = '#FF6600' |
| 54 | + num_button_bg = '#4B4B4B' |
| 55 | + other_button_bg = '#DDDDDD' |
| 56 | + text_fg = '#FFFFFF' |
| 57 | + button_active_bg = '#C0C0C0' |
| 58 | + |
| 59 | + num_button = partial(Button, root, fg=text_fg, bg=num_button_bg, |
| 60 | + padx=10, pady=3, activebackground=button_active_bg) |
| 61 | + cal_button = partial(Button, root, fg=text_fg, bg=cal_button_bg, |
| 62 | + padx=10, pady=3, activebackground=button_active_bg) |
| 63 | + |
| 64 | + button7 = num_button(text='7', bg=num_button_bg, |
| 65 | + command=lambda: get_input(entry, '7')) |
| 66 | + button7.grid(row=2, column=0, pady=5) |
| 67 | + |
| 68 | + button8 = num_button(text='8', command=lambda: get_input(entry, '8')) |
| 69 | + button8.grid(row=2, column=1, pady=5) |
| 70 | + |
| 71 | + button9 = num_button(text='9', command=lambda: get_input(entry, '9')) |
| 72 | + button9.grid(row=2, column=2, pady=5) |
| 73 | + |
| 74 | + button10 = cal_button(text='+', command=lambda: get_input(entry, '+')) |
| 75 | + button10.grid(row=4, column=3, pady=5) |
| 76 | + |
| 77 | + button4 = num_button(text='4', command=lambda: get_input(entry, '4')) |
| 78 | + button4.grid(row=3, column=0, pady=5) |
| 79 | + |
| 80 | + button5 = num_button(text='5', command=lambda: get_input(entry, '5')) |
| 81 | + button5.grid(row=3, column=1, pady=5) |
| 82 | + |
| 83 | + button6 = num_button(text='6', command=lambda: get_input(entry, '6')) |
| 84 | + button6.grid(row=3, column=2, pady=5) |
| 85 | + |
| 86 | + button11 = cal_button(text='-', command=lambda: get_input(entry, '-')) |
| 87 | + button11.grid(row=3, column=3, pady=5) |
| 88 | + |
| 89 | + button1 = num_button(text='1', command=lambda: get_input(entry, '1')) |
| 90 | + button1.grid(row=4, column=0, pady=5) |
| 91 | + |
| 92 | + button2 = num_button(text='2', command=lambda: get_input(entry, '2')) |
| 93 | + button2.grid(row=4, column=1, pady=5) |
| 94 | + |
| 95 | + button3 = num_button(text='3', command=lambda: get_input(entry, '3')) |
| 96 | + button3.grid(row=4, column=2, pady=5) |
| 97 | + |
| 98 | + button12 = cal_button(text='*', command=lambda: get_input(entry, '*')) |
| 99 | + button12.grid(row=2, column=3, pady=5) |
| 100 | + |
| 101 | + button0 = num_button(text='0', command=lambda: get_input(entry, '0')) |
| 102 | + #button0.grid(row=5, column=0, columnspan=2, padx=3, pady=5, sticky=N + S + E + W) |
| 103 | + button0.grid(row=5, column=0, pady=5) |
| 104 | + |
| 105 | + button13 = num_button(text='.', command=lambda: get_input(entry, '.')) |
| 106 | + button13.grid(row=5, column=1, pady=5) |
| 107 | + |
| 108 | + button14 = Button(root, text='/', fg=text_fg, bg=cal_button_bg, padx=10, pady=3, |
| 109 | + command=lambda: get_input(entry, '/')) |
| 110 | + button14.grid(row=1, column=3, pady=5) |
| 111 | + |
| 112 | + button15 = Button(root, text='<-', bg=other_button_bg, padx=10, pady=3, |
| 113 | + command=lambda: backspace(entry), activebackground=button_active_bg) |
| 114 | + button15.grid(row=1, column=0, columnspan=2, |
| 115 | + padx=3, pady=5, sticky=N + S + E + W) |
| 116 | + |
| 117 | + button16 = Button(root, text='C', bg=other_button_bg, padx=10, pady=3, |
| 118 | + command=lambda: clear(entry), activebackground=button_active_bg) |
| 119 | + button16.grid(row=1, column=2, pady=5) |
| 120 | + |
| 121 | + button17 = Button(root, text='=', fg=text_fg, bg=cal_button_bg, padx=10, pady=3, |
| 122 | + command=lambda: calc(entry), activebackground=button_active_bg) |
| 123 | + button17.grid(row=5, column=3, pady=5) |
| 124 | + |
| 125 | + button18 = Button(root, text='^', fg=text_fg, bg=cal_button_bg, padx=10, pady=3, |
| 126 | + command=lambda: get_input(entry, '**')) |
| 127 | + button18.grid(row=5, column=2, pady=5) |
| 128 | + |
| 129 | + root.mainloop() |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + cal() |
0 commit comments