-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal3.py
104 lines (78 loc) · 3.18 KB
/
cal3.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from tkinter import *
import tkinter.messagebox
#--------------Setting-------------
main_page = Tk()
main_page.geometry("500x240")
main_page.title("Calculator")
main_page.resizable(width=False , height=False)
main_page.configure(bg = 'gray') # BackGroung Color
#--------------Variables---------------
num1 = StringVar()
num2 = StringVar()
res_num = StringVar()
#--------------Functions---------------
def errorinput(message):
if message == "error":
tkinter.messagebox.showerror('error' , 'It is wrong Number!')
elif message == "Bad Division!" :
tkinter.messagebox.showerror('error' , 'It is wrong Number --> 0')
def plusnums():
try:
value = float(num1.get()) + float(num2.get())
res_num.set(value)
except:
errorinput("error")
def minusnums():
try:
value = float(num1.get()) - float(num2.get())
res_num.set(value)
except:
errorinput("error")
def multiplenums():
try:
value = float(num1.get()) * float(num2.get())
res_num.set(value)
except:
errorinput("error")
def dividenums():
if num2.get() == '0':
errorinput('Bad Division!')
elif num2.get():
try:
value = float(num1.get()) / float(num2.get())
res_num.set(value)
except:
errorinput("error")
#--------------Make Frames---------------
first_frame = Frame(main_page , width=500, height=60 , bg='gray')
first_frame.pack(side=TOP) # Top of GUI
second_frame = Frame(main_page , width=500, height=60 , bg='gray')
second_frame.pack(side=TOP)
third_frame = Frame(main_page , width=500, height=60 , bg='blue')
third_frame.pack(side=TOP)
forth_frame = Frame(main_page , width=500, height=60 , bg='gray')
forth_frame.pack(side=TOP)
#--------------Create Buttons---------------
plus = Button(third_frame, text="+", width=10,
highlightbackground='gray', command=lambda: plusnums())
plus.pack(side=LEFT, padx=10, pady=10)
minus = Button(third_frame , text="-" , highlightbackground= 'yellow', width=10, command=lambda:minusnums()) # master is the specific frame
minus.pack(side=LEFT,padx=5 , pady=5)
multiple = Button(third_frame , text="*" , highlightbackground= 'yellow', width=10, command=lambda:multiplenums()) # master is the specific frame
multiple.pack(side=LEFT,padx=5 , pady=5)
divide = Button(third_frame , text="/" , highlightbackground= 'yellow', width=10, command=lambda:dividenums()) # master is the specific frame
divide.pack(side=LEFT,padx=5 , pady=5)
#--------------Inputs---------------
first_label = Label(first_frame , text="Num1" , bg='gray')
first_label.pack(side=LEFT, pady=5 , padx=5)
first_num = Entry(first_frame , highlightbackground= 'yellow' , textvariable=num1)
first_num.pack(side=LEFT)
second_label = Label(second_frame , text="Num2" , bg='gray')
second_label.pack(side=LEFT, pady=5 , padx=5)
second_num = Entry(second_frame , highlightbackground= 'yellow', textvariable=num2)
second_num.pack(side=LEFT)
result = Label(forth_frame , text="Result:" , bg='gray')
result.pack(side=LEFT, pady=5 , padx=5)
result_num = Entry(forth_frame , highlightbackground= 'yellow', textvariable=res_num)
result_num.pack(side=LEFT)
main_page.mainloop()