窗口

本系列前章介绍,叫号器的显示端,完成了视频音频的形成和传输的介绍。本章节开始定制小窗口。
最终实现,处于桌面最前端,发送指令,集合前篇即可完成:
- 处理本地text.txt更新,随之被rtsp采集
ffmpeg 参数:-vf "drawtext=textfile=text.txt:reload=1:x=10:y=10:fontsize=20:fontcolor=red:fontfile=simhei.ttf"
- 播报语音 透过虚拟声卡发送到rtsp
ffmpeg 参数:-f dshow -i audio="CABLE Output (VB-Audio Virtual Cable)" -acodec aac -b:a 128k
集成运行的要点
- 指定声卡为虚拟声卡。
- 运行mediamtx免费的流媒体服务器,
- 本网络有一台可接受投屏,并支持rtsp流的显示设备android推荐airReciever
补充:
ffmpeg进程需要常驻后台,有断开检测。重启后需要再次推送。
推流 的python任务,需要检测播放状态。dlna是播放就结束。airplay是播放后等待。需要选择一种。
主要窗体代码:
bash
import tkinter as tk
import pyttsx3
from tkinter import messagebox
import asyncio
# 数值验证函数
def say(text):
eng=pyttsx3.init()
eng.say(text)
eng.runAndWait()
update_status(f"{saytext}-播放结束")
def validate_num_input(text):
if text == "": # 允许清空输入框
return True
try:
int(text)
return True
except ValueError:
return False
# 数值操作函数
def increase():
try:
current = int(num_entry.get())
except ValueError:
current = 0
num_entry.delete(0, tk.END)
num_entry.insert(0, str(current + 1))
def decrease():
try:
current = int(num_entry.get())
except ValueError:
current = 0
num_entry.delete(0, tk.END)
num_entry.insert(0, str(current - 1))
# 数据发送函数
def send_data():
global saytext
text = text_entry.get()
number = num_entry.get()
saytext=text.replace('n',number)
update_status(f"{saytext}-准备播放")
def update_status(new_status):
# 更新状态栏的文本内容
global status_bar
status_bar.config(text=new_status)
# 刷新界面以立即显示更新
status_bar.update_idletasks()
# messagebox.showinfo(
# "发送结果",
# f"文本内容:{text}\n数字数值:{number}\n说出文本:{saytext}",
# parent=root
# )
# 创建主窗口
root = tk.Tk()
root.title("控制面板")
root.geometry("250x150")
# 输入验证注册
validate_cmd = root.register(validate_num_input)
# 控件创建
tk.Label(root, text="文本输入:").grid(row=0, column=0, padx=1, pady=1, sticky="w")
text_entry = tk.Entry(root)
text_entry.grid(row=0, column=1, padx=1, pady=1, columnspan=2)
default_text = "请n号就诊"
text_entry.insert(tk.END, default_text)
tk.Label(root, text="数字输入:").grid(row=1, column=0, padx=1, pady=1, sticky="w")
num_entry = tk.Entry(
root,
validate="key",
validatecommand=(validate_cmd, "%P")
)
num_entry.grid(row=1, column=1, padx=2, pady=2, columnspan=2)
num_entry.insert(0, "0") # 设置初始值
# 按钮区域
button_frame = tk.Frame(root)
button_frame.grid(row=2, column=0, columnspan=3, pady=10)
tk.Button(button_frame, text="-", command=decrease).pack(side="left", padx=1)
tk.Button(button_frame, text="+", command=increase).pack(side="left", padx=1)
button=tk.Button(button_frame, text="发送", command=send_data,bg="white")
button.pack(side="left", padx=1,pady=2)
status_bar = tk.Label(root, text="初始状态")
status_bar.grid(row=3, column=1, padx=1, pady=1, sticky="w")
# 模拟一个异步函数
saytext=None
async def async_task():
global saytext
while True:
# print("异步任务正在运行...")
await asyncio.sleep(1)
if saytext:
say(saytext)
saytext=None
# 将异步任务与 Tkinter 集成
def run_async_task(root):
loop = asyncio.get_event_loop()
task = loop.create_task(async_task())
def update():
loop.stop()
loop.run_forever()
root.after(100, update)
root.after(100, update)
run_async_task(root)
root.wm_attributes("-topmost", 1)
root.mainloop()