利用tkinter制作简易计算器,页面美观,计算保留4位小数

import tkinter as tk
import time
window = tk.Tk()
window.title("简易计算器")
window.geometry('300x400')
content = ''
def btn_onclick(data):
    global content
    if data=="AC" or data =="MC":
        expression.set('')
        result.set('')
        content =''
    elif data == '=':
        result.set(f'{round(eval(content),5)}')
    else:
        content+=data
        expression.set(content)


#___________________________________________
lable_time_text = tk.Label(window,text="您好,北京时间:",font=("楷体",15))
lable_time_text.place(x=0,y=100,width=180,height=30)
gettime = tk.StringVar(value=(time.strftime("%H:%M:%S",time.localtime(time.time()))))

lable_time = tk.Label(window,textvariable=gettime,font=("楷体",15))
lable_time.place(x=180,y=100,width=100,height=30)
#_____________________________________________

btn_datas =[
    ["AC"," ","/","%"],
    ["7","8","9","*"],
    ["4","5","6","-"],
    ["1","2","3","+"],
    ["MC","0",".","="]
]

# command=lambda x=btn_datas[r][c]:btn_onclick(x)
for r in range(5):
    for c in range(4):
        btn_submit=tk.Button(window,text=btn_datas[r][c],font=("楷体",15),command=lambda x=btn_datas[r][c]:btn_onclick(x))
        btn_submit.place(x=c*72+6,y=r*50+140,width=70,height=50)
#------------------------------------------------
frame = tk.Frame(window,bd=2,relief="sunken")
frame.place(x=10,y=10,width=270,height=30)
expression = tk.StringVar( )
lable_express_text = tk.Label(frame,textvariable=expression,font=("楷体",15))
lable_express_text.place(x=0,y=0,width=270,height=30)

lable_result_text = tk.Label(window,text="计算结果:",font=("楷体",15))
lable_result_text.place(x=0,y=50,width=150,height=30)
result = tk.StringVar(value='0')
lable_result_content = tk.Label(window,textvariable=result,font=("楷体",15))
lable_result_content.place(x=150,y=50,width=100,height=30)



#______________________________________________________
window.mainloop()
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个使用 Tkinter GUI 库制作Python 计算器的代码示例: ``` python import tkinter as tk class Calculator: def __init__(self, master): self.master = master master.title("简易计算器") # 创建输入框 self.entry = tk.Entry(master, width=35, borderwidth=5) self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10) # 创建按钮 self.button_1 = tk.Button(master, text="1", padx=40, pady=20, command=lambda: self.button_click(1)) self.button_2 = tk.Button(master, text="2", padx=40, pady=20, command=lambda: self.button_click(2)) self.button_3 = tk.Button(master, text="3", padx=40, pady=20, command=lambda: self.button_click(3)) self.button_4 = tk.Button(master, text="4", padx=40, pady=20, command=lambda: self.button_click(4)) self.button_5 = tk.Button(master, text="5", padx=40, pady=20, command=lambda: self.button_click(5)) self.button_6 = tk.Button(master, text="6", padx=40, pady=20, command=lambda: self.button_click(6)) self.button_7 = tk.Button(master, text="7", padx=40, pady=20, command=lambda: self.button_click(7)) self.button_8 = tk.Button(master, text="8", padx=40, pady=20, command=lambda: self.button_click(8)) self.button_9 = tk.Button(master, text="9", padx=40, pady=20, command=lambda: self.button_click(9)) self.button_0 = tk.Button(master, text="0", padx=40, pady=20, command=lambda: self.button_click(0)) self.button_add = tk.Button(master, text="+", padx=39, pady=20, command=self.button_add) self.button_subtract = tk.Button(master, text="-", padx=41, pady=20, command=self.button_subtract) self.button_multiply = tk.Button(master, text="*", padx=40, pady=20, command=self.button_multiply) self.button_divide = tk.Button(master, text="/", padx=41, pady=20, command=self.button_divide) self.button_equal = tk.Button(master, text="=", padx=91, pady=20, command=self.button_equal) self.button_clear = tk.Button(master, text="清除", padx=79, pady=20, command=self.button_clear) # 把按钮放在界面上 self.button_1.grid(row=1, column=0) self.button_2.grid(row=1, column=1) self.button_3.grid(row=1, column=2) self.button_4.grid(row=2, column=0) self.button_5.grid(row=2, column=1) self.button_6.grid(row=2, column=2) self.button_7.grid(row=3, column=0) self.button_8.grid(row=3, column=1) self.button_9.grid(row=3, column=2) self.button_0.grid(row=4, column=0) self.button_clear.grid(row=4, column=1, columnspan=2) self.button_add.grid(row=5, column=0) self.button_subtract.grid(row=6, column=0) self.button_multiply.grid(row=6, column=1) self.button_divide.grid(row=6, column=2) self.button_equal.grid(row=5, column=1, columnspan=2) # 定义操作函数 self.current_num = "" self.first_num = None self.operation = None def button_click(self, number): self.current_num = str(self.current_num) + str(number) self.entry.delete(0, tk.END) self.entry.insert(0, self.current_num) def button_clear(self): self.entry.delete(0, tk.END) self.current_num = "" self.first_num = None self.operation = None def button_add(self): self.first_num = float(self.current_num) self.operation = "+" self.entry.delete(0, tk.END) self.current_num = "" def button_multiply(self): self.first_num = float(self.current_num) self.operation = "*" self.entry.delete(0, tk.END) self.current_num = "" def button_subtract(self): self.first_num = float(self.current_num) self.operation = "-" self.entry.delete(0, tk.END) self.current_num = "" def button_divide(self): self.first_num = float(self.current_num) self.operation = "/" self.entry.delete(0, tk.END) self.current_num = "" def button_equal(self): second_num = float(self.current_num) self.entry.delete(0, tk.END) if self.operation == "+": result = self.first_num + second_num elif self.operation == "-": result = self.first_num - second_num elif self.operation == "*": result = self.first_num * second_num elif self.operation == "/": result = self.first_num / second_num self.entry.insert(0, result) self.current_num = "" self.first_num = None self.operation = None root = tk.Tk() calculator = Calculator(root) root.mainloop() ``` 这个计算器使用了 Tkinter GUI 库来创建用户界面。用户可以通过按钮输入数字和运算符,计算器会在输入框中显示结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青龙摄影

你的鼓励是我创作的动力,支持下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值