Python 计算器设计与实现指南
功能:
代码:
from tkinter import *
def calculate():
"""计算结果并显示"""
try:
result = eval(equ.get()) # 计算表达式
equ.set(equ.get() + "=\n" + str(result)) # 显示结果
except Exception as e:
equ.set("错误") # 如果计算失败,显示“错误”
def show(buttonString):
"""显示按钮内容"""
content = equ.get()
if content == "0": # 如果当前显示为“0”,则清空
content = ""
equ.set(content + buttonString) # 拼接按钮内容
def backspace():
"""删除最后一个字符"""
content = equ.get()
if content: # 如果内容不为空
equ.set(content[:-1]) # 删除最后一个字符
def clear():
"""清空显示区域"""
equ.set("0") # 重置为“0”
# 创建主窗口
root = Tk()
root.title("计算器")
# 创建 StringVar 并初始化
equ = StringVar()
equ.set("0")
# 设立显示区
label = Label(root, width=25, height=2, relief="raised", anchor=SE,
textvariable=equ)
label.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# 清除显示区域
clearButton = Button(root, text="C", fg="blue", width=5, command=clear)
clearButton.grid(row=1, column=0)
# 其他按钮
Button(root, text="DEL", width=5, command=backspace).grid(row=1, column=1)
Button(root, text="%", width=5, command=lambda: show("%")).grid(row=1, column=2)
Button(root, text="/", width=5, command=lambda: show("/")).grid(row=1, column=3)
Button(root, text="7", width=5, command=lambda: show("7")).grid(row=2, column=0)
Button(root, text="8", width=5, command=lambda: show("8")).grid(row=2, column=1)
Button(root, text="9", width=5, command=lambda: show("9")).grid(row=2, column=2)
Button(root, text="*", width=5, command=lambda: show("*")).grid(row=2, column=3)
Button(root, text="4", width=5, command=lambda: show("4")).grid(row=3, column=0)
Button(root, text="5", width=5, command=lambda: show("5")).grid(row=3, column=1)
Button(root, text="6", width=5, command=lambda: show("6")).grid(row=3, column=2)
Button(root, text="-", width=5, command=lambda: show("-")).grid(row=3, column=3)
Button(root, text="1", width=5, command=lambda: show("1")).grid(row=4, column=0)
Button(root, text="2", width=5, command=lambda: show("2")).grid(row=4, column=1)
Button(root, text="3", width=5, command=lambda: show("3")).grid(row=4, column=2)
Button(root, text="+", width=5, command=lambda: show("+")).grid(row=4, column=3)
Button(root, text="0", width=12,
command=lambda: show("0")).grid(row=5, column=0, columnspan=2)
Button(root, text=".", width=5,
command=lambda: show(".")).grid(row=5, column=2)
Button(root, text="=", width=5, bg="yellow",
command=calculate).grid(row=5, column=3) # 绑定到 calculate 函数
# 启动主事件循环
root.mainloop()
代码解析:
代码实现了一个简单的计算器应用程序,功能包括:
-
支持基本的数字和运算符输入。
-
支持删除最后一个字符(DEL 按钮)。
-
支持清空显示区域(C 按钮)。
-
支持计算结果(= 按钮)。
代码逻辑清晰,功能完整,可以正常运行。以下是代码的详细解析和改进建议:
代码逐行解析:
-
导入 Tkinter 模块:
from tkinter import *
导入 Tkinter 库的所有内容,用于创建 GUI 应用程序。
-
定义
calculate
函数:def calculate(): """计算结果并显示""" try: result = eval(equ.get()) # 计算表达式 equ.set(equ.get() + "=\n" + str(result)) # 显示结果 except Exception as e: equ.set("错误") # 如果计算失败,显示“错误”
-
使用
eval
函数计算表达式的结果。 -
如果计算成功,将结果附加到当前表达式后面并显示。
-
如果计算失败(例如表达式不合法),显示“错误”。
-
定义
show
函数:def show(buttonString): """显示按钮内容""" content = equ.get() if content == "0": # 如果当前显示为“0”,则清空 content = "" equ.set(content + buttonString) # 拼接按钮内容
-
将按钮的内容追加到当前显示内容中。
-
如果当前显示为“0”,则清空内容后再追加。
-
定义
backspace
函数:def backspace(): """删除最后一个字符""" content = equ.get() if content: # 如果内容不为空 equ.set(content[:-1]) # 删除最后一个字符
-
删除当前显示内容的最后一个字符。
-
定义
clear
函数:def clear(): """清空显示区域""" equ.set("0") # 重置为“0”
-
清空显示区域,重置为“0”。
-
创建主窗口:
root = Tk() root.title("计算器")
-
创建一个 Tkinter 主窗口。
-
设置窗口标题为
"计算器"
。 -
创建
StringVar
并初始化:equ = StringVar() equ.set("0")
-
equ
是一个StringVar
,用于存储显示区域的内容。 -
初始值设置为
"0"
。 -
设立显示区:
label = Label(root, width=25, height=2, relief="raised", anchor=SE, textvariable=equ) label.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
-
创建一个
Label
控件,用于显示表达式和结果。 -
textvariable=equ
将Label
的内容与equ
绑定。 -
使用
grid
方法将Label
放置在第 0 行,跨越 4 列。 -
创建按钮:
-
清除按钮(C):
clearButton = Button(root, text="C", fg="blue", width=5, command=clear) clearButton.grid(row=1, column=0)
-
删除按钮(DEL):
Button(root, text="DEL", width=5, command=backspace).grid(row=1, column=1)
-
其他按钮:
Button(root, text="%", width=5, command=lambda: show("%")).grid(row=1, column=2) Button(root, text="/", width=5, command=lambda: show("/")).grid(row=1, column=3)
-
数字和运算符按钮:
Button(root, text="7", width=5, command=lambda: show("7")).grid(row=2, column=0) Button(root, text="8", width=5, command=lambda: show("8")).grid(row=2, column=1) Button(root, text="9", width=5, command=lambda: show("9")).grid(row=2, column=2) Button(root, text="*", width=5, command=lambda: show("*")).grid(row=2, column=3) Button(root, text="4", width=5, command=lambda: show("4")).grid(row=3, column=0) Button(root, text="5", width=5, command=lambda: show("5")).grid(row=3, column=1) Button(root, text="6", width=5, command=lambda: show("6")).grid(row=3, column=2) Button(root, text="-", width=5, command=lambda: show("-")).grid(row=3, column=3) Button(root, text="1", width=5, command=lambda: show("1")).grid(row=4, column=0) Button(root, text="2", width=5, command=lambda: show("2")).grid(row=4, column=1) Button(root, text="3", width=5, command=lambda: show("3")).grid(row=4, column=2) Button(root, text="+", width=5, command=lambda: show("+")).grid(row=4, column=3) Button(root, text="0", width=12, command=lambda: show("0")).grid(row=5, column=0, columnspan=2) Button(root, text=".", width=5, command=lambda: show(".")).grid(row=5, column=2) Button(root, text="=", width=5, bg="yellow", command=calculate).grid(row=5, column=3)
-
启动主事件循环:
root.mainloop()
启动 Tkinter 的主事件循环,等待用户交互。
作者:niuliting