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

liftword6个月前 (01-20)技术文章40

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


相关文章

Python超炫技巧!Tkinter神级应用打造震撼视觉的图形化界面设计

目录一、图形化界面设计的基本理解二、窗体控件布局2.1.根窗体显示实例2.2. tkinter 常用控件2.2.1 控件的共同属性2.3 控件布局2.3.1 pack()方法2.3.2 grid()方...

Python | graphics、tkinter 画回归线

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

【Python可视化系列】一文彻底教会你绘制美观的折线图(源码)

一、前言折线图是一种常用的可视化图表,可以清晰地展示数据随时间或其他连续变量的变化趋势,通过连接数据点,可以观察到数据的上升、下降、波动等变化趋势,帮助人们更直观地理解数据的变化规律。二、基本折线图2...

Python可视化很简单,一文学会绘制柱状图、条形图和直方图

matplotlib库作为Python数据化可视化的最经典和最常用库,掌握了它就相当于学会了Python的数据化可视化,通过前几次呢,咱们已经讨论了使用matplotlib库中的图表组成元素的几个重要...

干货,Python地图可视化三大秘密武器

Python地图可视化库有大家熟知的pyecharts、plotly、folium,还有稍低调的bokeh、basemap、geopandas,也是地图可视化利器。首先介绍下bokehbokeh擅长制...