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()
相关推荐
程序员爱钓鱼3 分钟前
Go语言泛型-泛型约束与实践
前端·后端·go
寻月隐君3 分钟前
保姆级教程:Zsh + Oh My Zsh 终极配置,让你的 Ubuntu 终端效率倍增
linux·后端·命令行
程序员爱钓鱼6 分钟前
Go语言泛型-泛型对代码结构的优化
后端·google·go
这里有鱼汤11 分钟前
“对象”?对象你个头!——Python世界观彻底崩塌的一天
后端·python
RainbowSea13 分钟前
跨域问题(Allow CORS)解决(3 种方法)
java·spring boot·后端
鼠鼠我呀237 分钟前
【设计模式04】单例模式
单例模式·设计模式
小林up1 小时前
github push:ssh: connect to host github.com port 22
运维·ssh·github
sniper_fandc2 小时前
SpringBoot系列—入门
java·spring boot·后端
Leinwin8 小时前
微软开源GitHub Copilot Chat,AI编程领域迎新突破
microsoft·github·copilot
Piper蛋窝8 小时前
深入 Go 语言垃圾回收:从原理到内建类型 Slice、Map 的陷阱以及为何需要 strings.Builder
后端·go