如何在 Tkinter 中播放视频文件?

编程入门 行业动态 更新时间:2024-10-24 16:32:07
本文介绍了如何在 Tkinter 中播放视频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法播放AVI、MP4 等?

Is there a way to play video files like AVI, MP4, etc.?

我尝试使用 PyMedia,但显然它只适用于 Pygame.

I tried using PyMedia, but apparently it only works with Pygame.

我的问题的解决方案是什么?

What is the solution to my problem?

推荐答案

你可以使用 python-gstreamer 来播放视频(这对我来说适用于 Linux,但它也适用于 Windows).这需要 python-gstreamer 和 python-gobject,我会推荐您可以使用此多合一安装程序.

You could use python-gstreamer for playing videos (this works for me on Linux, but it should also work on Windows). This requires python-gstreamer and python-gobject, I would recommend you to use this all-in-one installer.

代码如下:

import os import sys import Tkinter as tkinter import gobject import gst def on_sync_message(bus, message, window_id): if not message.structure is None: if message.structure.get_name() == 'prepare-xwindow-id': image_sink = message.src image_sink.set_property('force-aspect-ratio', True) image_sink.set_xwindow_id(window_id) gobject.threads_init() window = tkinter.Tk() window.geometry('500x400') video = tkinter.Frame(window, bg='#000000') video.pack(side=tkinter.BOTTOM,anchor=tkinter.S,expand=tkinter.YES,fill=tkinter.BOTH) window_id = video.winfo_id() player = gst.element_factory_make('playbin2', 'player') player.set_property('video-sink', None) player.set_property('uri', 'file://%s' % (os.path.abspath(sys.argv[1]))) player.set_state(gst.STATE_PLAYING) bus = player.get_bus() bus.add_signal_watch() bus.enable_sync_message_emission() bus.connect('sync-message::element', on_sync_message, window_id) window.mainloop()

更多推荐

如何在 Tkinter 中播放视频文件?

本文发布于:2023-07-11 09:20:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视频文件   如何在   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!