#-*-coding: utf-8-*-
from Tkinter import *
import math
class calc:
def getandreplace(self):
"""replace x with * and ÷ with /"""
self.expression = self.e.get()
self.newtext=self.expression.replace(self.newdiv,'/')
self.newtext=self.newtext.replace('x','*')
def equals(self):
"""when the equal button is pressed"""
self.getandreplace()
try:
self.value= eval(self.newtext) #evaluate the expression using the eval function
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
self.e.delete(0,END)
self.e.insert(0,self.value)
def squareroot(self):
"""squareroot method"""
self.getandreplace()
try:
self.value= eval(self.newtext) #evaluate the expression using the eval function
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
self.sqrtval=math.sqrt(self.value)
self.e.delete(0,END)
self.e.insert(0,self.sqrtval)
def volumesphere(self):
self.getandreplace()
try:
self.value= eval(self.newtext)
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
self.totval=(4*math.pi*(self.value**3))/3
self.e.delete(0,END)
self.e.insert(0,self.totval)
# new linee
def sphere(self):
self.getandreplace()
try:
self.value= eval(self.newtext)
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
self.lval=(4*math.pi*math.pow(self.value,2))
self.e.delete(0,END)
self.e.insert(0,self.lval)
def sa(self):
self.getandreplace()
try:
self.value= eval(self.newtext)
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
float(self.value)
l = (math.sqrt(math.pow(self.value,2)+(math.pow(self,2))))
self.saval = ( math.pi*self*l)+(math.pi*(math.pow(self,2)))
self.e.delete(0,END)
self.e.insert(0,self.saval)
def square(self):
"""square method"""
self.getandreplace()
try:
self.value= eval(self.newtext) #evaluate the expression using the eval function
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
self.sqval=math.pow(self.value,2)
self.e.delete(0,END)
self.e.insert(0,self.sqval)
def clearall(self):
"""when clear button is pressed,clears the text input area"""
self.e.delete(0,END)
def clear1(self):
self.txt=self.e.get()[:-1]
self.e.delete(0,END)
self.e.insert(0,self.txt)
def action(self,argi):
"""pressed button's value is inserted into the end of the text area"""
self.e.insert(END,argi)
def __init__(self,master):
"""Constructor method"""
master.title('Calulator')
master.geometry()
self.e = Entry(master)
self.e.grid(row=0,column=0,columnspan=9,pady=6)
self.e.focus_set() #Sets focus on the input text area
self.div='÷'
self.newdiv=self.div.decode('utf-8')
#Generating Buttons
Button(master,text="=",width=10,command=lambda:self.equals()).grid(row=4, column=4,columnspan=2)
Button(master,text='AC',width=3,command=lambda:self.clearall()).grid(row=1, column=4)
Button(master,text='CL',width=3,command=lambda:self.clear1()).grid(row=1, column=5)
Button(master,text="+",width=3,command=lambda:self.action('+')).grid(row=4, column=3)
Button(master,text="x",width=3,command=lambda:self.action('x')).grid(row=2, column=3)
Button(master,text="-",width=3,command=lambda:self.action('-')).grid(row=3, column=3)
Button(master,text="÷",width=3,command=lambda:self.action(self.newdiv)).grid(row=1, column=3)
Button(master,text="%",width=3,command=lambda:self.action('%')).grid(row=4, column=2)
Button(master,text="7",width=3,command=lambda:self.action('7')).grid(row=1, column=0)
Button(master,text="8",width=3,command=lambda:self.action(8)).grid(row=1, column=1)
Button(master,text="9",width=3,command=lambda:self.action(9)).grid(row=1, column=2)
Button(master,text="4",width=3,command=lambda:self.action(4)).grid(row=2, column=0)
Button(master,text="5",width=3,command=lambda:self.action(5)).grid(row=2, column=1)
Button(master,text="6",width=3,command=lambda:self.action(6)).grid(row=2, column=2)
Button(master,text="1",width=3,command=lambda:self.action(1)).grid(row=3, column=0)
Button(master,text="2",width=3,command=lambda:self.action(2)).grid(row=3, column=1)
Button(master,text="3",width=3,command=lambda:self.action(3)).grid(row=3, column=2)
Button(master,text="0",width=3,command=lambda:self.action(0)).grid(row=4, column=0)
Button(master,text=".",width=3,command=lambda:self.action('.')).grid(row=4, column=1)
Button(master,text="(",width=3,command=lambda:self.action('(')).grid(row=2, column=4)
Button(master,text=")",width=3,command=lambda:self.action(')')).grid(row=2, column=5)
Button(master,text="√",width=3,command=lambda:self.squareroot()).grid(row=3, column=4)
Button(master,text="x²",width=3,command=lambda:self.square()).grid(row=3, column=5)
Button(master,text="VS",width=3,command=lambda:self.volumesphere()).grid(row=3, column=6)
Button(master,text="SAS",width=3,command=lambda:self.sphere()).grid(row=2, column=6)
Button(master,text="SAC",width=3,command=lambda:self.sa()).grid(row=1, column=6)
#Main
root = Tk()
obj=calc(root) #object instantiated
root.mainloop()
这与python 2.7.10并且我需要帮助制作一个按钮,自动计算tkinter中锥体的表面积,因为它需要2个参数。功能 是def sa(self)代码“SAC”上的最后一个按钮假设代表锥体的表面区域。仅为了您的信息,我没有制作此代码,只是根据我的需要进行了修改并继续
答案 0 :(得分:0)
使用self.e
将Entry
然后split()
的文字转换为使用空格的元素。
def sa(self):
values = self.e.get().strip().split(' ')
if len(values) < 2:
self.e.delete(0,END)
self.e.insert(0,'Need two values ("r" and "h") separated by space')
else:
r = float(values[0])
h = float(values[1])
self.saval = math.pi*r*h + math.pi*math.pow(r,2)
self.e.delete(0,END)
self.e.insert(0,self.saval)