【问题标题】:Text scrolling on video using python使用python在视频上滚动文本
【发布时间】:2021-07-22 11:01:11
【问题描述】:

我正在使用 tkinter 和 PIL 图像播放视频,我想滚动视频底部的文本,如新闻滚动.. 多种语言,我该如何实现它.. 请有人给我一个想法我是 GUI 新手。

用于滚动:

import tkinter as tk
import time

root = tk.Tk()

# width=width chars, height=lines text
text = tk.Text(root, width=20, height=1,fg = 'white', bg='black')
text.pack()

# use a proportional font to handle spaces correctly
text.config(font=('Devlys 010', 24, 'bold'))

s1 = "fcuk fVdV\n;k=k djuk n.Muh; vijk/k gSA"
# pad front and end with 20 spaces
s2 = ' ' * 20
s = s2 + s1 + s2

for k in range(len(s)):
    # use string slicing to do the trick
    ticker_text = s[k:k+20]
    text.insert("1.1", ticker_text)
    root.update()
    # delay by 0.15 seconds
    time.sleep(0.15)

root.mainloop()

我试过这样。我可以让文本滚动,但我无法将它嵌入到视频中。

视频:

from tkinter import *
from tkvideo import tkvideo

root = Tk()
my_label = Label(root)
my_label.pack()
player = tkvideo("video.mp4", my_label, loop = 1, size = (1280,720))
player.play()

root.mainloop()

【问题讨论】:

  • 把播放视频的代码也贴出来
  • 请立即查看..
  • 由于您使用外部模块播放视频,因此无法直接将文本嵌入视频中。建议在滚动文本的视频标签顶部放置另一个Label
  • 我只遇到了这个问题,我无法在视频顶部创建标签。请问我可以得到任何参考吗?

标签: python python-2.7 tkinter video-streaming


【解决方案1】:

由于您使用外部模块tkvideo播放视频,您无法直接在视频中嵌入文字。

建议创建另一个标签并放置在视频顶部,如下所示:

import tkinter as tk
from tkvideo import tkvideo

root = tk.Tk()

# for video
my_label = tk.Label(root, bd=0)
my_label.pack()

# for scrolling text
scroll_label = tk.Text(root, fg="white", bg="black", width=20, height=1, bd=0)
scroll_label.place(relx=0.5, rely=1, y=-10, anchor="s")

player = tkvideo("video.mp4", my_label, loop=1, size=(640,480))
player.play()

# the scrolling text
pad = " " * 20
txt = pad + "fcuk fVdV\n;k=k djuk n.Muh; vijk/k gSA" + pad

def update_text(n=0):
    scroll_label.delete("1.0", "end")
    scroll_label.insert("1.0", txt[n:n+20])
    root.after(150, update_text, (n+1)%len(txt))

update_text() # start scrolling text
root.mainloop()

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多