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 分钟前
【SpringBoot】从环境准备到创建SpringBoot项目的全面解析.
java·spring boot·后端
caihuayuan57 小时前
升级element-ui步骤
java·大数据·spring boot·后端·课程设计
Kookoos8 小时前
ABP vNext + EF Core 实战性能调优指南
数据库·后端·c#·.net·.netcore
揣晓丹9 小时前
JAVA实战开源项目:健身房管理系统 (Vue+SpringBoot) 附源码
java·vue.js·spring boot·后端·开源
豌豆花下猫11 小时前
Python 3.14 新特性盘点,更新了些什么?
后端·python·ai
caihuayuan511 小时前
Vue生命周期&脚手架工程&Element-UI
java·大数据·spring boot·后端·课程设计
鸿蒙布道师12 小时前
ChatGPT深度研究功能革新:GitHub直连与强化微调
人工智能·深度学习·神经网络·自然语言处理·chatgpt·数据挖掘·github
陈苏同学12 小时前
从 Git 到 GitHub - 使用 Git 进行版本控制 - Git 常用命令
git·github
碎梦归途13 小时前
23种设计模式-行为型模式之模板方法模式(Java版本)
java·开发语言·jvm·设计模式·软考·模板方法模式·软件设计师
白总Server13 小时前
微软系统 红帽系统 网络故障排查:ping、traceroute、netstat
linux·运维·服务器·microsoft·中间件·架构·github