python 计算器 casio

编程入门 行业动态 更新时间:2024-10-19 10:20:25

python <a href=https://www.elefans.com/category/jswz/34/1765079.html style=计算器 casio"/>

python 计算器 casio

界面方式:

#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

更多推荐

python 计算器 casio

本文发布于:2024-02-12 03:39:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1685692.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计算器   python   casio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!