Python实现一个简单的计算器

简单版本

  使用 Python 的 Tkinter 模块来实现一个简单的图形化计算器。以下是一个基本的示例代码

示例效果

代码源码

python 复制代码
import tkinter as tk

def button_click(number):
    current = entry.get()
    entry.delete(0, tk.END)
    entry.insert(0, current + str(number))

def button_clear():
    entry.delete(0, tk.END)

def button_equal():
    result = eval(entry.get())
    entry.delete(0, tk.END)
    entry.insert(0, result)

root = tk.Tk()
root.title("简易计算器")

entry = tk.Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

buttons = [
    ("7", 1, 0), ("8", 1, 1), ("9", 1, 2), ("/", 1, 3),
    ("4", 2, 0), ("5", 2, 1), ("6", 2, 2), ("*", 2, 3),
    ("1", 3, 0), ("2", 3, 1), ("3", 3, 2), ("-", 3, 3),
    ("0", 4, 0), (".", 4, 1), ("=", 4, 2), ("+", 4, 3)
]

for button_text, row, col in buttons:
    button = tk.Button(root, text=button_text, padx=20, pady=20, command=lambda text=button_text: button_click(text))
    button.grid(row=row, column=col)

clear_button = tk.Button(root, text="清除", padx=61, pady=20, command=button_clear)
clear_button.grid(row=5, column=0, columnspan=2)

equal_button = tk.Button(root, text="=", padx=61, pady=20, command=button_equal)
equal_button.grid(row=5, column=2, columnspan=2)

root.mainloop()

高级版本

  相比于基础版本增加了很多的判断,并且对UI页面也进行了优化

实现效果

代码源码

python 复制代码
import tkinter as tk

def button_click(char):
    if calculated:
        entry.delete(0, tk.END)
        globals()['calculated'] = False
    entry.insert(tk.END, char)

def button_clear():
    entry.delete(0, tk.END)

def button_equal():
    try:
        result = eval(entry.get())
        entry.delete(0, tk.END)
        entry.insert(tk.END, str(result))
        globals()['calculated'] = True
    except:
        entry.delete(0, tk.END)
        entry.insert(tk.END, "Error")
        globals()['calculated'] = False

calculated = False

root = tk.Tk()
root.title("简易计算器")

entry = tk.Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

# 定义按钮布局和样式
buttons = [
    ("7", 1, 0), ("8", 1, 1), ("9", 1, 2), ("+", 1, 3),
    ("4", 2, 0), ("5", 2, 1), ("6", 2, 2), ("-", 2, 3),
    ("1", 3, 0), ("2", 3, 1), ("3", 3, 2), ("*", 3, 3),
    ("0", 4, 0), ("清除", 4, 1), ("=", 4, 2), ("/", 4, 3)
]

# 创建并放置按钮
for button_text, row, col in buttons:
    if button_text == "=":
        button = tk.Button(root, text=button_text, padx=20, pady=20, font=("Helvetica", 12), bg="lightgray", command=button_equal)
    elif button_text == "清除":
        button = tk.Button(root, text=button_text, padx=20, pady=20, font=("Helvetica", 12), bg="lightgray", command=button_clear)
    else:
        button = tk.Button(root, text=button_text, padx=20, pady=20, font=("Helvetica", 12), bg="lightgray", command=lambda text=button_text: button_click(text))
    button.grid(row=row, column=col, padx=5, pady=5)

root.mainloop()
相关推荐
Raas10018 分钟前
AI网关和OpenRouter区别?MAI Gateway(魔芋企业级AI网关)企业级方案对比指南
大数据·开发语言·人工智能·gateway·php·ai网关·mai gateway
2601_962293248 小时前
Python自动化统计团队工作量并生成可视化仪表盘的脚本方案【指导】
python·数据分析·自动化·可视化·仪表盘
点心的游戏开发世界8 小时前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
2601_962293799 小时前
OCRmyPDF批处理脚本:使用Python自动化复杂OCR任务
python·自动化·批处理脚本·ocrmypdf·ocr任务
事圆则缓10 小时前
Kotlin 协程完全指南
开发语言·kotlin
我爱写代码i10 小时前
BeLink - 支持生成多种URL 缩短网址PHP源码
开发语言·php·短网址php源码
Java后端的Ai之路10 小时前
20、Python - 备忘录模式
开发语言·人工智能·python·外观模式·备忘录模式
2601_9620777111 小时前
基于Python与NLP的新闻事件信息抽取实战:从NER到时空标准化
python·nlp·transformers·spacy·新闻事件抽取
JavaPub-rodert11 小时前
LangChain 从入门到 Agent 实战:用 Python 搭建一个真正能调用工具和知识库的 AI 助手
人工智能·python·langchain
统计学小王子11 小时前
数学建模国赛倒计时4天——《统计模型精讲(混合效应模型R语言实现)》
开发语言·数学建模·r语言