首页 > 其他分享 >基于Tkinter库设计的计算器

基于Tkinter库设计的计算器

时间:2023-10-14 13:22:21浏览次数:36  
标签:bd 基于 Tkinter height place tk 计算器 font root

基于Tkinter库设计的计算器

Tkinter库:Tkinter是Python的标准GUI库。使用Tkinter库可以可以快速的创建GUI应用程序。

1、创建计算器的窗口

复制代码
# 创建一个窗口
root = tk.Tk()
root.minsize(300, 480)
root.title('计算器')
result1 = tk.StringVar()
result1.set(0)
result2 = tk.StringVar()
result2.set('')
复制代码

2、设置屏幕显示

复制代码
#算式显示
screen2 = tk.Label(root, font=('微软雅黑', 20), bg='#EEE9E9', bd='9', fg='black', textvariable=result1, anchor='se')
screen2.place(width=300, height=170)
#结果显示
screen1 = tk.Label(root, font=('微软雅黑', 30), bg='#0000FF', bd='9', fg='black', textvariable=result2, anchor='se')
screen1.place(width=300, height=110)
复制代码

3、申明并定义点击函数

# 操作函数
lists = []
ispressfunction = False
ispressnumber = False
ispressequal = False

  1、#点击数字定义

复制代码
def pressnumber(num):
    global lists
    global ispressfunction
    global ispressequal

    if ispressequal == False:
        pass
    else:
        ispressequal = False
        result1.set('')

    if ispressfunction == False:
        pass
    else:
        result1.set(0)
        ispressfunction = False

    oldnum = result1.get()
    if oldnum == '0':
        result1.set(num)
    else:
        newnum = oldnum + num
        result1.set(newnum)
复制代码

  2、# 点击功能键定义

复制代码
def pressfunction(stringlist):
    global lists
    global ispressfunction
    global ispressequal

    num = result1.get()
    lists.append(num)
    shizi = ''
    ispressfunction = True

    if stringlist in ['-', '+', '/', '*']:

        lists.append(stringlist)
        for each in lists:
            shizi += each
        result2.set(shizi)

    if stringlist == '1/2':
            list1 = []
            numnow = result1.get()
            list1.append("sqrt(" + numnow + ")")
            sqrt1 = m.pow(int(numnow),1/2)
            result2.set(list1)
            result1.set(sqrt1)

    if stringlist == 'C':
        lists.pop()
        result1.set(0)
        ispressfunction = False
        ispressnumber = False
        ispressequal = False

    if stringlist == 'CE':
        lists.clear()
        result1.set(0)
        result2.set('')

    if stringlist == '←':
        a = num[0:-1]
        lists.pop()
        result1.set(a)
复制代码

4、定义按键位置

复制代码
# 第一行
buttonce = tk.Button(root, font=('微软雅黑', 20), text='1/2', bd='0.5', fg='blue', command=lambda: pressfunction('1/2'))
buttonce.place(x=140, y=170, width=60, height=50)

buttonc = tk.Button(root, font=('微软雅黑', 20), text='C', bd='0.5', bg='orange', fg='black', command=lambda: pressfunction('C'))
buttonc.place(x=70, y=170, width=60, height=50)

buttondelete = tk.Button(root, font=('微软雅黑', 20), text='←', bd='0.5',bg='orange', fg='black',command=lambda: pressfunction('←'))
buttondelete.place(x=0, y=170, width=60, height=50)

buttondiv = tk.Button(root, font=('微软雅黑', 20), text='÷', bd='0.5', fg='blue', command=lambda: pressfunction('/'))
buttondiv.place(x=220, y=170, width=70, height=50)
# 第二行
button7 = tk.Button(root, font=('微软雅黑', 20), text='7', bd='0.5', bg='white', command=lambda: pressnumber('7'))
button7.place(x=0, y=230, width=60, height=50)

button8 = tk.Button(root, font=('微软雅黑', 20), text='8', bd='0.5', bg='white', command=lambda: pressnumber('8'))
button8.place(x=70, y=230, width=60, height=50)

button9 = tk.Button(root, font=('微软雅黑', 20), text='9', bd='0.5', bg='white', command=lambda: pressnumber('9'))
button9.place(x=140, y=230, width=60, height=50)

buttonmul = tk.Button(root, font=('微软雅黑', 20), text='×', bd='0.5', fg='blue', command=lambda: pressfunction('*'))
buttonmul.place(x=220, y=230, width=70, height=50)
# 第三行
button4 = tk.Button(root, font=('微软雅黑', 20), text='4', bd='0.5', bg='white', command=lambda: pressnumber('4'))
button4.place(x=0, y=290, width=60, height=50)

button5 = tk.Button(root, font=('微软雅黑', 20), text='5', bd='0.5', bg='white', command=lambda: pressnumber('5'))
button5.place(x=70, y=290, width=60, height=50)

button6 = tk.Button(root, font=("微软雅黑", 20), text='6', bd='0.5', bg='white', command=lambda: pressnumber('6'))
button6.place(x=140, y=290, width=60, height=50)

buttonsub = tk.Button(root, font=('微软雅黑', 20), text='-', bd='0.5', fg='blue', command=lambda: pressfunction('-'))
buttonsub.place(x=220, y=290, width=70, height=50)
# 第四行
button1 = tk.Button(root, font=('微软雅黑', 20), text='1', bd='0.5', bg='white', command=lambda: pressnumber('1'))
button1.place(x=0, y=350, width=60, height=50)

button2 = tk.Button(root, font=('微软雅黑', 20), text='2', bd='0.5', bg='white', command=lambda: pressnumber('2'))
button2.place(x=70, y=350, width=60, height=50)

button3 = tk.Button(root, font=("微软雅黑", 20), text='3', bd='0.5', bg='white', command=lambda: pressnumber('3'))
button3.place(x=140, y=350, width=60, height=50)

buttonadd = tk.Button(root, font=('微软雅黑', 20), text='+', bd='0.5', fg='blue', command=lambda: pressfunction('+'))
buttonadd.place(x=220, y=350, width=70, height=50)
# 第五行
buttoninverse = tk.Button(root, font=("微软雅黑", 20), text='CE', bg='orange', bd='0.5', fg='black',command=lambda: pressfunction('CE'))
buttoninverse.place(x=0, y=410, width=60, height=50)

button0 = tk.Button(root, font=("微软雅黑", 20), text='0', bd='0.5', bg='white', command=lambda: pressnumber('0'))
button0.place(x=70, y=410, width=60, height=50)

buttonpoint = tk.Button(root, font=("微软雅黑", 20), text='.', bd='0.5', fg='blue', command=lambda: pressnumber('.'))
buttonpoint.place(x=140, y=410, width=60, height=50)

buttonvalue = tk.Button(root, font=("微软雅黑", 20), text='=', bd='0.5', bg='orange', fg='black',command=lambda: pressequal())
buttonvalue.place(x=220, y=410, width=70, height=50)
复制代码

此为计算器界面。

5、#申明并定义结果函数

复制代码
def pressequal():
    global lists
    global ispressfunction
    global ispressequal

    ispressequal = True
    curnum = result1.get()
    lists.append(curnum)
    computrStr = ''.join(lists)
    endnum = eval(computrStr)
    result1.set(endnum)
    result2.set(computrStr)
    lists.clear()

root.mainloop()
复制代码

 

  点击按键后,白色显示窗会显示相应的数字和操作符号,当点击下一按键时,前一次按键会显示在蓝色显示窗上,点击“=”按键后,蓝色显示窗展示操作算式,白色显示窗会显示算式的结果。

运算结果

加:

减:

乘:

除:

求平方根:

以下是关键代码:

复制代码
import tkinter as tk  # 导入tkinter模块
import math as m

# 创建一个窗口
root = tk.Tk()
root.minsize(300, 480)
root.title('计算器')
result1 = tk.StringVar()
result1.set(0)
result2 = tk.StringVar()
result2.set('')

# 显示屏幕
#算式显示
screen2 = tk.Label(root, font=('微软雅黑', 20), bg='#EEE9E9', bd='9', fg='black', textvariable=result1, anchor='se')
screen2.place(width=300, height=170)
#结果显示
screen1 = tk.Label(root, font=('微软雅黑', 30), bg='#0000FF', bd='9', fg='black', textvariable=result2, anchor='se')
screen1.place(width=300, height=110)

# 第一行
buttonce = tk.Button(root, font=('微软雅黑', 20), text='1/2', bd='0.5', fg='blue', command=lambda: pressfunction('1/2'))
buttonce.place(x=140, y=170, width=60, height=50)

buttonc = tk.Button(root, font=('微软雅黑', 20), text='C', bd='0.5', bg='orange', fg='black', command=lambda: pressfunction('C'))
buttonc.place(x=70, y=170, width=60, height=50)

buttondelete = tk.Button(root, font=('微软雅黑', 20), text='←', bd='0.5',bg='orange', fg='black',command=lambda: pressfunction('←'))
buttondelete.place(x=0, y=170, width=60, height=50)

buttondiv = tk.Button(root, font=('微软雅黑', 20), text='÷', bd='0.5', fg='blue', command=lambda: pressfunction('/'))
buttondiv.place(x=220, y=170, width=70, height=50)
# 第二行
button7 = tk.Button(root, font=('微软雅黑', 20), text='7', bd='0.5', bg='white', command=lambda: pressnumber('7'))
button7.place(x=0, y=230, width=60, height=50)

button8 = tk.Button(root, font=('微软雅黑', 20), text='8', bd='0.5', bg='white', command=lambda: pressnumber('8'))
button8.place(x=70, y=230, width=60, height=50)

button9 = tk.Button(root, font=('微软雅黑', 20), text='9', bd='0.5', bg='white', command=lambda: pressnumber('9'))
button9.place(x=140, y=230, width=60, height=50)

buttonmul = tk.Button(root, font=('微软雅黑', 20), text='×', bd='0.5', fg='blue', command=lambda: pressfunction('*'))
buttonmul.place(x=220, y=230, width=70, height=50)
# 第三行
button4 = tk.Button(root, font=('微软雅黑', 20), text='4', bd='0.5', bg='white', command=lambda: pressnumber('4'))
button4.place(x=0, y=290, width=60, height=50)

button5 = tk.Button(root, font=('微软雅黑', 20), text='5', bd='0.5', bg='white', command=lambda: pressnumber('5'))
button5.place(x=70, y=290, width=60, height=50)

button6 = tk.Button(root, font=("微软雅黑", 20), text='6', bd='0.5', bg='white', command=lambda: pressnumber('6'))
button6.place(x=140, y=290, width=60, height=50)

buttonsub = tk.Button(root, font=('微软雅黑', 20), text='-', bd='0.5', fg='blue', command=lambda: pressfunction('-'))
buttonsub.place(x=220, y=290, width=70, height=50)
# 第四行
button1 = tk.Button(root, font=('微软雅黑', 20), text='1', bd='0.5', bg='white', command=lambda: pressnumber('1'))
button1.place(x=0, y=350, width=60, height=50)

button2 = tk.Button(root, font=('微软雅黑', 20), text='2', bd='0.5', bg='white', command=lambda: pressnumber('2'))
button2.place(x=70, y=350, width=60, height=50)

button3 = tk.Button(root, font=("微软雅黑", 20), text='3', bd='0.5', bg='white', command=lambda: pressnumber('3'))
button3.place(x=140, y=350, width=60, height=50)

buttonadd = tk.Button(root, font=('微软雅黑', 20), text='+', bd='0.5', fg='blue', command=lambda: pressfunction('+'))
buttonadd.place(x=220, y=350, width=70, height=50)
# 第五行
buttoninverse = tk.Button(root, font=("微软雅黑", 20), text='CE', bg='orange', bd='0.5', fg='black',command=lambda: pressfunction('CE'))
buttoninverse.place(x=0, y=410, width=60, height=50)

button0 = tk.Button(root, font=("微软雅黑", 20), text='0', bd='0.5', bg='white', command=lambda: pressnumber('0'))
button0.place(x=70, y=410, width=60, height=50)

buttonpoint = tk.Button(root, font=("微软雅黑", 20), text='.', bd='0.5', fg='blue', command=lambda: pressnumber('.'))
buttonpoint.place(x=140, y=410, width=60, height=50)

buttonvalue = tk.Button(root, font=("微软雅黑", 20), text='=', bd='0.5', bg='orange', fg='black',command=lambda: pressequal())
buttonvalue.place(x=220, y=410, width=70, height=50)

# 操作函数
lists = []
ispressfunction = False
ispressnumber = False
ispressequal = False

# 数字按键定义
def pressnumber(num):
    global lists
    global ispressfunction
    global ispressequal

    if ispressequal == False:
        pass
    else:
        ispressequal = False
        result1.set('')

    if ispressfunction == False:
        pass
    else:
        result1.set(0)
        ispressfunction = False

    oldnum = result1.get()
    if oldnum == '0':
        result1.set(num)
    else:
        newnum = oldnum + num
        result1.set(newnum)


# 功能键定义
def pressfunction(stringlist):
    global lists
    global ispressfunction
    global ispressequal

    num = result1.get()
    lists.append(num)
    shizi = ''
    ispressfunction = True

    if stringlist in ['-', '+', '/', '*']:

        lists.append(stringlist)
        for each in lists:
            shizi += each
        result2.set(shizi)

    if stringlist == '1/2':
            list1 = []
            numnow = result1.get()
            list1.append("sqrt(" + numnow + ")")
            sqrt1 = m.pow(int(numnow),1/2)
            result2.set(list1)
            result1.set(sqrt1)


    if stringlist == 'C':
        lists.pop()
        result1.set(0)
        ispressfunction = False
        ispressnumber = False
        ispressequal = False

    if stringlist == 'CE':
        lists.clear()
        result1.set(0)
        result2.set('')

    if stringlist == '←':
        a = num[0:-1]
        lists.pop()
        result1.set(a)


# 结果
def pressequal():
    global lists
    global ispressfunction
    global ispressequal

    ispressequal = True
    curnum = result1.get()
    lists.append(curnum)
    computrStr = ''.join(lists)
    endnum = eval(computrStr)
    result1.set(endnum)
    result2.set(computrStr)
    lists.clear()

root.mainloop()

标签:bd,基于,Tkinter,height,place,tk,计算器,font,root
From: https://www.cnblogs.com/lzufe-rzx/p/17764052.html

相关文章

  • 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(8) -- 使用Co
    在我们WPF应用端的时候,和WInform开发或者Vue前端开发一样,有时候也需要对内容进行转义处理,如把一些0,1数值转换为具体含义的文本信息,或者把一些布尔变量转换为是否等,都是常见的转换处理,本篇随笔介绍在WPF应用端对内容使用Converter类实现内容的转义处理的操作。1、使用Converter实......
  • 基于Win 自带的.NET FrameWork平台,使用文本文件编写C#代码,命令行编译以及引用第三方库
    转载自https://www.infoq.cn/article/2015/12/visual-studio-windows 不用VisualStudio也能开发.NETWindows应用邵思华2015-12-29本文字数:2915字阅读完需:约10分钟对于.NET应用的开发人员而言,以VisualStudio(简称VS)作为首选的开发工具应当是一种最......
  • 可实现加、减、乘、除、开平方的计算器软件的实验设计
    1、思路代码:#include<stdio.h>#include<math.h>//牛顿迭代法计算平方根doublesqrt_newton(doublex){doubleguess=x/2.0;//初始猜测值为x的一半doubledelta=0.000001;//误差范围while(fabs(guess*guess-x)>delta){guess=(guess+x/guess)/2.0;......
  • 简易计算器的设计
    [实验目的]1.掌握软件开发的基本流程2.掌握软件设计和开发的基本工具3.理解集成软件开发环境在软件开发过程中的作用[实验内容]1.设计一个可实现加、减、乘、除功能的计算器软件2.使用牛顿迭代法完成算术开方的运算3.将自己编写的算术开方功能集成到计算器程序中,使计算器软......
  • 三防手持终端安卓主板,基于联发科MTK平台的主板方案
    三防手持终端方案基于MT6765安卓主板开发。它采用联发科12nm八核MT6765处理器,提供4G+64GB的内存配置(也可选配6GB+256GB)。该终端搭载最新的安卓系统,即Android10.0,具备高亮显示屏、高清摄像头、NFC、3A快速充电、1D/2D扫描(可选配)、高精度定位(可选配)、公网对讲、GPS导航、......
  • 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(7) -- 图标列
    我们在WPF应用端的界面中,使用lepoco/wpfui来做主要的入口框架,这个项目它的菜单内置了不少图标,我们需要在动态菜单的配置中,使用它作为图标的展示处理,本篇随笔介绍如何基于图标枚举集合进行图标的展示和选择处理。并扩展到Font-Awesome-WPF的处理进行展示和选择。1、lepoco/wpfui......
  • 基于.Net 的 AvaloniUI 多媒体播放器方案汇总
    基于.Net的AvaloniUI多媒体播放器方案汇总摘要随着国产化的推进,相信.Net的桌面端的小伙伴的可能已经有感受到了。为了让.Net的桌面框架能够跨桌面平台,首选的就是Avalona-UI。为了让AvaloniaUI能够跨多个平台播放视频,这里测试主要播放视频形式是使用RTSP。所以,在这篇博文中......
  • 基于 EventBridge 轻松搭建消息集成应用
    作者:昶风前言本篇文章主要介绍基于阿里云EventBridge的消息集成能力,结合目前消息产品的需求热点,从能力范围到场景实战,对EventBridge的消息集成解决方案进行了概要的介绍。从消息现状谈起消息队列作为应用解耦,流量削峰填谷的有效工具,早已被开发者广泛应用于各类分布式服务......
  • 基于知识图谱建模、全文检索的智能知识管理库(源码)
    一、项目介绍一款全源码,可二开,可基于云部署、私有部署的企业级知识库云平台,一款让企业知识变为实打实的数字财富的系统,应用在需要进行文档整理、分类、归集、检索、分析的场景。知识图谱提供了一种从海量文本和图像中抽取结构化知识的手段,让知识获取更便捷、知识整理更简单、知......
  • 计算器软件开发
    用Java实现计算器软件设计Java编写个人计算器软件一、所需要实现的功能1.该计算器需要实现加、减、乘、除、开平方功能。2.需要有一个运行的UI界面,可以和电脑自带的计算器相比较。该界面要有一个文本输入框,用来显示输入的表达式;若干个按钮,用来用来显示数字以及操作符;当点击......