tkinter制作一个简单计算器

文章展示了如何利用Python的tkinter库创建一个基础的图形用户界面计算器应用,包括清零、输入数字、操作符及结果显示等功能。代码中定义了不同按钮的点击事件,并实现了基本的加减乘除和百分比计算。虽然存在一些细节未处理,但整体实现了计算器的核心运算。

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

        我们知道tkinter是python常用的UI框架,那么它是如何使用的呢?我们用一个简单的例子来显示它的作用,制作一个简单的计算器,如下图所示。

上图是一个计算器,我们可以看出它一共有20个键,每个按键都表示一个功能,在最上方是一个文本框用来显示数值。接下来我们简单演示两个数相乘。

 从上图计算结果,可以看出它简单地实现了两数相乘功能。

 系统代码:

#!/user/bin/env python3
# -*- coding: utf-8 -*-
# author:Forge ahead
from tkinter import *

win = Tk()
win.title('计算器')
win.geometry('260x300+500+100')
text = Text(win, width=30, height=1)
text.grid(row=0, column=0, columnspan=50)


def btn1_click():
    text.delete(1.0, 'end')


def btn2_click():
    text.insert('end', '%')


def btn3_click():
    str = text.get(1.0, 'end')
    text.delete(1.0, 'end')
    text.insert(INSERT, str[:-2])


# 判断浮点数
def isFloatNum(str):
    s = str.split('.')
    if len(s) > 2:
        return False
    else:
        for si in s:
            if not si.isdigit():
                return False
        return True


# 计算
def calculate(str):
    global result
    for i in range(len(str)):
        if str[i] == '+' or str[i] == '-' or str[i] == 'x' or str[i] == '÷':
            a = i
            x = str[:a]
            y = str[a + 1:]
            if str[i] == '+':
                if x.isdigit() and y.isdigit():
                    result = int(x) + int(y)
                    break
                elif x.isdigit() and isFloatNum(y) == True:
                    result = '{:.2f}'.format(int(x) + float(y))
                    break
                elif isFloatNum(x) == True and y.isdigit():
                    result = '{:.2f}'.format(float(x) + int(y))
                    break
                elif isFloatNum(x) == True and isFloatNum(y) == True:
                    result = '{:.2f}'.format(float(x) + float(y))
                    break
            elif str[i] == '-':
                if x.isdigit() and y.isdigit():
                    result = int(x) - int(y)
                    break
                elif x.isdigit() and isFloatNum(y) == True:
                    result = '{:.2f}'.format(int(x) - float(y))
                    break
                elif isFloatNum(x) == True and y.isdigit():
                    result = '{:.2f}'.format(float(x) - int(y))
                    break
                elif isFloatNum(x) == True and isFloatNum(y) == True:
                    result = '{:.2f}'.format(float(x) - float(y))
                    break
            elif str[i] == 'x':
                if x.isdigit() and y.isdigit():
                    result = int(x) * int(y)
                    result=round(result,2)
                    break
                elif x.isdigit() and isFloatNum(y) == True:
                    result = '{:.2f}'.format(int(x) * float(y))
                    break
                elif isFloatNum(x) == True and y.isdigit():
                    result = '{:.2f}'.format(float(x) * int(y))
                    break
                elif isFloatNum(x) == True and isFloatNum(y) == True:
                    result = '{:.2f}'.format(float(x) * float(y))
                    break
            elif str[i] == '÷':
                if x.isdigit() and y.isdigit():
                    result = round(int(x) / int(y),2)
                    break
                elif x.isdigit() and isFloatNum(y) == True:
                    result = int(x) / float(y)
                    break
                elif isFloatNum(x) == True and y.isdigit():
                    result = float(x) / int(y)
                    break
                elif isFloatNum(x) == True and isFloatNum(y) == True:
                    result = float(x) / float(y)
                    break
        elif str[i] == '%':
            a = i
            x = str[:a]
            if x.isdigit():
                result = int(x) / 100
            elif isFloatNum(x):
                result = float(x) / 100
                break


def btn4_click():
    text.insert('end', '=')
    str = text.get(1.0, 'end')
    str = str[:-2]
    calculate(str)
    text.insert('end', result)


def btn5_click():
    text.insert('end', '1')


def btn6_click():
    text.insert('end', '2')


def btn7_click():
    text.insert('end', '3')


def btn8_click():
    text.insert('end', '+')


def btn9_click():
    text.insert('end', '4')


def btn10_click():
    text.insert('end', '5')


def btn11_click():
    text.insert('end', '6')


def btn12_click():
    text.insert('end', '-')


def btn13_click():
    text.insert('end', '7')


def btn14_click():
    text.insert('end', '8')


def btn15_click():
    text.insert('end', '9')


def btn16_click():
    text.insert('end', 'x')


def btn17_click():
    text.insert('end', '00')


def btn18_click():
    text.insert('end', '0')


def btn19_click():
    text.insert('end', '.')


def btn20_click():
    text.insert('end', '÷')


btn1 = Button(win, text='C', width=4, height=1, command=btn1_click)
btn1.grid(row=1, column=0)
btn2 = Button(win, text='%', width=4, height=1, command=btn2_click)
btn2.grid(row=1, column=1)
btn3 = Button(win, text='D', width=4, height=1, command=btn3_click)
btn3.grid(row=1, column=2)
btn4 = Button(win, text='=', width=4, height=1, command=btn4_click)
btn4.grid(row=1, column=3)
btn5 = Button(win, text='1', width=4, height=1, command=btn5_click)
btn5.grid(row=2, column=0)
btn6 = Button(win, text='2', width=4, height=1, command=btn6_click)
btn6.grid(row=2, column=1)
btn7 = Button(win, text='3', width=4, height=1, command=btn7_click)
btn7.grid(row=2, column=2)
btn8 = Button(win, text='+', width=4, height=1, command=btn8_click)
btn8.grid(row=2, column=3)
btn9 = Button(win, text='4', width=4, height=1, command=btn9_click)
btn9.grid(row=3, column=0)
btn10 = Button(win, text='5', width=4, height=1, command=btn10_click)
btn10.grid(row=3, column=1)
btn11 = Button(win, text='6', width=4, height=1, command=btn11_click)
btn11.grid(row=3, column=2)
btn12 = Button(win, text='-', width=4, height=1, command=btn12_click)
btn12.grid(row=3, column=3)
btn13 = Button(win, text='7', width=4, height=1, command=btn13_click)
btn13.grid(row=4, column=0)
btn14 = Button(win, text='8', width=4, height=1, command=btn14_click)
btn14.grid(row=4, column=1)
btn15 = Button(win, text='9', width=4, height=1, command=btn15_click)
btn15.grid(row=4, column=2)
btn16 = Button(win, text='x', width=4, height=1, command=btn16_click)
btn16.grid(row=4, column=3)
btn17 = Button(win, text='00', width=4, height=1, command=btn17_click)
btn17.grid(row=5, column=0)
btn18 = Button(win, text='0', width=4, height=1, command=btn18_click)
btn18.grid(row=5, column=1)
btn19 = Button(win, text='.', width=4, height=1, command=btn19_click)
btn19.grid(row=5, column=2)
btn20 = Button(win, text='÷', width=4, height=1, command=btn20_click)
btn20.grid(row=5, column=3)

win.mainloop()

        但是系统还有一些细节部分没有完全处理掉,如果哪位聪明地小伙伴发现了bug,可以私信我和我一起讨论哟!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值