jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
550 stars 68 forks source link

Keybinding don't work in Pyside6 (PyQt). #200

Closed Senkai350 closed 1 year ago

Senkai350 commented 2 years ago

When i try to use keybindings with Pyside they just don't work, but without pyside all works perfect, why? My code:

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('res/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_())
Axel-Erfurt commented 2 years ago

I've made a test in Linux with PyQt6, this works. In PyQt6 you must use app.exec() not app.exec()_

import os
#os.add_dll_directory(os.getcwd())
import mpv
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
mpvfolderpath = f"mpv.net/portable_config/"
import sys

class Test(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setGeometry(100, 100, 400, 260)
        self.container = QWidget(self)
        self.setCentralWidget(self.container)

        self.player = mpv.MPV(wid=str(int(self.container.winId())),
                         log_handler=print,
                         loglevel='debug',
                         input_default_bindings=True,
                         input_vo_keyboard=True)

        @self.player.on_key_press('f')
        def my_f_binding():
            print("f работает!")
        self.player.play('test.mp4')

app = QApplication(sys.argv)

import locale
locale.setlocale(locale.LC_NUMERIC, 'C')
win = Test()
win.show()
sys.exit(app.exec())