- Python基础
- Python - 首页
- Python - 概述
- Python - 历史
- Python - 特性
- Python vs C++
- Python - Hello World程序
- Python - 应用领域
- Python - 解释器
- Python - 环境搭建
- Python - 虚拟环境
- Python - 基本语法
- Python - 变量
- Python - 数据类型
- Python - 类型转换
- Python - Unicode系统
- Python - 字面量
- Python - 运算符
- Python - 算术运算符
- Python - 比较运算符
- Python - 赋值运算符
- Python - 逻辑运算符
- Python - 位运算符
- Python - 成员运算符
- Python - 身份运算符
- Python - 运算符优先级
- Python - 注释
- Python - 用户输入
- Python - 数字
- Python - 布尔值
- Python 控制语句
- Python - 控制流
- Python - 决策
- Python - if语句
- Python - if else
- Python - 嵌套if
- Python - Match-Case语句
- Python - 循环
- Python - for循环
- Python - for-else循环
- Python - while循环
- Python - break语句
- Python - continue语句
- Python - pass语句
- Python - 嵌套循环
- Python 函数与模块
- Python - 函数
- Python - 默认参数
- Python - 关键字参数
- Python - 只接受关键字参数
- Python - 位置参数
- Python - 只接受位置参数
- Python - 可变参数
- Python - 变量作用域
- Python - 函数注解
- Python - 模块
- Python - 内置函数
- Python 字符串
- Python - 字符串
- Python - 字符串切片
- Python - 修改字符串
- Python - 字符串连接
- Python - 字符串格式化
- Python - 转义字符
- Python - 字符串方法
- Python - 字符串练习
- Python 列表
- Python - 列表
- Python - 访问列表元素
- Python - 修改列表元素
- Python - 添加列表元素
- Python - 删除列表元素
- Python - 遍历列表
- Python - 列表推导式
- Python - 排序列表
- Python - 复制列表
- Python - 合并列表
- Python - 列表方法
- Python - 列表练习
- Python 元组
- Python - 元组
- Python - 访问元组元素
- Python - 更新元组
- Python - 解包元组
- Python - 遍历元组
- Python - 合并元组
- Python - 元组方法
- Python - 元组练习
- Python 集合
- Python - 集合
- Python - 访问集合元素
- Python - 添加集合元素
- Python - 删除集合元素
- Python - 遍历集合
- Python - 合并集合
- Python - 复制集合
- Python - 集合运算符
- Python - 集合方法
- Python - 集合练习
- Python 字典
- Python - 字典
- Python - 访问字典元素
- Python - 修改字典元素
- Python - 添加字典元素
- Python - 删除字典元素
- Python - 字典视图对象
- Python - 遍历字典
- Python - 复制字典
- Python - 嵌套字典
- Python - 字典方法
- Python - 字典练习
- Python 数组
- Python - 数组
- Python - 访问数组元素
- Python - 添加数组元素
- Python - 删除数组元素
- Python - 遍历数组
- Python - 复制数组
- Python - 反转数组
- Python - 排序数组
- Python - 合并数组
- Python - 数组方法
- Python - 数组练习
- Python 文件处理
- Python - 文件处理
- Python - 写入文件
- Python - 读取文件
- Python - 重命名和删除文件
- Python - 目录
- Python - 文件方法
- Python - OS文件/目录方法
- Python - OS路径方法
- 面向对象编程
- Python - OOPs概念
- Python - 类与对象
- Python - 类属性
- Python - 类方法
- Python - 静态方法
- Python - 构造函数
- Python - 访问修饰符
- Python - 继承
- Python - 多态
- Python - 方法重写
- Python - 方法重载
- Python - 动态绑定
- Python - 动态类型
- Python - 抽象
- Python - 封装
- Python - 接口
- Python - 包
- Python - 内部类
- Python - 匿名类和对象
- Python - 单例类
- Python - 包装类
- Python - 枚举
- Python - 反射
- Python 错误与异常
- Python - 语法错误
- Python - 异常
- Python - try-except块
- Python - try-finally块
- Python - 抛出异常
- Python - 异常链
- Python - 嵌套try块
- Python - 用户自定义异常
- Python - 日志记录
- Python - 断言
- Python - 内置异常
- Python 多线程
- Python - 多线程
- Python - 线程生命周期
- Python - 创建线程
- Python - 启动线程
- Python - 连接线程
- Python - 线程命名
- Python - 线程调度
- Python - 线程池
- Python - 主线程
- Python - 线程优先级
- Python - 守护线程
- Python - 同步线程
- Python 同步
- Python - 线程间通信
- Python - 线程死锁
- Python - 中断线程
- Python 网络编程
- Python - 网络编程
- Python - 套接字编程
- Python - URL处理
- Python - 泛型
- Python 库
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 杂项
- Python - 日期与时间
- Python - 数学
- Python - 迭代器
- Python - 生成器
- Python - 闭包
- Python - 装饰器
- Python - 递归
- Python - 正则表达式
- Python - PIP
- Python - 数据库访问
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 输出格式化
- Python - 性能测量
- Python - 数据压缩
- Python - CGI编程
- Python - XML处理
- Python - GUI编程
- Python - 命令行参数
- Python - 文档字符串
- Python - JSON
- Python - 发送邮件
- Python - 扩展
- Python - 工具/实用程序
- Python - GUIs
- Python 高级概念
- Python - 抽象基类
- Python - 自定义异常
- Python - 高阶函数
- Python - 对象内部
- Python - 内存管理
- Python - 元类
- Python - 使用元类进行元编程
- Python - 模拟和存根
- Python - 猴子补丁
- Python - 信号处理
- Python - 类型提示
- Python - 自动化教程
- Python - Humanize包
- Python - 上下文管理器
- Python - 协程
- Python - 描述符
- Python - 诊断和修复内存泄漏
- Python - 不可变数据结构
- Python 有用资源
- Python - 问答
- Python - 在线测验
- Python - 快速指南
- Python - 参考
- Python - 速查表
- Python - 项目
- Python - 有用资源
- Python - 讨论
- Python 编译器
- NumPy 编译器
- Matplotlib 编译器
- SciPy 编译器
使用Flask构建基于Web的计算器
在这里,我们将学习使用Python和Flask创建基于Web的计算器的分步过程。Flask易于安装且使用简单,非常适合小型到中型项目。对于在使用Python进行Web开发领域的新手来说,本教程是一个极好的入门方式。
我们还将回顾构建计算器界面所需的最小HTML、CSS和JavaScript知识,该界面允许进行简单的计算。
安装
首先,在我们继续编码之前,我们需要确保已经安装了Python和Flask。如果你尚未安装Flask,可以通过pip(Python的包安装程序)轻松安装。
步骤1:安装Python
首先,您必须在您的机器上安装Python。您可以从官方Python网站下载。
另请阅读:Python环境搭建
步骤2:安装Flask
打开命令提示符或终端,并运行以下命令:
pip install flask
使用Flask构建基于Web的计算器
我们将首先创建一个简单的Flask应用程序,然后设计计算器的HTML界面并使用CSS进行样式设置。
步骤1:创建Flask应用程序 (app.py)
此文件将存储我们Flask应用程序的主要逻辑。
app.py 代码
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def index():
result = ''
if request.method == "POST":
try:
expression = request.form["display"]
# Evaluate the expression safely using eval
result = eval(expression)
except Exception as e:
result = "Error"
return render_template("index.html", result=result)
if __name__ == "__main__":
app.run(debug=True)
代码解释
让我们首先导入必要的模块,包括:Flask、RenderTemplate和Request。
**index()**函数处理两种HTTP方法协议,包括GET和POST。当用户提交表单(POST请求)时,该函数尝试计算用户输入的表达式并返回该值。
如果在计算过程中出现任何问题(例如,输入错误),则返回“错误”。
最后,我们将计算结果与index.HTML模板一起呈现。
步骤2:创建HTML模板 (index.html)
在你的项目文件夹内创建一个名为“templates”的目录。在“templates”目录中,创建一个名为“index.html”的文件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flask Calculator</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="calculator">
<h1>Flask Calculator</h1>
<form method="POST">
<input type="text" name="display" id="display" value="{{ result }}" readonly>
<div class="buttons">
<button type="button" class="btn" onclick="appendToDisplay('1')">1</button>
<button type="button" class="btn" onclick="appendToDisplay('2')">2</button>
<button type="button" class="btn" onclick="appendToDisplay('3')">3</button>
<button type="button" class="btn operator" onclick="appendToDisplay('+')">+</button>
<button type="button" class="btn" onclick="appendToDisplay('4')">4</button>
<button type="button" class="btn" onclick="appendToDisplay('5')">5</button>
<button type="button" class="btn" onclick="appendToDisplay('6')">6</button>
<button type="button" class="btn operator" onclick="appendToDisplay('-')">-</button>
<button type="button" class="btn" onclick="appendToDisplay('7')">7</button>
<button type="button" class="btn" onclick="appendToDisplay('8')">8</button>
<button type="button" class="btn" onclick="appendToDisplay('9')">9</button>
<button type="button" class="btn operator" onclick="appendToDisplay('*')">×</button>
<button type="button" class="btn" onclick="appendToDisplay('0')">0</button>
<button type="button" class="btn" onclick="clearDisplay()">C</button>
<button type="submit" class="btn operator">=</button>
<button type="button" class="btn operator" onclick="appendToDisplay('/')">÷</button>
</div>
</form>
</div>
<script>
function appendToDisplay(value) {
const display = document.getElementById('display');
display.value += value;
}
function clearDisplay() {
document.getElementById('display').value = '';
}
</script>
</body>
</html>
代码解释
index.html 文件定义了计算器的外观。在这个程序中,它包含一个输入字段用于输入数值和显示结果。
它包含数字按钮和数学运算按钮(+,-,*,/),以及功能键,包括“C”键清除结果和“=”键显示结果。
计算器上的按钮,显示追加和清除功能,使用 JavaScript 函数来处理按钮交互以及显示的更新或清除。
步骤 3:添加样式 (style.css)
在你的项目文件夹内创建一个名为“static”的目录。在“static”目录中,创建一个名为“style.css”的文件来设置计算器的样式。
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator {
width: 100%;
max-width: 600px;
background-color: #333;
padding: 30px;
border-radius: 15px;
box-shadow: 0px 0px 15px 0px #000;
text-align: center;
}
h1 {
color: #fff;
margin-bottom: 20px;
font-size: 36px;
}
#display {
width: 100%;
height: 80px;
font-size: 36px;
text-align: right;
padding: 15px;
margin-bottom: 20px;
border-radius: 10px;
border: none;
background-color: #222;
color: #fff;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 15px;
}
.btn {
width: 100%;
padding: 30px;
font-size: 24px;
border: none;
border-radius: 10px;
background-color: #666;
color: #fff;
cursor: pointer;
transition: background-color 0.2s;
}
.btn:hover {
background-color: #555;
}
.operator {
background-color: #ff9500;
}
.operator:hover {
background-color: #e68a00;
}
.btn:active {
background-color: #444;
}
.operator:active {
background-color: #cc7a00;
}
步骤 4:运行应用程序
现在我们已经拥有了所有必要的文件,你可以在你的项目目录中执行以下命令来运行你的 Flask 应用程序:
python app.py
在你的网页浏览器中访问 http://127.0.0.1:5000/ 来查看你的基于网页的计算器。
输出
现在将 http://127.0.0.1:5000 复制到隐身窗口中粘贴。
现在你将看到这个界面。
现在你可以使用鼠标点击进行计算。