界面方式:
#coding=utf-8
#by : linhut
from Tkinter import * #导入Tkinter所有组件
def clear():
display.set('')
def dele():
display.set(str(display.get()[:-1])) #取最后一位前面的,转数字
def call(m):
content = display.get()+m #display.get()获得当前内容
display.set(content) #display.set() 显示内容
# 得到 '2+3'
def calee():
if '=' in display.get(): #判断当前内容是否进行过计算
con = display.get().split('=')[-1]
else:
con = display.get()
try:
res =eval(con) #eval字符可计算
display.set(con + '=\n' + str(res))
#得到 2+3=5
except:
display.set('Error')
clear()
def main():
root = Tk() # 创建顶层窗口
root.title('Linhut计算器')
root.geometry() #设置界面大小
global display #创建一个全局变量
display=StringVar() #用来得到和设置显示的内容
label = Label(root,relief='sunken',borderwidth=3,anchor=SE)
#relief设置边框样式,borderwidth设置边框的宽度,anchor设置pack分配的位置
label.config(bg='grey',width=25,height=3)
label['textvariable'] = display
#textvariable显示display变量的内容
label.grid(row=0,column=0,columnspan = 4)
#columnspan设置用几列来显示该组件
Button(root,text='C',fg = 'Green',width=3,command=lambda:clear()).grid(row=1,column=0)
Button(root,text='DEL',width=3,command=lambda:dele()).grid(row=1,column=1)
Button(root,text='/', width=3, command=lambda: call('/')).grid(row=1, column=2)
Button(root,text='*', width=3, command=lambda: call('*')).grid(row=1, column=3)
Button(root,text='-', width=3, command=lambda: call('-')).grid(row=2, column=3)
Button(root,text='+', width=3, command=lambda: call('+')).grid(row=3, column=3)
Button(root,text='%', width=3, command=lambda: call('%')).grid(row=4, column=3)
Button(root,text='.', width=3, command=lambda: call('.')).grid(row=5, column=1)
Button(root,text='1', width=3, command=lambda: call('1')).grid(row=4, column=0)
Button(root,text='2', width=3, command=lambda: call('2')).grid(row=4, column=1)
Button(root,text='3', width=3, command=lambda: call('3')).grid(row=4, column=2)
Button(root,text='4', width=3, command=lambda: call('4')).grid(row=3, column=0)
Button(root,text='5', width=3, command=lambda: call('5')).grid(row=3, column=1)
Button(root,text='6', width=3, command=lambda: call('6')).grid(row=3, column=2)
Button(root,text='7', width=3, command=lambda: call('7')).grid(row=2, column=0)
Button(root,text='8', width=3, command=lambda: call('8')).grid(row=2, column=1)
Button(root,text='9', width=3, command=lambda: call('9')).grid(row=2, column=2)
Button(root,text='0', width=3, command=lambda: call('0')).grid(row=5, column=0)
Button(root,text='=', width=3, command=lambda: calee()).grid(row=5, column=2,rowspan=3)
root.mainloop() # 显示界面
if __name__ == '__main__':
main()
字符方式:
#coding=utf-8
#by : linhut
def jia(x,y):
return (x+y)
def jian(x,y):
return (x-y)
def cheng(x,y):
return (x*y)
def chu(x,y):
return (x/y)
func_dict = {'+':jia,'-':jian,'*':cheng,'/':chu}
oper_set = {'+','-','*','/'}
#运算符集合
#一行来输入,并且完成单项运算就OK
#2 * 3
#2+3
result = 0
while True:
mystr = input('请输入你要做的操作:')
mystr_set = set(mystr.strip())
#去掉空格 变成集合
oper = (mystr_set&oper_set).pop()
#取出运算符
num1 = int(mystr.split(oper)[0])
num2 = int(mystr.split(oper)[1])
result = func_dict[oper](num1,num2)
print('结果是:',result)
mylist = [1,2,-3]
def getsum(mylist):
mysum = 0
for var in mylist:
mysum += var
return mysum
def jiajian(*arg):
res = 0
for var in arg:
res += var
#就一步
def chengchu(oper,*arg):
res = 0
if oper == '*':
for var in arg:
res *= arg
if oper == '/':
for var in arg:
res /= arg