tkinter制作一个简单计算器

这篇具有很好参考价值的文章主要介绍了tkinter制作一个简单计算器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

tkinter制作一个简单计算器

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

tkinter制作一个简单计算器

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

 系统代码:

#!/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,可以私信我和我一起讨论哟!文章来源地址https://www.toymoban.com/news/detail-512794.html

到了这里,关于tkinter制作一个简单计算器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 用python制作一个简易计算器

    这是一个用Python制作简单计算器的教程。你可以根据需要进行更多的改进,例如添加其他运算符或功能。 首先,我们需要创建一个简单的用户界面,用于显示计算器的按键和结果。在Python中,我们可以使用 tkinter 库来创建图形用户界面。创建一个新的Python文件,并将其命名为

    2024年02月11日
    浏览(34)
  • 制作一个简易的计算器app

    github项目地址:https://github.com/13008451162/AndroidMoblieCalculator 笔者的Ui制作的制作的比较麻烦仅供参考,在这里使用了多个LinearLayout对屏幕进行了划分。不建议大家这样做最好使用GridLayout会更加快捷简单 笔者大致划分是这样的: 使用了四个大框,在第四个大框里面有多个小框

    2024年02月15日
    浏览(33)
  • python界面开发案例:制作一个计算器软件

    前言 大家早好、午好、晚好吖 ❤ ~欢迎光临本文章 在我们手机上大家都有一个计算器,对吧 那它这功能是怎么实现的呢? 今天我们在电脑上来实现一个电脑端计算器界面~ 开发环境: Python 3.8 / 编译器 Pycharm 2021.2版本 / 编辑器 本文所有模块环境源码教程皆可点击文章下方

    2023年04月16日
    浏览(36)
  • Java中规模软件开发实训——简单计算器制作

    ✨ 博主: 命运之光 🌸 专栏: Python星辰秘典 🐳 专栏: web开发(html css js) ❤️ 专栏: Java经典程序设计 ☀️ 博主的其他文章: 点击进入博主的主页 前言: 在现代社会中,计算器是我们生活中不可或缺的工具之一。它们可以轻松地进行各种数值计算,从简单的加减乘除

    2024年02月12日
    浏览(37)
  • 用代码实现一个简单计算器

    作者主页: paper jie的博客_CSDN博客-C语言,算法详解领域博主 本文作者: 大家好,我是paper jie,感谢你阅读本文,欢迎一建三连哦。 本文录入于 《C语言》专栏,本专栏是针对于大学生,编程小白精心打造的。笔者用重金(时间和精力)打造,将C语言基础知识一网打尽,希望可

    2024年02月08日
    浏览(29)
  • 模拟实现一个简单的计算器

    2024年02月11日
    浏览(39)
  • 【Java GUI】用java实现简单计算器的制作 项目(一)

    目录 正文       项目前提      项目思路      项目实现 一:实现图形化界面的设计    二:关于按钮监听及文本框展示的实现         三:表达式运算的实现 四:完整代码 作者的话          ~掌握java基本语法         ~熟悉基础数据结构的运用         ~了解Java Swin

    2024年02月05日
    浏览(33)
  • Java——一个简单的计算器程序

      该代码是一个简单的计算器程序,使用了Java的图形化界面库Swing。具体分析如下: 导入必要的类和包: 代码中导入了用于创建图形界面的类和接口,以及其他必要的类。 定义Calculator类: 代码中定义了一个名为Calculator的类,继承了JFrame类,并实现了ActionListener接口。Calc

    2024年02月04日
    浏览(34)
  • 使用C语言构造一个简单计算器

    本节我们用小学生知识来制作一个简单的计算器,可以运算加,减,乘,除,以及余数的运算。 在这节代码中用到switch语句,因为要输入运算符,所以注意%c的对应 接下来上代码: 这里的话我们简单演示一下乘法的运算: 如果用其他的计算符号直接更改即可,这里使用双精

    2024年02月12日
    浏览(36)
  • Java设计一个简单的计算器程序

    计算器是一种常见的工具,用于进行基本数学运算。在计算机科学中,我们可以使用编程语言来模拟和实现一个计算器程序。本文将基于Java语言,设计并实现一个简单的计算器程序。 1. 需求分析 在设计计算器程序之前,我们需要明确程序的需求。本文设计的计算器程序应满

    2024年02月05日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包