python编写简易计算器并打包成.exe程序

import re  # 导入re模块,用于分割字符串
import tkinter  # 使用tkinter模块设计图形用户界面,使用messagebox模块实现弹窗报错
import tkinter.messagebox
from pywin.scintilla.formatter import operators

window = tkinter.Tk()  # 创建窗口并赋值给变量window
window.geometry('480x400+400+100')  # 指定窗口大小,宽度:480,高度:5400,x坐标:400,y坐标:100
window.resizable(False, False)  # 设置窗口不可调整大小
window.configure(bg='silver')  # 设置背景色
window.attributes("-alpha", 0.95)  # 设置透明度
window.title('计算器')  # 设置窗口标题


def ButtonOperation(m):
    """按钮操作"""
    theme = themeVar.get()  # 使用get函数获取文本框中的字符串,赋值给变量theme
    if theme.startswith('.'):
        theme = '' + theme  # 在变量theme代表的字符串开头加上一个“0”,再重新赋值给变量theme
    if m in '0123456789':
        theme += m  # 0~9哪个按钮被单击,就在theme中添加哪个数字
    elif m == '.':
        # 将theme从+、-、*、/、%这些字符处分隔开,获取最后一个字符
        SegmentationPart = re.split(r'\+|-|\*|/|%|', theme)[-1]
        if '.' in SegmentationPart:
            tkinter.messagebox.showerror('错误', '重复出现小数点!')
            return
        else:
            theme += m
    elif m == 'Clear':
        theme = ''  # 清除文本框中的内容
        # 判断是否出现连续的运算符
    elif m in operators:
        if theme.endswith(operators):  # 如果是以运算符结尾,就报错
            tkinter.messagebox.showerror('错误', '不允许存在连续运算符!')
            return
        else:
            theme += m
    # 判断是否进行开方操作
    elif m == 'Sqrt':
        n = theme.split('.')
        # isdigit函数检测字符串是否只由数字组成,如果x中至少有一个字符串且x中的所有字符都是数字,则返回True
        if all(map(lambda x: x.isdigit(), n)):
            theme = eval(theme) ** 0.5
        else:
            tkinter.messagebox.showerror('错误', '算式错误!')
            return
    elif m == '=':
        try:
            theme = str(eval(theme))  # 调用eval函数,用字符串计算出结果
        except:
            tkinter.messagebox.showerror('错误', '算式错误!')
            return
    themeVar.set(theme)  # 用set函数获取计算结果,显示到文本框中


# 创建一个字符串变量对象
themeVar = tkinter.StringVar(window, '')
themeEntry = tkinter.Entry(window, textvariable=themeVar, font=('黑体', 18, 'bold', 'italic'), bg='LightGray',
                            justify='right', bd=3)
themeEntry['state'] = 'readonly'  # 设置文本框只读
themeEntry.place(x=10, y=10, width=460, height=40)  # 设置文本框在窗口中的x、y坐标位置,以及文本框的宽度和高度
# 放置按钮
figure = list('1234567890.') + ['Sqrt']
index = 0
for row in range(4):
    for col in range(3):
        a = figure[index]  # 按索引从列表中去除按钮文字
        index += 1
        m_figure = tkinter.Button(window, text=a, font=('黑体', 24, 'bold', 'italic'), justify='center',
                                  bg='LightSlateGray',
                                  activebackground='gray', activeforeground='white', bd=2.5,
                                  command=lambda x=a: ButtonOperation(x))
        m_figure.place(x=40 + col * 94, y=160 + row * 50, width=110, height=70)  # 设置按钮的位置和大小
operators = ('+', '-', '*', '/', '**', '//', '%')
for index, operator in enumerate(operators):
    m_operator = tkinter.Button(window, text=operator, font=('黑体', 18, 'bold', 'italic'), anchor='center',
                                justify='center', bg='LightSkyBlue4',
                                border=2.1, command=lambda x=operator: ButtonOperation(x))
    m_operator.place(x=340, y=160 + index * 30, width=110, height=40)
# 清除键C
# relief: flat, groove, raised, ridge, solid, or sunken
m_Clear = tkinter.Button(window, text='Clear', bg='LightSkyBlue3', font=('黑体', 18, 'bold', 'italic'), anchor='center',
                         border=2.1, command=lambda: ButtonOperation('Clear'))
# 设置按键的位置
m_Clear.place(x=40, y=100, width=260, height=40)
# 等号键
m_EqualSign = tkinter.Button(window, text='=', font=('黑体', 18, 'bold', 'italic'), bg='orange', anchor='center',
                             border=2.1, command=lambda: ButtonOperation('='))
# 设置”=“键的位置
m_EqualSign.place(x=340, y=100, width=110, height=40)
# 窗口持续显示
window.mainloop()

在这里插入图片描述

  • python打包成exe文件
    • 1.安装pyinstaller:pip install pyinstaller
      在这里插入图片描述
    • 2.文件路径不能超过18个字符,最好直接放在磁盘下(如E:)
    • 3.pyinstaller -F -w xx.py
      在这里插入图片描述
    • 4.打包成功后,文件目录下会出现几个新的文件:dist + build + xx.spec,exe文件就在dist文件夹中。
      在这里插入图片描述
    • 5.双击calculator.exe。
      在这里插入图片描述
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里逆天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值