-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple graphical complex number calculator.py
157 lines (121 loc) · 5.73 KB
/
simple graphical complex number calculator.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#Name: Rajkumar Kushwaha
#Built a Complex calculator using built in complex
from tkinter import *
# importing everything from tkinter module
root= Tk()
root.title("complex calculator")
#setting the whole window size i.e giving the dimensions
#for every object on the calculator screen
width=600
height=500
screenwidth=root.winfo_screenwidth()
screenheight=root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=True,height=True)
# Creating the entry fields for complex numbers
# Adding Label
inputtxt = Label(root, text="First Complex:", font=("Arial",14))
inputtxt.grid(row=1,column=1)
#@entry field Firstcomplex realpart
Realpart1 = Entry(root, width=11, font=("Arial", 12))
Realpart1.grid(row = 1, column = 2, padx=10, pady=10)
#@entry field Firstcomplex imaginarypart
Imaginarypart1 = Entry(root, width=11, font=("Arial", 12))
Imaginarypart1.grid(row=1,column=3, padx=10, pady=10)
#Firstcomplex real part
firstlabelreal = Label(root, text="Real Part", width=11, font=("Arial", 12))
firstlabelreal.grid(row = 2, column = 2)
#and imaginary part labels
firstlabelimag = Label(root, text="Imaginary Part", width=11, font=("Arial", 12))
firstlabelimag.grid(row =2, column = 3)
# Second complex
# Adding Label
inputtxt = Label(root, text="Second Complex:", font= ("Arial", 14))
inputtxt.grid(row=3,column=1)
Realpart2 = Entry(root, width=11, font=("Arial", 12))
Realpart2.grid(row=3, column=2, padx=10, pady=10)
Imaginarypart2 = Entry(root, width=11, font=("Arial", 12))
Imaginarypart2.grid(row=3, column=3, padx=10, pady=10)
#secondcomplex real part and imaginary part text labels
firstlabelreal = Label(root, text="Real Part", width=11, font=("Arial", 12))
firstlabelreal.grid(row = 4, column = 2)
#Firstcomplex real part and imaginary part labels
firstlabelreal = Label(root, text="Imaginary Part", width=11, font=("Arial", 12))
firstlabelreal.grid(row = 4, column = 3)
#Result label
inputtxt = Label(root, text="Result:", width=11, font= ("Arial", 14))
inputtxt.grid(row=10,column=1)
#entry field Creating the real result field
result = Entry(root, width=11, font=("Arial", 12))
result.grid(row=10, column= 2, padx=10, pady=10)
#result field for imaginary field
result2 = Entry(root, width=11, font=("Arial", 12))
result2.grid(row=10,column=3, padx=10, pady=10)
#label for real field
firstlabelreal = Label(root, text="Real Part", width=11, font= ("Arial",12))
firstlabelreal.grid(row =11, column = 2)
#label for imaginary field
firstlabelreal = Label(root, text="imaginary Part", width=11, font = ("Arial",12))
firstlabelreal.grid(row =11, column = 3)
#used Built in complex, (can also be done using cplex)
#Creating the function for the operations
def add():
num1 = complex(int(Realpart1.get()),int(Imaginarypart1.get()))
#num2 = complex(Imaginarypart1.get())
num2 = complex(int(Realpart2.get()),int(Imaginarypart2.get()))
#num4 = complex(Imaginarypart2.get())
sum=num1+num2
result.delete(0, END)
result.insert(END, sum.real)
result2.delete(0, END)
result2.insert(END, sum.imag)
def subtract():
num1 = complex(int(Realpart1.get()),int(Imaginarypart1.get()))
num2 = complex(int(Realpart2.get()),int(Imaginarypart2.get()))
subtract=num1-num2
result.delete(0, END)
result.insert(END,subtract.real)
result2.delete(0,END)
result2.insert(END, subtract.imag)
def multiply():
num1 = complex(int(Realpart1.get()),int(Imaginarypart1.get()))
num2 = complex(int(Realpart2.get()),int(Imaginarypart2.get()))
multiply=num1*num2
result.delete(0, END)
result.insert(END, multiply.real)
result2.delete(0,END)
result2.insert(END, multiply.imag)
def divide():
num1 = complex(int(Realpart1.get()),int(Imaginarypart1.get()))
num2 = complex(int(Realpart2.get()),int(Imaginarypart2.get()))
divide = num1/num2
result.delete(0, END)
result.insert(END, divide.real)
result2.delete(0, END)
result2.insert(END, divide.imag)
# Create the buttons
addButton = Button(root, text="+", command=add, height=2,width=10)
addButton.grid(row=8, column=2, padx=7)
subtractButton = Button(root, text="-", command=subtract, height=2,width=10)
subtractButton.grid(row=8, column=3, padx=7)
multiplyButton = Button(root, text="*", command=multiply, height=2,width=10)
multiplyButton.grid(row=8, column=4, padx=7)
divideButton = Button(root, text="/", command=divide, height=2,width=10)
divideButton.grid(row=8, column=5, padx=7)
#clearButton = Button(root, text="Clear Entries", command=lambda: Realpart1.delete(0, END) or Imaginarypart1.delete(0, END) or result.delete(0, END) or Realpart2.delete(0,END) or Imaginarypart2.delete(0,END) or result2.delete(0,END), height=2,width=10)
#clearButton.grid(row=12, column=5, padx=10)
firstlabelreal = Label(root, text="Add", width=11, font= ("Arial",12))
firstlabelreal.grid(row =9, column = 2)
firstlabelreal = Label(root, text="Subtract", width=11, font= ("Arial",12))
firstlabelreal.grid(row =9, column = 3)
firstlabelreal = Label(root, text="Multiply", width=11, font= ("Arial",12))
firstlabelreal.grid(row =9, column = 4)
firstlabelreal = Label(root, text="Divide", width=11, font= ("Arial",12))
firstlabelreal.grid(row =9, column = 5)
clearButton = Button(root, text="Clear Entries", command=lambda: Realpart1.delete(0, END) or Imaginarypart1.delete(0, END) or result.delete(0, END) or Realpart2.delete(0,END) or Imaginarypart2.delete(0,END) or result2.delete(0,END), height=3,width=10)
clearButton.grid(row=12, column=5, padx=10)
#label for opertions
inputtxt = Label(root, text="Opertions", font=("Arial",14))
inputtxt.grid(row=8,column=1)
root.mainloop()