Python:【小程序】基于tkinter库制作简易的计算器

基于 python 的 tkinter 库编写简易的计算器。

1 具体功能实现

  • 输入数字并显示
  • 加减乘除运算
  • 赋值功能
  • 删除功能
  • 清零功能

2 实现过程

2.1 建立 tkinter 窗口

window = tk.Tk()                                  # 建立一个窗口
window.geometry('500x540')                        # 设置窗口的长度和宽度
window.resizable(width=False, height=False)       # 设置窗口长度和宽度不可变化

                                                  # 设置两个文本显示栏
t = tk.Text(window, height=1, width = 24, font=('Arial',24))
t.place(x=35,y=30)
t_result = tk.Text(window, height=1, width=24, font=('Arial', 24))
t_result.place(x=35, y=72)

window.mainloop()                                 # 实现窗口刷新

2.2 实现数字及运算符的输入

数字和运算符统一命名为 b_x,以输入数字 1 为例:

b_1 = tk.Button(window, text='1', width=10, height=3, command=lambda : display('1'), font=('Arial', 12), bg='lavender')
b_1.place(x=50, y=350)

Button中的command用于点击按钮时启用display函数:

def display(x):
    t.insert('insert', str(x))
    equal()

其中equal函数实现自动运算并将结果显示的功能:

def equal():
    str1 = t.get('1.0', 'end')
    string = str1.replace('%', '/100').replace('x', '*')
    t_result.delete('1.0', 'end')
    try:
        num = round(eval(string), 8)
        t_result.insert('end', num)
    except:
        num = ''
        t_result.insert('end', num)

特别注意:计算机运算时采用二进制算法,我们输入的十进制有限小数转换成二进制时有可能变成无限循环小数,可能会使最后的输出结果存在微小偏差,因此我们采用round函数保留其小数点后8位,避免误差的产生。

2.3 赋值功能

计算器的=号按钮连接equalall函数,实现赋值功能,将第二行自动运算的结果赋值给第一行,如果运算错误,则给出错误信息的提示:

def equalall():
    str1 = t.get('1.0', 'end')
    string = str1.replace('%', '/100').replace('x', '*')
    t_result.delete('1.0', 'end')
    try:
        num = round(eval(string), 8)
        t.delete('1.0', 'end')
        t.insert('end', num)
    except:
        num = 'Wrong Expression!'
        t_result.insert('end', num)

2.4 删除功能和清零功能

删除功能和清零功能由对应的dele函数和clear函数分别实现:

def clear():
    t.delete('1.0', 'end')
    t_result.delete('1.0', 'end')

def dele():
    len1 = len(t.get('1.0', 'insert'))
    len2 = len1 - 1
    t.delete('1.' + str(len2), '1.' + str(len1))
    equal()

3 效果展示

Python:【小程序】基于tkinter库制作简易的计算器_第1张图片

4 程序源码

import tkinter as tk
from tkinter import messagebox

def ask_close():
    ask = messagebox.askokcancel('温❤提示 :', '你要退出计算器吗?')
    if ask == True:
        window.destroy()
        exit(0)

def display(x):
    t.insert('insert', str(x))
    equal()

def clear():
    t.delete('1.0', 'end')
    t_result.delete('1.0', 'end')

def dele():
    len1 = len(t.get('1.0', 'insert'))
    len2 = len1 - 1
    t.delete('1.' + str(len2), '1.' + str(len1))
    equal()

def equal():
    str1 = t.get('1.0', 'end')
    string = str1.replace('%', '/100').replace('x', '*')
    t_result.delete('1.0', 'end')
    try:
        num = round(eval(string), 8)
        t_result.insert('end', num)
    except:
        num = ''
        t_result.insert('end', num)

def equalall():
    str1 = t.get('1.0', 'end')
    string = str1.replace('%', '/100').replace('x', '*')
    t_result.delete('1.0', 'end')
    try:
        num = round(eval(string), 8)
        t.delete('1.0', 'end')
        t.insert('end', num)
    except:
        num = 'Wrong Expression!'
        t_result.insert('end', num)

if __name__ == '__main__':
    try:
        window = tk.Tk()
        window.title('小辰的计算器')
        window.geometry('500x540')
        window.protocol('WM_DELETE_WINDOW', ask_close)
        window.resizable(width=False, height=False)
        tk.Label(window, bg='lavenderblush', width=100, height=100).place(x=0, y=0)

        #第一排的按钮
        b_C = tk.Button(window, text='C', width=10, height=3, command=clear, font=('Arial',12), bg='plum' )
        b_C.place(x=50,y=140)
        b_division = tk.Button(window, text='/', width=10, height=3, command=lambda : display('/'), font=('Arial', 12), bg='plum')
        b_division.place(x=152, y=140)
        b_multipy = tk.Button(window, text='x', width=10, height=3, command=lambda : display('x'), font=('Arial', 12), bg='plum')
        b_multipy.place(x=254, y=140)
        b_delete = tk.Button(window, text='<=', width=10, height=3, command=dele, font=('Arial', 12), bg='plum')
        b_delete.place(x=356, y=140)

        # 第二排的按钮
        b_7 = tk.Button(window, text='7', width=10, height=3, command=lambda : display('7'), font=('Arial', 12), bg='lavender')
        b_7.place(x=50, y=210)
        b_8 = tk.Button(window, text='8', width=10, height=3, command=lambda : display('8'), font=('Arial', 12), bg='lavender')
        b_8.place(x=152, y=210)
        b_9 = tk.Button(window, text='9', width=10, height=3, command=lambda : display('9'), font=('Arial', 12), bg='lavender')
        b_9.place(x=254, y=210)
        b_subtraction = tk.Button(window, text='--', width=10, height=3, command=lambda : display('-'), font=('Arial', 12), bg='plum')
        b_subtraction.place(x=356, y=210)

        # 第三排的按钮
        b_4 = tk.Button(window, text='4', width=10, height=3, command=lambda : display('4'), font=('Arial', 12), bg='lavender')
        b_4.place(x=50, y=280)
        b_5 = tk.Button(window, text='5', width=10, height=3, command=lambda : display('5'), font=('Arial', 12), bg='lavender')
        b_5.place(x=152, y=280)
        b_6 = tk.Button(window, text='6', width=10, height=3, command=lambda : display('6'), font=('Arial', 12), bg='lavender')
        b_6.place(x=254, y=280)
        b_addition = tk.Button(window, text='+', width=10, height=3, command=lambda : display('+'), font=('Arial', 12), bg='plum')
        b_addition.place(x=356, y=280)

        # 第四排的按钮
        b_1 = tk.Button(window, text='1', width=10, height=3, command=lambda : display('1'), font=('Arial', 12), bg='lavender')
        b_1.place(x=50, y=350)
        b_2 = tk.Button(window, text='2', width=10, height=3, command=lambda : display('2'), font=('Arial', 12), bg='lavender')
        b_2.place(x=152, y=350)
        b_3 = tk.Button(window, text='3', width=10, height=3, command=lambda : display('3'), font=('Arial', 12), bg='lavender')
        b_3.place(x=254, y=350)
        b_equalall = tk.Button(window, text='=', width=10, height=7, command=equalall, font=('Arial', 12), bg='blueviolet')
        b_equalall.place(x=356, y=348)

        # 第五排的按钮
        b_percent = tk.Button(window, text='%', width=10, height=3, command=lambda : display('%'), font=('Arial', 12), bg='lavender')
        b_percent.place(x=50, y=420)
        b_0 = tk.Button(window, text='0', width=10, height=3, command=lambda : display('0'), font=('Arial', 12), bg='lavender')
        b_0.place(x=152, y=420)
        b_spot = tk.Button(window, text='.', width=10, height=3, command=lambda : display('.'), font=('Arial', 12), bg='lavender')
        b_spot.place(x=254, y=420)

        # 显示栏
        t = tk.Text(window, height=1, width = 24, font=('Arial',24))
        t.place(x=35,y=30)
        t_result = tk.Text(window, height=1, width=24, font=('Arial', 24))
        t_result.place(x=35, y=72)

        window.mainloop()
    except SystemExit:
        pass

你可能感兴趣的:(Python)