我使用庫https://github.com/jaseg/python-mpv來控制 mpv 播放器,但是當它與 pyside6 一起使用時,鍵系結不起作用(播放器不完全接受輸入)。我究竟做錯了什么?還是嵌入pyside6時無法使用它們?(如果我在沒有嵌入的情況下使用相同的引數運行播放器,一切正常)
import os
os.add_dll_directory(os.getcwd())
import mpv
from PySide6.QtWidgets import *
from PySide6.QtCore import *
mpvfolderpath = f"mpv.net/portable_config/"
import sys
class Test(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.container = QWidget(self)
self.setCentralWidget(self.container)
self.container.setAttribute(Qt.WA_DontCreateNativeAncestors)
self.container.setAttribute(Qt.WA_NativeWindow)
player = mpv.MPV(wid=str(int(self.container.winId())),
vo="gpu", # You may not need this
log_handler=print,
loglevel='debug',
input_default_bindings=True,
input_vo_keyboard=True)
@player.on_key_press('f')
def my_f_binding():
print("f работает!")
player.play('test.mp4')
app = QApplication(sys.argv)
# This is necessary since PyQT stomps over the locale settings needed by libmpv.
# This needs to happen after importing PyQT before creating the first mpv.MPV instance.
import locale
locale.setlocale(locale.LC_NUMERIC, 'C')
win = Test()
win.show()
sys.exit(app.exec_())
uj5u.com熱心網友回復:
如果未處理鍵盤(在我的測驗中,僅在滑鼠未懸停視頻時發生),則鍵事件將傳播到 Qt 視窗。這意味著我們可以在keyPressEvent()覆寫中處理這些事件,然后創建一個適當的 mpv 命令,該命令已經映射到該keypress()函式。顯然,對播放器的參考必須存在,因此您需要將其設為實體屬性。
對于標準文字鍵,通常使用事件的 就足夠了text(),但對于其他鍵(例如箭頭),您需要將事件映射到 mpv 的鍵名。使用字典當然更簡單:
MpvKeys = {
Qt.Key.Key_Backspace: 'BS',
Qt.Key.Key_PageUp: 'PGUP',
Qt.Key.Key_PageDown: 'PGDWN',
Qt.Key.Key_Home: 'HOME',
Qt.Key.Key_End: 'END',
Qt.Key.Key_Left: 'LEFT',
Qt.Key.Key_Up: 'UP',
Qt.Key.Key_Right: 'RIGHT',
Qt.Key.Key_Down: 'DOWN',
# ...
}
class Test(QMainWindow):
def __init__(self, parent=None):
# ...
self.player = mpv.MPV(...)
def keyPressEvent(self, event):
# look up for the key in our mapping, otherwise use the event's text
key = MpvKeys.get(event.key(), event.text())
self.player.keypress(key)
注意:在我的測驗中,我必須使用該vo='x11'標志來正確嵌入視窗,并且osc=True還需要使用本機 OSD。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/432039.html
標籤:Python 用户界面 语言环境 pyside6 mpv
