microdot micropython下web框架快速手册

主要使用异步+装饰器的方式

from microdot import Microdot, send_file

创建服务app=Microdot()

启动服务app.run(debug=True)

创建一个网页链接需要使用 装饰器 @app.route(‘/’)

再创建对应函数

async def index(request):
return send_file(‘g.html’)

创建static目录下静态资源访问需要<path:path>
@app.route(‘/static/<path:path>’)
async def static(request, path):
if ‘..’ in path:
# directory traversal is not allowed
return ‘Not found’, 404
return send_file(‘static/’ + path)

GET 和POST 访问需要
@app.route(‘/gp’, methods=[‘GET’,’POST’])
其中get抓取 req.args.get ();post抓取 req.form.get()
async def gp_index(req):
code = req.form.get(‘code’)

回传return
return 1 ,2,3
1可以直接信息,或者send_file发文件,
2是代码 200 302 404 500
3为响应头字典{‘Content-Type’: ‘text/plain; charset=gb2312’}

就是为了简单 所以其他可以暂时不用



app = Microdot()


@app.route('/')
async def index(request):
    return send_file('g.html')


@app.route('/static/<path:path>')
async def static(request, path):
    if '..' in path:
        # directory traversal is not allowed
        return 'Not found', 404
    return send_file('static/' + path)

@app.route('/gp', methods=['GET'])
async def gp_index(req):
    code = req.args.get('code')
    print(code)
    bk=gp_search(code)
    return bk,200, {'Content-Type': 'text/plain; charset=gb2312'}

@app.route('/gpx', methods=['POST'])
async def gp_index(req):
    code = req.form.get('code')
    print(code)
    bk=gp_search(code)
    return bk,200, {'Content-Type': 'text/plain; charset=gb2312'}
    
app.run(debug=True)

创建AP热点
import network
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)

发表在 None | 留下评论

页面 添加简要查询 实现输入后查询修改

HTML页面

<style>
    /* 固定定位的搜索框容器 */
 .fixed-top-search {
      position: fixed; /* 固定定位,悬浮不随滚动移动 */
      top: 0;          /* 锁定到页面顶部 */
      right: 20px;     /* 水平靠右 20px,可改 left: 20px 靠左,或 left: 50%; transform: translateX(-50%) 居中 */
      z-index: 9999;   /* 最高层级,避免被遮挡 */
      padding: 10px 0; /* 上下内边距,让搜索框更美观 */
      width: 320px;    /* 搜索框宽度,可调整 */
    }
</style>


<!-- 悬浮搜索框 -->
    <div class="fixed-top-search">
        <div class="input-group">
            <input type="text" class="form-control search" placeholder="请输入搜索内容" aria-label="搜索">
            <span class="input-group-addon">
                输入名称
            </span>
        </div>
    </div>

JS处理

function clearHighlight() {
  // 找到所有高亮的span,直接替换为文本内容,避免unwrap的转义问题
  $('span.bg-danger').each(function() {
    var $this = $(this);
    // console.log($this.text());
    // 替换span为其文本内容(纯文本,无HTML解析)
    // $this.replaceWith($this.text());
    $this.parent().text($this.parent().text())
  });
}

// 搜索字符串并添加 bg-danger 类
function searchAndHighlight(keyword) {

  if (!keyword.trim()) return;

  // 遍历所有文本节点(排除 script、style 标签)
  $('body :not(script, style)').contents().each(function() {
    // 只处理文本节点
    if (this.nodeType === Node.TEXT_NODE) {
      var text = $(this).text();
    //   text=text.replace(/"/g,"");
      const regex = new RegExp(`(${keyword})`, 'gi');
      
      // 匹配到关键词时,用 span 包裹
      if (regex.test(text)) {
        
        const newHtml = text.replace(regex, '<span class="bg-danger">$1</span>');
        $(this).replaceWith(newHtml);
        // $(this).addClass('bg-danger')
      }
    }
  });
}

$(".search").on("input", function(){
  var se = $(this).val().trim(); // 去除首尾空格,避免无效查询
  if (se) {
    clearHighlight();
    searchAndHighlight(se); // 有内容则高亮
  }
});
发表在 None | 留下评论

Thinkphp 前端页面获取get值

{:request()->get(‘id/d’)}
调用上面的函数可以获得 get类型的id并初始化为数字形式

发表在 None | 留下评论

python打包 pyinstaller PIL

带logo 无黑框打包

pyinstaller -F -w -i logo.ico x.py

无logo 无黑框

pyinstaller -F -w x.py

普通打包 一个 单文件exe 有黑框

pyinstaller -F x.py

普通打包 非单文件exe有黑框

pyinstaller x.py

PIL 打包坑点 需要指定 字体

#否则会失败

options = {
    'module_width': 0.24,
    'module_height':6.0,
    'font_size': 8,
    'text_distance': 3,
    'quiet_zone':4,
    'font_path': r'C:\Windows\Fonts\arial.ttf',  # Arial字体(纯数字/英文)
}
发表在 None | 留下评论

Tkinter 输入回车返回

import tkinter as tk
from tkinter import messagebox

def submit_content(event=None):
    """
    提交输入框内容的核心函数
    event 参数:绑定键盘事件时会自动传入,需保留(设为默认None兼容按钮点击)
    """
    # 获取输入框内容并去除首尾空格
    content = entry.get().strip()
    
    # 空值校验
    if not content:
        messagebox.warning("提示", "输入框不能为空!")
        # 清空输入框并让输入框重新获得焦点
        entry.delete(0, tk.END)
        entry.focus()
        return
    
    # 处理提交逻辑(这里仅做演示,你可以替换为自己的业务代码)
    messagebox.showinfo("提交成功", f"你提交的内容是:{content}")
    
    # 提交后清空输入框并保持焦点,方便继续输入
    entry.delete(0, tk.END)
    entry.focus()

# 创建主窗口
root = tk.Tk()
root.title("回车提交输入框内容")
root.geometry("400x200")

# 创建输入框
entry = tk.Entry(
    root,
    font=("Arial", 14),
    width=30
)
entry.pack(pady=30)

# 核心:绑定回车键事件(<Return> 对应回车键)
entry.bind("<Return>", submit_content)

# 可选:添加提交按钮(兼容鼠标点击场景)
submit_btn = tk.Button(
    root,
    text="提交",
    font=("Arial", 12),
    command=submit_content  # 直接调用函数,无需传参(因为event设为默认None)
)
submit_btn.pack()

# 让输入框默认获得焦点,打开窗口即可直接输入
entry.focus()

# 运行主循环
root.mainloop()
发表在 None | 留下评论