用Python写个计算器:Python tkinter帮你轻松搞定!

2653 篇文章 2 订阅
2490 篇文章 14 订阅

2024软件测试面试刷题,这个小程序(永久刷题),靠它快速找到工作了!(刷题APP的天花板)_软件测试刷题小程序-CSDN博客文章浏览阅读3.4k次,点赞86次,收藏15次。你知不知道有这么一个软件测试面试的刷题小程序。里面包含了面试常问的软件测试基础题,web自动化测试、app自动化测试、接口测试、性能测试、自动化测试、安全测试及一些常问到的人力资源题目。最主要的是他还收集了像阿里、华为这样的大厂面试真题,还有互动交流板块……_软件测试刷题小程序​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502​编辑https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502icon-default.png?t=N7T8https://blog.csdn.net/AI_Green/article/details/134931243?spm=1001.2014.3001.5502

大家好!今天我们将用Python中的tkinter库来制作一个简单的计算器。tkinter是Python的标准GUI库,可以用来创建图形用户界面。在这篇文章中,我们将一步步介绍如何使用tkinter创建一个功能齐全的计算器。

什么是tkinter?

tkinter是Python的标准GUI(图形用户界面)库。它为我们提供了创建窗口和各种控件(如按钮、文本框、标签等)的工具。使用tkinter,我们可以很方便地创建交互式应用程序。

计算器的基本结构

一个计算器通常包括以下几个部分:

  • • 显示屏:用于显示输入的数字和计算结果。

  • • 按钮:用于输入数字和操作符,如“+”、“-”、“*”、“/”、“=”等。

  • • 逻辑:处理按钮点击事件并执行相应的计算。

详细示例

第一步:创建窗口和显示屏

首先,我们需要创建一个主窗口和一个显示屏。显示屏可以使用tkinter的Entry控件来实现。

import tkinter as tk

# 创建主窗口
root = tk.Tk()
root.title("计算器")

# 创建显示屏
display = tk.Entry(root, font=("Arial", 24), borderwidth=2, relief="ridge")
display.grid(row=0, column=0, columnspan=4)

root.mainloop()

第二步:创建按钮

接下来,我们将创建计算器的按钮。我们可以使用tkinter的Button控件来实现。为了方便管理,我们将按钮的标签和位置存储在一个列表中,然后用一个循环来创建按钮。

# 按钮的标签和位置
buttons = [
    '7', '8', '9', '/',
    '4', '5', '6', '*',
    '1', '2', '3', '-',
    '0', '.', '=', '+'
]

# 创建按钮并放置在窗口中
row = 1
col = 0
for button in buttons:
    tk.Button(root, text=button, font=("Arial", 18), command=lambda b=button: on_button_click(b)).grid(row=row, column=col, sticky="nsew")
    col += 1
    if col == 4:
        col = 0
        row += 1

# 设置行和列的权重,使按钮自动扩展以填充空间
for i in range(4):
    root.grid_columnconfigure(i, weight=1)
for i in range(5):
    root.grid_rowconfigure(i, weight=1)

第三步:按钮点击事件处理

为了处理按钮点击事件,我们需要定义一个回调函数。当按钮被点击时,该函数将被调用,并根据按钮的标签执行相应的操作。

def on_button_click(button):
    current_text = display.get()
    if button == '=':
        try:
            result = eval(current_text)
            display.delete(0, tk.END)
            display.insert(tk.END, str(result))
        except Exception as e:
            display.delete(0, tk.END)
            display.insert(tk.END, "错误")
    elif button == 'C':
        display.delete(0, tk.END)
    else:
        display.insert(tk.END, button)

完整代码

最后,我们将所有代码整合在一起,得到一个完整的计算器程序。

import tkinter as tk

# 创建主窗口
root = tk.Tk()
root.title("计算器")

# 创建显示屏
display = tk.Entry(root, font=("Arial", 24), borderwidth=2, relief="ridge")
display.grid(row=0, column=0, columnspan=4)

# 按钮的标签和位置
buttons = [
    '7', '8', '9', '/',
    '4', '5', '6', '*',
    '1', '2', '3', '-',
    '0', '.', '=', '+',
    'C'
]

# 创建按钮并放置在窗口中
row = 1
col = 0
for button in buttons:
    tk.Button(root, text=button, font=("Arial", 18), command=lambda b=button: on_button_click(b)).grid(row=row, column=col, sticky="nsew")
    col += 1
    if col == 4:
        col = 0
        row += 1

# 设置行和列的权重,使按钮自动扩展以填充空间
for i in range(4):
    root.grid_columnconfigure(i, weight=1)
for i in range(5):
    root.grid_rowconfigure(i, weight=1)

# 按钮点击事件处理
def on_button_click(button):
    current_text = display.get()
    if button == '=':
        try:
            result = eval(current_text)
            display.delete(0, tk.END)
            display.insert(tk.END, str(result))
        except Exception as e:
            display.delete(0, tk.END)
            display.insert(tk.END, "错误")
    elif button == 'C':
        display.delete(0, tk.END)
    else:
        display.insert(tk.END, button)

root.mainloop()

将 Python 脚本打包为 exe

安装 PyInstaller

首先,我们需要安装PyInstaller,这是一个可以将Python脚本打包成独立可执行文件的工具。你可以使用以下命令来安装它:

pip install pyinstaller

打包脚本

安装PyInstaller后,打开命令行并导航到你的Python脚本所在的目录。然后运行以下命令来打包你的脚本:

pyinstaller --onefile --windowed calculator.py
  • • --onefile 参数表示将所有文件打包成一个独立的EXE文件。

  • • --windowed 参数表示生成的EXE文件将不会弹出命令行窗口。

运行完这条命令后,你会在当前目录下看到一个 dist 文件夹,里面包含了生成的EXE文件。

在这篇文章中,我们使用Python的tkinter库创建了一个简单的计算器。通过这个项目,我们学习了如何创建窗口和控件,如何处理按钮点击事件,并将这些知识应用于一个实际的应用程序。希望你能通过这个项目更好地理解tkinter和Python编程。

行动吧,在路上总比一直观望的要好,未来的你肯定会感谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入群: 759968159,里面有各种测试开发资料和技术可以一起交流哦。

最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

​​​软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

在这里插入图片描述

在这里插入图片描述

  • 19
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值