AI 终端模拟器 Python 脚本

✅ 黑白终端风格

✅ 每条信息模拟"打字机式"逐字输出

✅ 屏幕边框 + 图标 + 闪烁感

✅ 可自定义输入内容(即你提前写好的 AI 回应)

依赖: 使用 rich 库(需安装)

安装命令:pip install rich

python 复制代码
import time
import os
import sys
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich.align import Align
from rich.live import Live

# 初始化控制台
console = Console()

# 模拟 AI 输出的内容(你可以自由修改这些文字)
script_lines = [
    "[cyan]> Booting AI SEVEN-SENSE CORE SYSTEM...[/cyan]",
    "[white]> Input received: Audio stream from user.[/white]",
    "[white]> Detecting emotion...[/white]",
    "[green]> Result: Happy 😄 (Confidence: 83%)[/green]",
    "[white]> Generating response...[/white]",
    "[yellow]"你今天似乎很开心哟。"[/yellow]",
]

# 每个字符的输出延迟时间(秒)
TYPE_DELAY = 0.02
LINE_DELAY = 0.8

# UI框架宽度
FRAME_WIDTH = 70

def clear_terminal():
    os.system("cls" if os.name == "nt" else "clear")

def type_line(text: str, delay: float = TYPE_DELAY) -> Text:
    """模拟一行打字输出效果"""
    rendered = Text()
    buffer = ""
    tag_open = False
    for char in text:
        buffer += char
        if char == '[':
            tag_open = True
        elif char == ']':
            tag_open = False
        if not tag_open and char != ']':
            rendered = Text.from_markup(buffer)
            yield rendered
            time.sleep(delay)

def run_ai_terminal(script):
    clear_terminal()
    content = Text()
    with Live(Panel.fit(Align.left(content),
                        title="AI TERMINAL",
                        border_style="white",
                        padding=(1, 2),
                        width=FRAME_WIDTH),
              console=console,
              refresh_per_second=30) as live:

        for line in script:
            for partial in type_line(line):
                content.append(partial[-1])
                live.update(Panel.fit(Align.left(content),
                                      title="AI TERMINAL",
                                      border_style="white",
                                      padding=(1, 2),
                                      width=FRAME_WIDTH))
            content.append("\n")
            live.update(Panel.fit(Align.left(content),
                                  title="AI TERMINAL",
                                  border_style="white",
                                  padding=(1, 2),
                                  width=FRAME_WIDTH))
            time.sleep(LINE_DELAY)

# 运行终端模拟器
run_ai_terminal(script_lines)

🔧 你可以修改哪些部分?

部分 修改建议
script_lines 换成你七感觉醒宣传片里的每段输出内容
TYPE_DELAY 改变打字速度,比如 0.01 更快
LINE_DELAY 每条消息后停留多久
[cyan]...[/cyan] 改变颜色(支持 white, green, yellow, red 等)

🎥 用途:

  • 录屏导出视频片段(用于剪辑宣传片)
  • 嵌入互动项目 Demo,让每个模块都有"终端输出"
  • 现场展示时实时运行,AI仿佛有个中枢"控制台"

GUI界面模拟代码:

python 复制代码
import tkinter as tk
import time
import threading

# ====== 自定义输出内容 ======
script_lines = [
    "Booting AI SEVEN-SENSE CORE SYSTEM...",
    "中文可以吗?",
    "Detecting emotion...",
    "Result: Happy ^ -^ (Confidence: 83%)",
    "Generating response...",
    "haha haha haha"
]

# ====== 初始化窗口 ======
root = tk.Tk()
root.title("AI Terminal")
root.configure(bg="black")
root.geometry("900x500")

# ====== 创建文本框 ======
text_box = tk.Text(
    root, bg="black", fg="white", insertbackground="white",
    font=("Courier", 16), borderwidth=0
)
text_box.pack(fill=tk.BOTH, expand=True)
text_box.configure(state='disabled')

# ====== 打字输出函数 ======
def type_line(line, delay=0.05):
    text_box.configure(state='normal')
    text_box.insert(tk.END, "> ")  # 起始箭头
    for char in line:
        text_box.insert(tk.END, char)
        text_box.see(tk.END)
        root.update()
        time.sleep(delay)
    text_box.insert(tk.END, "\n")
    text_box.configure(state='disabled')

# ====== 模拟AI终端打印脚本 ======
def run_terminal_script():
    time.sleep(0.5)
    for line in script_lines:
        type_line(line)
        time.sleep(0.6)
    # 打印一个停留的提示符
    blinking_cursor()

# ====== 闪烁光标:持续闪动的 '>' ======
def blinking_cursor():
    def blink():
        while True:
            text_box.configure(state='normal')
            text_box.insert(tk.END, "> ")
            text_box.configure(state='disabled')
            root.update()
            time.sleep(0.5)
            text_box.configure(state='normal')
            text_box.delete("end-2c", "end")
            text_box.configure(state='disabled')
            root.update()
            time.sleep(0.5)
    threading.Thread(target=blink, daemon=True).start()

# ====== 开始线程运行脚本输出 ======
threading.Thread(target=run_terminal_script, daemon=True).start()

# ====== 运行窗口主循环 ======
root.mainloop()
相关推荐
大大怪小小酥2 分钟前
kafka如何防止消息丢失
后端
十分钟空间3 分钟前
Go语言实现权重抽奖系统
后端·go
用户25430500960035 分钟前
go 指针接收者和值接收者的区别
后端
JackieLeee6 分钟前
云端业务本地化:告别宕机与弱网,拥抱良好体验——inline-grpc-sdk-go方案解析
后端
吴生43967 分钟前
What?原来Google Play 支持透传,被蒙蔽了这么多年
后端
幺九九九7 分钟前
【实操】k8s安装和初始化集群
后端
无限大68 分钟前
数据结构与算法入门 Day 0:程序世界的基石与密码
后端·算法·程序员
一介输生9 分钟前
Spring Boot 实现权限管理(下)
java·后端
这里有鱼汤11 分钟前
Python 列表不香了?聊聊 NumPy 为什么更快更强!
后端·python
GoGeekBaird12 分钟前
69天探索操作系统-第59天:容器化内部机制 - 深入探讨命名空间实现
后端·操作系统·github