-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (60 loc) · 1.61 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
68
69
70
import tkinter as tk
window = tk.Tk()
window.title = "Calculator"
window.geometry("300x400")
window.configure(bg = "#303030")
window.resizable(False, False)
input = ""
display = tk.Label(
window,
text = input,
width = 280,
fg = "#ffffff",
bg = "#474747",
anchor = "w",
padx = 15,
pady = 15
)
display.config(font = ("Courier New", 24))
display.pack(padx = 10, pady = 10)
def clicked(ch):
global input
input += str(ch)
display.config(text = input)
def clear():
global input
input = ""
display.config(text = "")
def equals():
global input
try:
display.config(text=eval(input))
input=""
except:
display.config(text="ERROR")
def main():
labels = [
["0", "1", "4", "7", "C"],
[".", "2", "5", "8", "("],
["%", "3", "6", "9", ")"],
["=", "+", "-", "*", "/"]
]
buttons = [[0 for x in range(5)] for y in range(4)]
for i in range(4):
for j in range(5):
buttons[i][j] = tk.Button(
window,
text = labels[i][j],
font = ("Courier New", 24),
fg = "#ffffff",
bg = "#474747",
anchor = "center",
command = lambda i = i, j = j: clicked(labels[i][j])
)
buttons[i][j].place(x = 10 + i * 78, y = 400 - (j + 1) * 60)
buttons[2][0].config(command = lambda: clicked("*0.01"))
buttons[0][4].config(command = clear)
buttons[3][0].config(command = equals)
window.mainloop()
if __name__=="__main__":
main()