AI 终端模拟器 Python 脚本

85 阅读2分钟

✅ 黑白终端风格
✅ 每条信息模拟“打字机式”逐字输出
✅ 屏幕边框 + 图标 + 闪烁感
✅ 可自定义输入内容(即你提前写好的 AI 回应)

依赖: 使用 rich 库(需安装)
安装命令:pip install rich

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界面模拟代码:

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()