tkinter制作计算器

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

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

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

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

最后打包在dist中显示。

我自己的运行结果如下:

 已经打包成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()

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的 Python Tkinter 计算器的代码示例: ``` python import tkinter as tk class Calculator: def __init__(self, master): self.master = master master.title("计算器") self.display = tk.Entry(master, width=30, font=('Arial', 16)) self.display.grid(row=0, column=0, columnspan=4, padx=10, pady=10) button_list = [ "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" ] row = 1 col = 0 for button_text in button_list: button = tk.Button(master, text=button_text, width=5, height=2, font=('Arial', 16), command=lambda x=button_text: self.button_click(x)) button.grid(row=row, column=col, padx=5, pady=5) col += 1 if col > 3: col = 0 row += 1 def button_click(self, text): if text == "=": result = eval(self.display.get()) self.display.delete(0, tk.END) self.display.insert(0, result) elif text == "C": self.display.delete(0, tk.END) else: self.display.insert(tk.END, text) root = tk.Tk() app = Calculator(root) root.mainloop() ``` 在这个计算器中,我们使用了 Tkinter 中的 Entry 和 Button 组件。Entry 组件用于显示计算结果,而 Button 组件则用于用户输入数字和运算符。 在构造函数 `__init__` 中,我们首先创建了一个 Entry 组件,并将其放置在第一行第一列,并占据 4 列。接着,我们创建了一个按钮列表,其中包含了计算器的所有功能键。然后,我们使用 for 循环来将这些按钮放置在计算器的界面中。 在 `button_click` 函数中,我们根据用户输入的按钮内容进行相应的处理。如果是 "=" 按钮,则获取 Entry 中的内容并计算结果,然后将结果显示在 Entry 中。如果是 "C" 按钮,则清空 Entry 的内容。否则,将按钮内容添加到 Entry 的末尾。最后,我们创建了一个 Tkinter 窗口,并将其传递给 Calculator 类的实例以启动计算器。 这个计算器还比较简单,但是基本的功能已经实现了。如果你想要添加其他功能,可以在按钮列表中添加相应的按钮,并在 `button_click` 函数中进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值