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()
相关推荐
草梅友仁1 分钟前
AI 图片文字翻译与视频字幕翻译工具推荐 | 2025 年第 23 周草梅周报
开源·github·aigc
冒泡的肥皂13 分钟前
强大的ANTLR4语法解析器入门demo
后端·搜索引擎·编程语言
IT_陈寒38 分钟前
Element Plus 2.10.0 重磅发布!新增Splitter组件
前端·人工智能·后端
有梦想的攻城狮1 小时前
spring中的@RabbitListener注解详解
java·后端·spring·rabbitlistener
Java水解1 小时前
MySQL DQL全面解析:从入门到精通
后端·mysql
Aurora_NeAr1 小时前
Apache Spark详解
大数据·后端·spark
程序员岳焱1 小时前
Java 程序员成长记(二):菜鸟入职之 MyBatis XML「陷阱」
java·后端·程序员
hello早上好1 小时前
BeanFactory 实现
后端·spring·架构
我命由我123451 小时前
Spring Boot 项目集成 Redis 问题:RedisTemplate 多余空格问题
java·开发语言·spring boot·redis·后端·java-ee·intellij-idea
面朝大海,春不暖,花不开1 小时前
Spring Boot消息系统开发指南
java·spring boot·后端