|
| 1 | +# importing |
| 2 | +from tkinter import * |
| 3 | +from tkinter import ttk |
| 4 | +# from ttkthemes import ThemedStyle |
| 5 | + |
| 6 | + |
| 7 | +a_root = Tk() |
| 8 | +a_root.geometry('320x315') |
| 9 | +a_root.title('Calculator') |
| 10 | +# style = ThemedStyle(a_root) |
| 11 | +# style.set_theme("plastik") |
| 12 | +a_root.resizable(False, False) |
| 13 | +a = StringVar() |
| 14 | +Label(text='').pack() |
| 15 | +user_input1 = Entry(a_root, textvariable=a, width=25, font='calibri 18') |
| 16 | +user_input1.pack() |
| 17 | +user_output = Entry(a_root, width=43) |
| 18 | +user_output.pack() |
| 19 | + |
| 20 | + |
| 21 | +# Define functions for commands |
| 22 | +def result(): |
| 23 | + try: |
| 24 | + com_result = eval(str(a.get())) |
| 25 | + user_output.delete(0, END) |
| 26 | + user_output.insert(0, com_result) |
| 27 | + print(com_result) |
| 28 | + except Exception as e: |
| 29 | + print(e) |
| 30 | + user_output.delete(0, END) |
| 31 | + user_output.insert(0, 'Error') |
| 32 | + |
| 33 | +def clean(): |
| 34 | + user_input1.delete(0, END) |
| 35 | + user_output.delete(0, END) |
| 36 | + |
| 37 | +def backspace(): |
| 38 | + user_input1.delete(0, 1) |
| 39 | + |
| 40 | +def click(event): |
| 41 | + text = event.widget.cget("text") |
| 42 | + user_input1.insert(10, text) |
| 43 | + |
| 44 | + |
| 45 | +# Packing button under different frame |
| 46 | +# Packing button under 'my_frametop' frame |
| 47 | +my_frametop = Frame(a_root) |
| 48 | +my_frametop.pack() |
| 49 | +Button(my_frametop, text='=', command=result, padx=100, font='clibri 11 bold', bg='grey').pack(side=LEFT) |
| 50 | +Label(my_frametop, text='').pack(side=LEFT) |
| 51 | + |
| 52 | +ttk.Button(my_frametop, text='Backspace', command=backspace).pack(side=LEFT) |
| 53 | + |
| 54 | + |
| 55 | +# Packing button under 'my_frame' frame |
| 56 | +my_frame = Frame(a_root) |
| 57 | +my_frame.pack() |
| 58 | +b = ttk.Button(my_frame, text='[') |
| 59 | +b.pack(side=LEFT) |
| 60 | +b.bind("<Button-1>", click) |
| 61 | +b = ttk.Button(my_frame, text=']') |
| 62 | +b.pack(side=LEFT) |
| 63 | +b.bind("<Button-1>", click) |
| 64 | +b = ttk.Button(my_frame, text='{') |
| 65 | +b.pack(side=LEFT) |
| 66 | +b.bind("<Button-1>", click) |
| 67 | +b = ttk.Button(my_frame, text='}') |
| 68 | +b.pack(side=LEFT) |
| 69 | +b.bind("<Button-1>", click) |
| 70 | + |
| 71 | + |
| 72 | +# Packing button under 'my_frame1' frame |
| 73 | +my_frame1 = Frame(a_root) |
| 74 | +my_frame1.pack() |
| 75 | +b = ttk.Button(my_frame1, text='(') |
| 76 | +b.pack(side=LEFT) |
| 77 | +b.bind("<Button-1>", click) |
| 78 | +b = ttk.Button(my_frame1, text=')') |
| 79 | +b.pack(side=LEFT) |
| 80 | +b.bind("<Button-1>", click) |
| 81 | +b = ttk.Button(my_frame1, text='%') |
| 82 | +b.pack(side=LEFT) |
| 83 | +b.bind("<Button-1>", click) |
| 84 | +b = ttk.Button(my_frame1, text='^') |
| 85 | +b.pack(side=LEFT) |
| 86 | +b.bind("<Button-1>", click) |
| 87 | + |
| 88 | + |
| 89 | +# Packing button under 'my_frame2' frame |
| 90 | +my_frame2 = Frame(a_root) |
| 91 | +my_frame2.pack() |
| 92 | +b = ttk.Button(my_frame2, text='7') |
| 93 | +b.pack(side=LEFT) |
| 94 | +b.bind("<Button-1>", click) |
| 95 | +b = ttk.Button(my_frame2, text='8') |
| 96 | +b.pack(side=LEFT) |
| 97 | +b.bind("<Button-1>", click) |
| 98 | +b = ttk.Button(my_frame2, text='9') |
| 99 | +b.pack(side=LEFT) |
| 100 | +b.bind("<Button-1>", click) |
| 101 | +b = ttk.Button(my_frame2, text='/') |
| 102 | +b.pack(side=LEFT) |
| 103 | +b.bind("<Button-1>", click) |
| 104 | + |
| 105 | + |
| 106 | +# Packing button under 'my_frame3' frame |
| 107 | +my_frame3 = Frame(a_root) |
| 108 | +my_frame3.pack() |
| 109 | +b = ttk.Button(my_frame3, text='4') |
| 110 | +b.pack(side=LEFT) |
| 111 | +b.bind("<Button-1>", click) |
| 112 | +b = ttk.Button(my_frame3, text='5') |
| 113 | +b.pack(side=LEFT) |
| 114 | +b.bind("<Button-1>", click) |
| 115 | +b = ttk.Button(my_frame3, text='6') |
| 116 | + |
| 117 | +b.pack(side=LEFT) |
| 118 | +b.bind("<Button-1>", click) |
| 119 | +b = ttk.Button(my_frame3, text='*') |
| 120 | +b.pack(side=LEFT) |
| 121 | +b.bind("<Button-1>", click) |
| 122 | + |
| 123 | + |
| 124 | +# Packing button under 'my_frame4' frame |
| 125 | +my_frame4 = Frame(a_root) |
| 126 | +my_frame4.pack() |
| 127 | +b = ttk.Button(my_frame4, text='1') |
| 128 | +b.pack(side=LEFT) |
| 129 | +b.bind("<Button-1>", click) |
| 130 | +b = ttk.Button(my_frame4, text='2') |
| 131 | +b.pack(side=LEFT) |
| 132 | +b.bind("<Button-1>", click) |
| 133 | +b = ttk.Button(my_frame4, text='3') |
| 134 | +b.pack(side=LEFT) |
| 135 | +b.bind("<Button-1>", click) |
| 136 | +b = ttk.Button(my_frame4, text='-') |
| 137 | +b.pack(side=LEFT) |
| 138 | +b.bind("<Button-1>", click) |
| 139 | +# Packing button under 'my_frame5' frame |
| 140 | +my_frame5 = Frame(a_root) |
| 141 | +my_frame5.pack() |
| 142 | +b = ttk.Button(my_frame5, text='0') |
| 143 | +b.pack(side=LEFT) |
| 144 | +b.bind("<Button-1>", click) |
| 145 | +b = ttk.Button(my_frame5, text='00') |
| 146 | +b.pack(side=LEFT) |
| 147 | +b.bind("<Button-1>", click) |
| 148 | +b = ttk.Button(my_frame5, text='.') |
| 149 | +b.pack(side=LEFT) |
| 150 | +b.bind("<Button-1>", click) |
| 151 | +b = ttk.Button(my_frame5, text='+') |
| 152 | +b.pack(side=LEFT) |
| 153 | +b.bind("<Button-1>", click) |
| 154 | + |
| 155 | + |
| 156 | +# Packing button under 'my_frame6' frame |
| 157 | +my_frame6 = Frame(a_root) |
| 158 | +my_frame6.pack() |
| 159 | +Button(my_frame6, text='Clear All', command=clean, padx=117, font='clibri 11 bold', bg='grey').pack(side=LEFT) |
| 160 | + |
| 161 | + |
| 162 | +a_root.mainloop() |
| 163 | +# finish |
| 164 | + |
0 commit comments