百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

用Python制作一个带图形界面的计算器

off999 2025-08-01 20:09 25 浏览 0 评论

大家好,今天我要带大家使用Python制作一个具有图形界面的计算器应用程序。这个项目不仅可以帮助你巩固Python编程基础,还可以让你初步体验图形化编程的乐趣。我们将使用Python的tkinter库,它是一个简单易用的GUI库,非常适合用来开发小型的桌面应用。

一、项目简介

在我们日常生活中,计算器是一个不可或缺的工具。通过本项目,你将学会如何用Python创建一个类似Windows系统自带计算器的应用程序,实现基本的加、减、乘、除等运算功能。最终效果将是一个带有数字按钮、运算符按钮和显示屏的完整计算器。

二、准备工作

首先,你需要确保你的Python环境中已经安装了tkinter库。幸运的是,如果你已经安装了Python,那么tkinter库一般会随Python一起安装,无需额外操作。

接下来,我们将一步步构建这个计算器。

三、构建图形界面

  1. 创建主窗口和输入框

我们首先需要创建一个主窗口,并在窗口中放置一个用于显示计算结果的输入框。代码如下:

import tkinter as tk

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

# 输入框
entry = tk.Entry(root, width=16, font=('Arial', 24), borderwidth=2, relief="solid")
entry.grid(row=0, column=0, columnspan=4)

在这段代码中,tk.Tk()用来创建一个主窗口,tk.Entry()则创建了一个输入框,设置了字体大小和边框样式。最后,通过grid方法将输入框放置在网格的第一行,并跨越四列。

  1. 实现按钮点击事件

接下来,我们定义一些函数来处理按钮的点击事件。我们需要处理三种类型的按钮:数字按钮、运算符按钮和功能按钮(如等号和清除)。

# 全局变量来存储计算器的状态
current = ""
operator = ""

# 按钮点击事件
def button_click(number):
    global current
    current += str(number)
    entry.delete(0, tk.END)
    entry.insert(tk.END, current)

def button_clear():
    global current, operator
    current = ""
    operator = ""
    entry.delete(0, tk.END)

def button_equal():
    global current, operator
    try:
        result = str(eval(current))
        entry.delete(0, tk.END)
        entry.insert(tk.END, result)
        current = result
    except:
        entry.delete(0, tk.END)
        entry.insert(tk.END, "Error")
        current = ""

在这里,button_click函数用来处理数字和运算符按钮的点击,button_clear清除输入框的内容,button_equal则用于计算输入的表达式并显示结果。

  1. 创建和布局按钮

计算器的每个按钮(数字键和运算符键)都需要创建,并通过grid方法放置在窗口中的合适位置。

# 创建按钮
button_1 = tk.Button(root, text="1", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(1))
button_2 = tk.Button(root, text="2", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(2))
button_3 = tk.Button(root, text="3", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(3))
button_4 = tk.Button(root, text="4", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(4))
button_5 = tk.Button(root, text="5", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(5))
button_6 = tk.Button(root, text="6", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(6))
button_7 = tk.Button(root, text="7", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(7))
button_8 = tk.Button(root, text="8", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(8))
button_9 = tk.Button(root, text="9", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(9))
button_0 = tk.Button(root, text="0", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(0))

button_add = tk.Button(root, text="+", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click('+'))
button_subtract = tk.Button(root, text="-", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click('-'))
button_multiply = tk.Button(root, text="*", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click('*'))
button_divide = tk.Button(root, text="/", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click('/'))
button_equal = tk.Button(root, text="=", padx=20, pady=20, font=('Arial', 18), command=button_equal)
button_clear = tk.Button(root, text="C", padx=20, pady=20, font=('Arial', 18), command=button_clear)

# 布局按钮
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

button_0.grid(row=4, column=0)

button_add.grid(row=1, column=3)
button_subtract.grid(row=2, column=3)
button_multiply.grid(row=3, column=3)
button_divide.grid(row=4, column=3)

button_equal.grid(row=4, column=2)
button_clear.grid(row=4, column=1)

上面的代码定义了数字按钮(0-9)和运算符按钮(+,-,*,/),并且通过grid方法将它们放置在不同的行和列中,构成了一个标准的计算器布局。

  1. 启动应用程序

最后一步,我们通过root.mainloop()启动主循环,运行我们的应用程序。

# 启动主循环
root.mainloop()

这行代码会让我们的程序进入一个无限循环状态,等待用户的操作。

四、完整代码

下面是我们构建的完整代码:

import tkinter as tk

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

# 输入框
entry = tk.Entry(root, width=16, font=('Arial', 24), borderwidth=2, relief="solid")
entry.grid(row=0, column=0, columnspan=4)

# 全局变量来存储计算器的状态
current = ""
operator = ""

# 按钮点击事件
def button_click(number):
    global current
    current += str(number)
    entry.delete(0, tk.END)
    entry.insert(tk.END, current)

def button_clear():
    global current, operator
    current = ""
    operator = ""
    entry.delete(0, tk.END)

def button_equal():
    global current, operator
    try:
        result = str(eval(current))
        entry.delete(0, tk.END)
        entry.insert(tk.END, result)
        current = result
    except:
        entry.delete(0, tk.END)
        entry.insert(tk.END, "Error")
        current = ""

# 创建按钮
button_1 = tk.Button(root, text="1", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(1))
button_2 = tk.Button(root, text="2", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(2))
button_3 = tk.Button(root, text="3", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(3))
button_4 = tk.Button(root, text="4", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(4))
button_5 = tk.Button(root, text="5", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(5))
button_6 = tk.Button(root, text="6", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(6))
button_7 = tk.Button(root, text="7", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(7))
button_8 = tk.Button(root, text="8", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(8))
button_9 = tk.Button(root, text="9", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(9))
button_0 = tk.Button(root, text="0", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click(0))

button_add = tk.Button(root, text="+", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click('+'))
button_subtract = tk.Button(root, text="-", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click('-'))
button_multiply = tk.Button(root, text="*", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click('*'))
button_divide = tk.Button(root, text="/", padx=20, pady=20, font=('Arial', 18), command=lambda: button_click('/'))
button_equal = tk.Button(root, text="=", padx=20, pady=20, font=('Arial', 18), command=button_equal)
button_clear = tk.Button(root, text="C", padx=20, pady=20, font=('Arial', 18), command=button_clear)

# 布局按钮
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

button_0.grid(row=4, column=0)

button_add.grid(row=1, column=3)
button_subtract.grid(row=2, column=3)
button_multiply.grid(row=3, column=3)
button_divide.grid(row=4, column=3)

button_equal.grid(row=4, column=2)
button_clear.grid(row=4, column=1)

# 启动主循环
root.mainloop()

五、总结

这个Python计算器项目展示了如何使用tkinter库创建一个基本的GUI应用程序。虽然这个计算器功能简单,但它涵盖了Python编程的许多基本概念,例如事件处理、函数定义、全局变量的使用,以及图形界面的布局管理。通过这个项目,相信大家能够对Python的图形化编程有一个更直观的了解。如果你觉得这篇文章对你有帮助,欢迎分享给更多的朋友!


相关推荐

大文件传不动?WinRAR/7-Zip 入门到高手,这 5 个技巧让你效率翻倍

“这200张照片怎么传给女儿?微信发不了,邮箱附件又超限……”62岁的张阿姨对着电脑犯愁时,儿子只用了3分钟就把照片压缩成一个文件,还教她:“以后用压缩软件,比打包行李还方便!”职场人更懂这...

电脑解压缩软件推荐——7-Zip:免费、高效、简洁的文件管理神器

在日常工作中,我们经常需要处理压缩文件。无论是下载软件包、接收文件,还是存储大量数据,压缩和解压缩文件都成为了我们日常操作的一部分。而说到压缩解压软件,7-Zip绝对是一个不可忽视的名字。今天,我就来...

设置了加密密码zip文件要如何打开?这几个方法可以试试~

Zip是一种常见的压缩格式文件,文件还可以设置密码保护。那设置了密码的Zip文件要如何打开呢?不清楚的小伙伴一起来看看吧。当我们知道密码想要打开带密码的Zip文件,我们需要用到适用于Zip格式的解压缩...

大文件想要传输成功,怎么把ZIP文件分卷压缩

不知道各位小伙伴有没有这样的烦恼,发送很大很大的压缩包会受到限制,为此,想要在压缩过程中将文件拆分为几个压缩包并且同时为所有压缩包设置加密应该如何设置?方法一:使用7-Zip免费且强大的文件管理工具7...

高效处理 RAR 分卷压缩包:合并解压操作全攻略

在文件传输和存储过程中,当遇到大文件时,我们常常会使用分卷压缩的方式将其拆分成多个较小的压缩包,方便存储和传输。RAR作为一种常见的压缩格式,分卷压缩包的使用频率也很高。但很多人在拿到RAR分卷...

2个方法教你如何删除ZIP压缩包密码

zip压缩包设置了加密密码,每次解压文件都需要输入密码才能够顺利解压出文件,当压缩包文件不再需要加密的时候,大家肯定想删除压缩包密码,或是忘记了压缩包密码,想要通过删除操作将压缩包密码删除,就能够顺利...

速转!漏洞预警丨压缩软件Winrar目录穿越漏洞

WinRAR是一款功能强大的压缩包管理器,它是档案工具RAR在Windows环境下的图形界面。该软件可用于备份数据,缩减电子邮件附件的大小,解压缩从Internet上下载的RAR、ZIP及其它类...

文件解压方法和工具分享_文件解压工具下载

压缩文件减少文件大小,降低文件失效的概率,总得来说好处很多。所以很多文件我们下载下来都是压缩软件,很多小伙伴不知道怎么解压,或者不知道什么工具更好,所以今天做了文件解压方法和工具的分享给大家。一、解压...

[python]《Python编程快速上手:让繁琐工作自动化》学习笔记3

1.组织文件笔记(第9章)(代码下载)1.1文件与文件路径通过importshutil调用shutil模块操作目录,shutil模块能够在Python程序中实现文件复制、移动、改名和删除;同时...

Python内置tarfile模块:读写 tar 归档文件详解

一、学习目标1.1学习目标掌握Python内置模块tarfile的核心功能,包括:理解tar归档文件的原理与常见压缩格式(gzip/bz2/lzma)掌握tar文件的读写操作(创建、解压、查看、过滤...

使用python展开tar包_python拓展

类Unix的系统,打包文件经常使用的就是tar包,结合zip工具,可以方便的打包并解压。在python的标准库里面有tarfile库,可以方便实现生成了展开tar包。使用这个库最大的好处,可能就在于不...

银狐钓鱼再升级:白文件脚本化实现GO语言后门持久驻留

近期,火绒威胁情报中心监测到一批相对更为活跃的“银狐”系列变种木马。火绒安全工程师第一时间获取样本并进行分析。分析发现,该样本通过阿里云存储桶下发恶意文件,采用AppDomainManager进行白利...

ZIP文件怎么打开?2个简单方法教你轻松搞定!

在日常工作和生活中,我们经常会遇到各种压缩文件,其中最常见的格式之一就是ZIP。ZIP文件通过压缩数据来减少文件大小,方便我们进行存储和传输。然而,对于初学者来说,如何打开ZIP文件可能会成为一个小小...

Ubuntu—解压多个zip压缩文件.zip .z01 .z02

方法将所有zip文件放在同一目录中:zip_file.z01,zip_file.z02,zip_file.z03,...,zip_file.zip。在Zip3.0版本及以上,使用下列命令:将所有zi...

如何使用7-Zip对文件进行加密压缩

7-Zip是一款开源的文件归档工具,支持多种压缩格式,并提供了对压缩文件进行加密的功能。使用7-Zip可以轻松创建和解压.7z、.zip等格式的压缩文件,并且可以通过设置密码来保护压缩包中的...

取消回复欢迎 发表评论: