Python简单实现计算器

这篇博客分享了如何使用Python的Tkinter库创建一个简单的图形界面计算器。作者通过定义不同的按钮功能,包括数字输入、运算符选择和计算结果展示,实现了基本的加减乘除运算。此外,还包含了清零和删除功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python简单实现计算器
最近刚刚学完python基础,就想着自己试试编写一个计算器来。还有很多不足的地方大家可以交流一下。

from tkinter import *
class calculator:
    def __init__(self,root):
        self.v1 = StringVar()
        self.v1.set(0)
        self.iscal = False#设置一个变量判断是否按下运算键

        #设置两个frame来存放输入框和按键
        frame1 = Frame(root)
        frame1.pack()
        frame2 = Frame(root)
        frame2.pack()

        e1 = Entry(frame1, width=32, textvariable=self.v1, state='readonly')
        e1.grid(row=0, column=1)
        Button(frame2, text='1',height=3,width=5,command=lambda: self.numbutton('1')).grid(row=1, column=0)
        Button(frame2, text='2', height=3,width=5,command=lambda: self.numbutton('2')).grid(row=1, column=1)
        Button(frame2, text='3', height=3,width=5,command=lambda: self.numbutton('3')).grid(row=1, column=2)
        Button(frame2, text='4', height=3,width=5,command=lambda: self.numbutton('4')).grid(row=2, column=0)
        Button(frame2, text='5', height=3,width=5,command=lambda: self.numbutton('5')).grid(row=2, column=1)
        Button(frame2, text='6', height=3,width=5,command=lambda: self.numbutton('6')).grid(row=2, column=2)
        Button(frame2, text='7', height=3,width=5,command=lambda: self.numbutton('7')).grid(row=3, column=0)
        Button(frame2, text='8', height=3,width=5,command=lambda: self.numbutton('8')).grid(row=3, column=1)
        Button(frame2, text='9', height=3,width=5,command=lambda: self.numbutton('9')).grid(row=3, column=2)
        Button(frame2, text='0', height=3,width=5,command=lambda: self.numbutton('0')).grid(row=4, column=1)

        Button(frame2, text='+', height=3,width=5,command=lambda: self.calbutton('+')).grid(row=1, column=3)
        Button(frame2, text='-', height=3,width=5,command=lambda: self.calbutton('-')).grid(row=2, column=3)
        Button(frame2, text='*', height=3,width=5,command=lambda: self.calbutton('*')).grid(row=3, column=3)
        Button(frame2, text='/', height=3,width=5,command=lambda: self.calbutton('/')).grid(row=4, column=3)
        Button(frame2, text='=', height=3,width=5,command=lambda: self.calc()).grid(row=4, column=4)
        Button(frame2, text='C', height=3,width=5,command=lambda: self.clear()).grid(row=4, column=0)
        Button(frame2, text='.', height=3, width=5, command=lambda: self.calbutton('.')).grid(row=4, column=2)
        Button(frame2, text='M+', height=3, width=5, command=lambda: self.new()).grid(row=3, column=4)
        Button(frame2, text='M-', height=3, width=5, command=lambda: self.new()).grid(row=2, column=4)
        Button(frame2, text='<-', height=3, width=5, command=lambda: self.dele()).grid(row=1, column=4)

    #输入数字的函数
    def numbutton(self,num):
        oldnum = self.v1.get()
        if oldnum == '0':
            self.v1.set(num)
        else:
            newnum = oldnum + num
            self.v1.set(newnum)

    #输入运算符的函数
    def calbutton(self,cal):
        if self.iscal == False:
                self.v1.set(self.v1.get()+cal)
                self.iscal = True
        else:
            if self.v1.get()[-1] in ['+','-','*','/','.']:
                #v1.get[-1]用来获取v1.get的最后一个值
                self.v1.set(self.v1.get()[:-1])
                #v1.get()[:-1]用来获取除了最后一个值以外的值
                self.v1.set(self.v1.get() + cal)
            else:
                self.v1.set(self.v1.get() + cal)

    #计算函数
    def calc(self):
        self.v1.set(self.v1.get()+'='+str(eval(self.v1.get())))
            #eval()可以直接计算str内的计算式

    #清除函数
    def clear(self):
        self.v1.set(0)

    #尚未开发的函数
    def new(self):
        pass

    #删除单个字符串
    def dele(self):
        self.v1.set(self.v1.get()[:-1])


root = Tk()
cal = calculator(root)
mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值