-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
67 lines (65 loc) · 2.43 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from functools import partial
import tkinter as tk
class calcku(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.wm_title("Kalkulator sederhana")
self.geometry("290x400")
self.membuatButton()
def membuatButton(self):
self.layar=tk.Entry(self,width=40)
self.layar.grid(row=0,column=0,padx=20,pady=10,columnspan=3)
buttonList=[
"1",'2','3','4','5','6','7','8','9','-','0','+','C','/','x','<=','='
]
row=1
column=0
for text in buttonList:
perintah=partial(self.tambah,text)
self.layar.bind('<Return>',self.hai)
if text == "x":
perintah=partial(self.tambah,"*")
tk.Button(self,text=text,height=3,width=2,padx=30,pady=2,command=perintah).grid(row=row,column=column)
elif text == "=":
tk.Button(self,text=text,width=24,pady=17,command=perintah).grid(row=row,column=column,columnspan=3,pady=10)
else:
tk.Button(self,text=text,height=3,width=2,padx=30,pady=2,command=perintah).grid(row=row,column=column)
column+=1
if column>2:
column=0
row+=1
def tambah(self,key):
if self.layar.get() == "Masukan Perintah Dengan Benar":
self.layar.delete(0,tk.END)
elif key=="C":
self.layar.delete(0,tk.END)
elif key == "<=":
self.layar.delete(len(self.layar.get())-1,tk.END)
elif key == "=":
result=self.layar.get()
print(result)
if "=" in result:
self.layar.delete(len(self.layar.get)-1,tk.END)
if len(result) != 0:
try:
result=eval(result)
self.layar.delete(0,tk.END)
self.layar.insert(tk.END,result)
except:
self.layar.delete(0,tk.END)
self.layar.insert(tk.END,"Masukan Perintah Dengan Benar")
else:
self.layar.insert(tk.END,key)
def hai(self,x):
result=self.layar.get()
print(result)
if "=" in result:
self.layar.delete(len(result)-1,tk.END)
result=self.layar.get()
self.layar.delete(0,tk.END)
self.layar.insert(tk.END,eval(result))
if __name__ == "__main__":
kalkulator= calcku()
kalkulator.mainloop()