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

liftword4个月前 (01-20)技术文章24

大家好,今天我要带大家使用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的图形化编程有一个更直观的了解。如果你觉得这篇文章对你有帮助,欢迎分享给更多的朋友!


相关文章

开发图形界面Tkinter、wxPython、PyQt、PySide选哪一个?

  学习Python,就不得不提一下Python的图形界面开发,如果只用命令行写程序,用户体验不太好,难以胜任复杂的人机交互场景。Python的图形界面开发库常用的有:Tkinter、wxPython...

Python | graphics、tkinter 画回归线

前言写 Python 题时遇到一道绘制回归线的题目。要求点击数据点输入进行绘制,我用 graphics 完成了。但是,这样输入并不精确,加上高中受到线性回归方程那庞大计算量的折磨,于是想写个能输数据的...

Python可视化教程

一、基础篇利用python实现可视化的三个步骤:1、确定问题,选择图形2、转换数据,应用函数3、参数设置,一目了然01首先,要知道我们用哪些库来画图?matplotlibpython中最基本的作图库就...

如何在Python中创建Gif动图?(动图数据可视化基础教学)

随着数据以前所未有的速度不断膨胀,数据分析师们往往被要求对数据进行分析并加以理解。一旦发生这种情况,就需要更加有效的方式来传达数据。然而,传达数据分析的结果往往十分棘手。为了更有效地交流,数据可视化是...

python下又一款漂亮超炫酷的动态数据可视化工具——可动态交互

python下有很多漂亮的数据可视化库,例如 Matplotlib、Seaborn、Bokeh、Plotly、Pyecharts等等,我们直接使用这些第三方库来进行漂亮的数据可视化操作。虽然这些库都可...