如何用python做一个简易的计算器

 这是代码:

import tkinter as tk
tx1=''
tx2=''
op=''
re=''
t=tk.Tk()
t.geometry('400x300')
te=tk.StringVar()
te.set('')
l1=tk.Label(t,textvariable=te,bg='black',fg='white',anchor='w')
l1.place(x=0,y=0,width=400,height=100)
def cefun(n):  #读入操作 重点
    if n=='0':
        if te.get()!='0':
            te.set(te.get()+n)
        else:
            te.set('0')
    elif te.get()!='0':
        te.set(te.get()+n)
    else:
        te.set(n)
def cfun():  #清除操作
    te.set('')
def opfun(ope): #运算符读入
    global tx1,op
    if te.get()!='':
        tx1=te.get()
        op=ope
        te.set('')
def cal():      #计算模块
    global tx1,tx2,op,re
    if te.get()!='':
        tx2=te.get()
        if op=='+':
            re=str(eval(tx1)+eval(tx2))
        elif op=='-':
            re=str(eval(tx1)-eval(tx2))
        elif op=='*':
            re=str(eval(tx1)*eval(tx2))
        elif op=='/':
            if tx2!=0:
                re=str(eval(tx1)/eval(tx2))
        te.set(re)
# 以下是按照自己的喜好建立图标
b1=tk.Button(t,text='1',width=5,height=2,command=lambda:cefun('1')) #command调用函数,'1'为调用参数值
b1.place(x=0,y=100,width=100,height=50)
b2=tk.Button(t,text='2',width=5,height=2,command=lambda:cefun('2'))
b2.place(x=100,y=100,width=100,height=50)
b3=tk.Button(t,text='3',width=5,height=2,command=lambda:cefun('3'))
b3.place(x=200,y=100,width=100,height=50)
b4=tk.Button(t,text='4',width=5,height=2,command=lambda:cefun('4'))
b4.place(x=300,y=100,width=100,height=50)
b5=tk.Button(t,text='5',width=5,height=2,command=lambda:cefun('5'))
b5.place(x=0,y=150,width=100,height=50)
b6=tk.Button(t,text='6',width=5,height=2,command=lambda:cefun('6'))
b6.place(x=100,y=150,width=100,height=50)
b7=tk.Button(t,text='7',width=5,height=2,command=lambda:cefun('7'))
b7.place(x=200,y=150,width=100,height=50)
b8=tk.Button(t,text='8',width=5,height=2,command=lambda:cefun('8'))
b8.place(x=300,y=150,width=100,height=50)
b9=tk.Button(t,text='9',width=5,height=2,command=lambda:cefun('9'))
b9.place(x=0,y=200,width=100,height=50)
b0=tk.Button(t,text='0',width=5,height=2,command=lambda:cefun('0'))
b0.place(x=100,y=200,width=100,height=50)
bc=tk.Button(t,text='c',width=5,height=2,command=cfun)
bc.place(x=200,y=200,width=100,height=50)
bce=tk.Button(t,text='=',width=5,height=2,command=cal)
bce.place(x=300,y=200,width=100,height=50)
ba=tk.Button(t,text='+',width=5,height=2,command=lambda:opfun('+'))
ba.place(x=0,y=250,width=100,height=50)
bmi=tk.Button(t,text='-',width=5,height=2,command=lambda:opfun('-'))
bmi.place(x=100,y=250,width=100,height=50)
bmu=tk.Button(t,text='*',width=5,height=2,command=lambda:opfun('*'))
bmu.place(x=200,y=250,width=100,height=50)
bd=tk.Button(t,text='/',width=5,height=2,command=lambda:opfun('/'))
bd.place(x=300,y=250,width=100,height=50)
t.mainloop()

这是做出来的形状:


重点就是:

1、形状建设

2、0的特判

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值