tkinter制作计算器

简单的计算器,大家可以试着参考下,本来想还原电脑自带的,奈何学艺不精。

原码如下:有什么不对的还请多多指教。

后面可以打包成exe文件形式。

首先需要导入pyinstaller这个包,然后在终端运行该文件,输入指令:pyinstaller -F -w 文件名字.py

最后打包在dist中显示。

我自己的运行结果如下:

tkinter制作计算器_第1张图片

 已经打包成exe文件了。

# coding:utf-8
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.minsize(300, 550)
root.config(background="#F3F3F3")
root.attributes("-alpha", 0.98)
root.title('计算器')
my_font = ('微软雅黑', 20)


# 开始设置按键
class A:
    def __init__(self):
        self.a=0
b = A()
# 给按键插入功能
# 数字键
def get_num(num):
    old_num = result.get()
    if old_num == '0':
        result.set(num)
    else:
        result.set(old_num + num)

def press_operation(opertion):
    # 防止特殊符号
    # 在第二次点击运算符时就该计算结果
    if opertion == 'c':
        result.set('0')
        record.set('')
        return 0
    if opertion == '←':
        result.set(result.get()[0:-1])
        return 0
    # 考虑其他正常运算情况
    # 先读取运算符到record中
    b.a += 1
    record.set(record.get()+result.get()+opertion)
    result.set('')
    if b.a != 1:
        m = record.get()[0:-1]
        final = eval(m+result.get())
        record.set(str(final)+opertion)
    if opertion == '=':
        # 计算值,result消失,record显示最终结果
        m = record.get()[0:-1]
        final = eval(m + result.get())
        record.set(str(final))
        result.set('')


# 显示输入数字及结果
result = tkinter.StringVar()
# 显示运算过程
record = tkinter.StringVar()
result.set('0')
record.set('')
# 显示板
label1 = tk.Label(root, font=my_font, bg='#E6E6E6', anchor='se', textvariable=record, bd='3')
label1.place(width=300, height=120)
label2 = tk.Label(root, font=my_font, bg='#E6E6E6', anchor='se', textvariable=result, bd='3')
label2.place(y=120, width=300, height=80)

# 添加第一行
btn_ac = tk.Button(root, text='c', font=my_font, bg='#E0E0E0', bd=0, activebackground='#9ABCBC',
                   command=lambda: press_operation('c'))
btn_ac.place(x=75 * 0, y=200 + 70 * 0, width=75, height=70)
btn_back = tk.Button(root, text='←', font=my_font, bg='#E0E0E0', bd=0, activebackground='#9ABCBC',
                     command=lambda: press_operation('←'))
btn_back.place(x=75 * 1, y=200 + 70 * 0, width=75, height=70)
btn_per = tk.Button(root, text='%', font=my_font, bg='#E0E0E0', bd=0, activebackground='#9ABCBC',
                    command=lambda: press_operation('%'))
btn_per.place(x=75 * 2, y=200 + 70 * 0, width=75, height=70)
btn_divi = tk.Button(root, text='÷', font=my_font, bg='#E0E0E0', bd=0, activebackground='#9ABCBC',
                     command=lambda: press_operation('/'))
btn_divi.place(x=75 * 3, y=200 + 70 * 0, width=75, height=70)

# 添加第二行
btn_7 = tk.Button(root, text='7', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('7'))
btn_7.place(x=75 * 0, y=200 + 70 * 1, width=75, height=70)
btn_8 = tk.Button(root, text='8', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('8'))
btn_8.place(x=75 * 1, y=200 + 70 * 1, width=75, height=70)
btn_9 = tk.Button(root, text='9', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('9'))
btn_9.place(x=75 * 2, y=200 + 70 * 1, width=75, height=70)
btn_mul = tk.Button(root, text='x', font=my_font, bg='#E0E0E0', bd=0, activebackground='#9ABCBC',
                    command=lambda: press_operation('*'))
btn_mul.place(x=75 * 3, y=200 + 70 * 1, width=75, height=70)

# 添加第三行
btn_4 = tk.Button(root, text='4', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('4'))
btn_4.place(x=75 * 0, y=200 + 70 * 2, width=75, height=70)
btn_5 = tk.Button(root, text='5', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('5'))
btn_5.place(x=75 * 1, y=200 + 70 * 2, width=75, height=70)
btn_6 = tk.Button(root, text='6', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('6'))
btn_6.place(x=75 * 2, y=200 + 70 * 2, width=75, height=70)
btn_sub = tk.Button(root, text='-', font=my_font, bg='#E0E0E0', bd=0, activebackground='#9ABCBC',
                    command=lambda: press_operation('-'))
btn_sub.place(x=75 * 3, y=200 + 70 * 2, width=75, height=70)

# 添加第四行
btn_1 = tk.Button(root, text='1', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('1'))
btn_1.place(x=75 * 0, y=200 + 70 * 3, width=75, height=70)
btn_2 = tk.Button(root, text='2', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('2'))
btn_2.place(x=75 * 1, y=200 + 70 * 3, width=75, height=70)
btn_3 = tk.Button(root, text='3', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('3'))
btn_3.place(x=75 * 2, y=200 + 70 * 3, width=75, height=70)
btn_add = tk.Button(root, text='+', font=my_font, bg='#E0E0E0', bd=0, activebackground='#9ABCBC',
                    command=lambda: press_operation('+'))
btn_add.place(x=75 * 3, y=200 + 70 * 3, width=75, height=70)

# 添加第五行
btn_0 = tk.Button(root, text='0', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                  command=lambda: get_num('0'))
btn_0.place(x=75 * 0, y=200 + 70 * 4, width=150, height=70)
btn_point = tk.Button(root, text='.', font=my_font, bg='#F0F0F0', bd=0, activebackground='#84a4a2',
                      command=lambda: get_num('.'))
btn_point.place(x=75 * 2, y=200 + 70 * 4, width=75, height=70)
btn_equ = tk.Button(root, text='=', font=my_font, bg='#95ACB1', bd=0, activebackground='#9ABCBC',
                    command=lambda: press_operation('='))
btn_equ.place(x=75 * 3, y=200 + 70 * 4, width=75, height=70)
# 进入循环显示
root.mainloop()

你可能感兴趣的:(days.1,python,开发语言)