Python制作一个简单的计算器(加减乘除)

技术

点击关注丨获取更多精彩内容

简易计算器

picture.image

写在前面

小编用python实现了一个简单的计算器,一起来看看吧~

需要环境:

  • pycharm

  • python

一、需求分析

1.1 功能分析

使用Python的Tkinter界面设计实现一个简单的计算器,主要功能按钮包括数字键、四则运算符、等于号和清除键,实现了简单的加减乘除运算。

1.2 性能分析

① 系统具有易操作性

在日常使用中,这个计算器应该可以满足大多数用户的需求,即使在旧计算机上也能够流畅运行。

② 系统具有高效性

这个计算器应用程序非常简单,没有太多的计算和数据处理。因此,它的性能应该是相对较好的,无论是在较老的电脑还是在较新的电脑上都能够快速响应和运行。

二、技术原理

这个计算器应用程序是基于Tkinter GUI工具集编写的一个Python应用程序,它使用不同类型的控件和布局管理器来创建图形用户界面并实现计算器的各种功能,通过用方法来处理用户在应用程序上的操作并显示相应的结果。

三、详细设计

3.1 导入tkinter库

① 程序设计


        
            

          import tkinter as tk
        
      

② 程序分析

在程序开头,通过"import tkinter as tk"导入tkinter库,以便使用tkinter库中的控件和方法。

3.2 定义全局变量

① 程序设计


          
t = ""
          
color1 = "skyblue"
          
color2 = "yellow"
          
color3 = "pink"
          
color4 = "red"
          
result = tk.StringVar()
      

② 程序分析

全局变量t用于表示计算器输入框中的内容。全局变量result是一个StringVar对象,用于实时更新输入框中的内容,全局变量color1-4分别代表不同的颜色。

3.3 定义添加函数

① 程序设计


          
def Add(num):
          
    global t
          
    t = t + str(num)
          
    result.set(t)
      

② 程序分析

Add函数用于将数字和运算符添加到输入框中。它获取单击的按钮上的值,将其转换为字符串并附加到全局变量t中,最后更新输入框中的内容result。

3.4 定义结果函数

① 程序设计


          
def Result():
          
    try:
          
        global t
          
        total = str(eval(t))
          
        result.set(total)
          
        t = ""
          
    except:
          
        result.set(" error ")
          
        t = ""
      

② 程序分析

Result函数用于计算表达式并将结果更新到输入框中。它首先尝试使用Python的内置eval函数计算从输入框中获取的表达式。如果计算成功,则将结果转换为字符串并设置为result,最后清空全局变量t。如果出现任何错误,设置result为" error "字符串并清空全局变量t。

3.5 定义清空函数

① 程序设计


          
def Clear():
          
    global t
          
    t = ""
          
    result.set("")
      

② 程序分析

Clear函数用于清空输入框。

3.6 创建主窗口并指定其大小和位置

① 程序设计


          
root = tk.Tk()
          
root.configure(background="black")
          
root.title("简易计算器")
          
sh = root.winfo_screenheight()
          
sw = root.winfo_screenwidth()
          
h = 150
          
w = 295
          
x = (sw - w) // 2
          
y = (sh - h) // 2
          
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
      

② 程序分析

通过调用tkinter库中的Tk()函数创建主窗口对象,并设置它的标题和背景颜色和大小。然后通过获取屏幕高度和宽度来自适应地计算窗口的大小和位置。最后使用geometry()方法将窗口定位和显示在屏幕上。

3.7 创建输入框

① 程序设计


          
entrys = tk.Entry(root, textvariable=result, width=22)
          
entrys.grid(columnspan=4, ipadx=70)
      

② 程序分析

创建一个tkinter.Entry对象来显示计算器的输入框。并使用grid()方法来将其添加到主窗口上,并设置它的位置和大小。

3.8 创建数字和运算符按钮

① 程序设计


          
button1 = tk.Button(root, text=' 1 ', bg=color1, command=lambda: Add(1), height=1, width=7)
          
button1.grid(row=2, column=0)
          
button2 = tk.Button(root, text=' 2 ', bg=color1, command=lambda: Add(2), height=1, width=7)
          
button2.grid(row=2, column=1)
          
button3 = tk.Button(root, text=' 3 ', bg=color1, command=lambda: Add(3), height=1, width=7)
          
button3.grid(row=2, column=2)
          
button4 = tk.Button(root, text=' 4 ', bg=color1, command=lambda: Add(4), height=1, width=7)
          
button4.grid(row=3, column=0)
          
button5 = tk.Button(root, text=' 5 ', bg=color1, command=lambda: Add(5), height=1, width=7)
          
button5.grid(row=3, column=1)
          
button6 = tk.Button(root, text=' 6 ', bg=color1, command=lambda: Add(6), height=1, width=7)
          
button6.grid(row=3, column=2)
          
button7 = tk.Button(root, text=' 7 ', bg=color1, command=lambda: Add(7), height=1, width=7)
          
button7.grid(row=4, column=0)
          
button8 = tk.Button(root, text=' 8 ', bg=color1, command=lambda: Add(8), height=1, width=7)
          
button8.grid(row=4, column=1)
          
button9 = tk.Button(root, text=' 9 ', bg=color1, command=lambda: Add(9), height=1, width=7)
          
button9.grid(row=4, column=2)
          
button0 = tk.Button(root, text=' 0 ', bg=color1, command=lambda: Add(0), height=1, width=7)
          
button0.grid(row=5, column=0)
          

          
add = tk.Button(root, text=' + ', bg=color2, command=lambda: Add("+"), height=1, width=7)
          
add.grid(row=2, column=3)
          
subtract = tk.Button(root, text=' - ', bg=color2, command=lambda: Add("-"), height=1, width=7)
          
subtract.grid(row=3, column=3)
          
multiply = tk.Button(root, text=' * ', bg=color2, command=lambda: Add("*"), height=1, width=7)
          
multiply.grid(row=4, column=3)
          
divide = tk.Button(root, text=' / ', bg=color2, command=lambda: Add("/"), height=1, width=7)
          
divide.grid(row=5, column=3)
      

② 程序分析

创建10个数字按钮和4个运算符按钮,并用grid()方法将它们添加到主窗口上。每个按钮都分配了相应的数字或符号,并与Add函数相关联以便将它们添加到输入框中。

3.9 创建等于号和清除按钮

① 程序设计


          
equal = tk.Button(root, text=' = ', bg=color3, command=Result, height=1, width=7)
          
equal.grid(row=5, column=2)
          
clear = tk.Button(root, text='Clear', bg=color4, command=Clear, height=1, width=7)
          
clear.grid(row=5, column=1)
      

② 程序分析

为等于号和清除按钮创建两个单独的按钮,并将它们添加到主窗口上。为等于号按钮分配Result函数并为清除按钮分配Clear函数。

四、功能实现

picture.image

完整代码放在公众号里喽,在公众号对话框回复“ python102 ”然后输入提取码“ co03 ”就可以下载啦~

写在后面

我是一只有趣的兔子,感谢你的喜欢!

0
0
0
0
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论