-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.pyw
More file actions
75 lines (67 loc) · 2.58 KB
/
Calculator.pyw
File metadata and controls
75 lines (67 loc) · 2.58 KB
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
71
72
73
74
75
from tkinter import *
# ===============================
window = Tk()
window.title('ماشین حساب')
window.geometry("250x300")
window.resizable(width=False, height=False)
window.iconbitmap("calc.ico")
color = "#0F7BDB"
window.configure(bg=color)
num1 = StringVar()
num2 = StringVar()
# ===============================
Label(window, text="مقدار را وارد کنید",bg=color,fg="white",font=(None,17)).place(x=55,y=5)
# ===============================
first_num = Entry(window, textvariable=num1).place(x= 30 ,y=50)
Label(window, text=":عدد اول",bg=color,fg="white",font=(None,13)).place(x=180,y=50)
# ===============================
second_num = Entry(window, textvariable=num2).place(x=30,y=90)
Label(window, text=":عدد دوم",bg=color,fg="white",font=(None,13)).place(x=180,y=90)
# ===============================
B_mul =Button(window, text = "X",font=(None,20),command= lambda:mul()) .place(x=20, y=130,width=45)
B_plus =Button(window, text = "+",font=(None,20),command= lambda:plus()) .place(x=75, y=130,width=45)
B_minus =Button(window, text = "-",font=(None,20),command= lambda:minus()) .place(x=133,y=130,width=45)
B_div =Button(window, text = "÷",font=(None,20),command= lambda:div()) .place(x=190,y=130,width=45)
EXIT =Button(window, text="خروج",width=10,height=2 ,command=window.destroy).place(x=85,y=250)
B_result =Label(window,bg=color, width=20,font=(None,13))
B_result.place(x=30,y=200)
def result(x):
if x == 'error':
B_result.config(bg ='red')
B_result.config(text = '!مشکلی پیش آمد')
elif x == 'division zero error':
B_result.config(bg = 'pink')
B_result.config(fg = 'black')
B_result.config(text= 'نمیتوان بر صفر تقسیم کرد')
else:
B_result.config(bg = 'black')
B_result.config(fg = 'white')
B_result.config(text = x)
def mul():
try:
value = float(num1.get()) * float(num2.get())
result(value)
except:
result('error')
def plus():
try:
value = float(num1.get()) + float(num2.get())
result(value)
except:
result('error')
def minus():
try:
value = float(num1.get()) - float(num2.get())
result(value)
except:
result('error')
def div():
if num2.get() == '0':
result('division zero error')
elif num2.get() != '0':
try:
value = float(num1.get()) / float(num2.get())
result(value)
except:
result('error')
window.mainloop()