Create a graphical user interface that is suitable for use with the program you
ID: 3823181 • Letter: C
Question
Create a graphical user interface that is suitable for use with the program you wrote for below program. Your interface should reflect both the information you need to prompt for and what the output would look like based on your answer to below program. #!/usr/bin/python import math class PI: pi=0 def calculate_pi(self,n): sum=0 for j in range(0,n): sum = sum + (math.pow(-1,j)/((2*j)+1)) #summation of the Leibniz series pi = 4 * sum return float(pi) #return number def use_pi(self,r,pi): #shows usage of pi attribute in class radius=r area=pi*radius*radius #calculates Circle area return float(area) #print the output obj=PI() n = input('Enter number of values to be used in the pi calculation : ') p=obj.calculate_pi(int(n)) print (" pi = ", p) rad = input(' Enter radius of Circle : ') print (" Area of Circle = ", obj.use_pi(int(rad),p))
Explanation / Answer
I've used Tkinter package for GUI.
import math
import Tkinter
main = Tkinter.Tk() #Creating main window.
class PI:
pi=0
def calculate_pi(self,n):
sum=0
for j in range(0,n):
sum = sum + (math.pow(-1,j)/((2*j)+1)) #summation of the Leibniz series
pi = 4 * sum
return float(pi) #return number
def use_pi(self,r,pi): #shows usage of pi attribute in class
radius=r
area=pi*radius*radius #calculates Circle area
return float(area)
p = 0.0
obj=PI()
def reading_radius():
global p
rad = entry2.get()#Getting text from entry box.
entry2.delete(0, 'end')#Clearing entry box.
area_display = ' Area of Circle = ' + str(obj.use_pi(int(rad),p))
#Displaying result in a label.
label4 = Tkinter.Label(main, text = area_display, font = ('Candara', 12, 'bold'))
label4.pack()
def reading_input():
global p
n = entry1.get()
entry1.delete(0, 'end')
p=obj.calculate_pi(int(n))
pi_display = ' PI = ' + str(p)
label2 = Tkinter.Label(main, text = pi_display, font = ('Candara', 12, 'bold'))
label2.pack()
#Adding components to main window.
label1 = Tkinter.Label(main, text = 'Enter number of values to be used in the pi calculation :', font = ('Candara', 12, 'bold'))
label1.pack()#Using pack() method to place component to window.
entry1 = Tkinter.Entry(main, font = ('Candara', 11), width = 40, justify = 'right')
entry1.pack()
#Adding submit button for reading first input.
submit1 = Tkinter.Button(main, font = ('Candara', 12, 'bold'), text = 'Submit', command = reading_input)
submit1.pack()
label3 = Tkinter.Label(main, text = 'Enter radius of circle :', font = ('Candara', 12, 'bold'))
label3.pack()
entry2 = Tkinter.Entry(main, font = ('Candara', 11), width = 40, justify = 'right')
entry2.pack()
submit2 = Tkinter.Button(main, font = ('Candara', 12, 'bold'), text = 'Submit', command = reading_radius)
submit2.pack()
#Running GUI Application in mainloop().
main.mainloop()
Screenshots:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.