Is it possible in PyQt4 to embed a video via mpylayer into a QWidget (or into a subclass of it). If so, could you provide a minimal working example.
For a complete example of a Qt Widget that embeds MPlayer, try qmpwidget.
But here's a minimal PyQt demo to get you started:
import mpylayer
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.container = QtGui.QWidget(self)
self.container.setStyleSheet('background: black')
self.button = QtGui.QPushButton('Open', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.container)
layout.addWidget(self.button)
self.mplayer = mpylayer.MPlayerControl(
'mplayer', ['-wid', str(self.container.winId())])
def handleButton(self):
path = QtGui.QFileDialog.getOpenFileName()
if not path.isEmpty():
self.mplayer.loadfile(unicode(path))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
(NB: this demo has only been tested on Linux)
You have to get the handle (id) of the widget → http://qt-project.org/doc/qt-4.8/qwidget.html#winId
And pass it to the -wid option of the MPlayer.
I can't provide you an example with Qt, simply because I don't know Qt, but I already wrote an MplayerCtrl for wxPython: https://bitbucket.org/dav1d/mplayerctrl
Relevant Code: https://bitbucket.org/dav1d/mplayerctrl/src/c680a1d99ad2/MplayerCtrl.py#cl-873
Related
I am trying to translate my small application written in pyside2/pyqt5 to several languages, for example, Chinese. After googling, I managed to change the main window to Chinese after select from the menu -> language -> Chinese. However, the pop up dialog from menu -> option still remains English version. It seems the translation info is not transferred to the dialog. How do I solve this?
Basically, I build two ui files in designer and convert to two python files:One mainui.py and one dialogui.py. I then convert the two python file into one *.ts file using
pylupdate5 -verbose mainui.py dialogui.py -ts zh_CN.ts
after that, in linguist input the translation words. I can see the items in the dialog, which means this information is not missing. Then I release the file as zh_CN.qm file. All this supporting file I attached below using google drive.
Supporting files for the question
The main file is as
import os
import sys
from PySide2 import QtCore, QtGui, QtWidgets
from mainui import Ui_MainWindow
from dialogui import Ui_Dialog
class OptionsDialog(QtWidgets.QDialog,Ui_Dialog):
def __init__(self,parent):
super().__init__(parent)
self.setupUi(self)
self.retranslateUi(self)
class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.actionConfigure.triggered.connect(self.showdialog)
self.actionChinese.triggered.connect(self.change_lang)
def showdialog(self):
dlg = OptionsDialog(self)
dlg.exec_()
def change_lang(self):
trans = QtCore.QTranslator()
trans.load('zh_CN')
QtCore.QCoreApplication.instance().installTranslator(trans)
self.retranslateUi(self)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
ret = app.exec_()
sys.exit(ret)
I think it should be a typical task because almost no application will only have a mainwindow.
You have to overwrite the changeEvent() method and call retranslateUi() when the event is of type QEvent::LanguageChange, on the other hand the QTranslator object must be a member of the class but it will be deleted and it will not exist when the changeEvent() method is called.
Finally assuming that the Language menu is used to establish only translations, a possible option is to establish the name of the .qm as data of the QActions and to use the triggered method of the QMenu as I show below:
from PySide2 import QtCore, QtGui, QtWidgets
from mainui import Ui_MainWindow
from dialogui import Ui_Dialog
class OptionsDialog(QtWidgets.QDialog,Ui_Dialog):
def __init__(self,parent):
super().__init__(parent)
self.setupUi(self)
def changeEvent(self, event):
if event.type() == QtCore.QEvent.LanguageChange:
self.retranslateUi(self)
super(OptionsDialog, self).changeEvent(event)
class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.m_translator = QtCore.QTranslator(self)
self.actionConfigure.triggered.connect(self.showdialog)
self.menuLanguage.triggered.connect(self.change_lang)
# set translation for each submenu
self.actionChinese.setData('zh_CN')
#QtCore.Slot()
def showdialog(self):
dlg = OptionsDialog(self)
dlg.exec_()
#QtCore.Slot(QtWidgets.QAction)
def change_lang(self, action):
QtCore.QCoreApplication.instance().removeTranslator(self.m_translator)
if self.m_translator.load(action.data()):
QtCore.QCoreApplication.instance().installTranslator(self.m_translator)
def changeEvent(self, event):
if event.type() == QtCore.QEvent.LanguageChange:
self.retranslateUi(self)
super(MainWindow, self).changeEvent(event)
if __name__=='__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
ret = app.exec_()
sys.exit(ret)
I am learning pyqt5 and somehow I can't use fcitx in the text box created by QTextEdit or QLineEdit although fcitx works normally with other Qt apps like goldendict or kate. But later I found out that fcitx also doesn't work with another Qt app named Retext which uses Qt 5.10. Maybe this has something to do with the latest version of Qt or so I think.
Here's my code, just a simple textbox and nothing else:
import PyQt5.QtWidgets as QtWidgets
import sys
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.text = QtWidgets.QTextEdit()
self.initUI()
def initUI(self):
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(self.text)
self.setLayout(vbox)
self.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Thanks to #ekhumoro, I know how to fix this. Just enable input method by adding this line to the __init__ function:
self.setAttribute(Qt.WA_InputMethodEnabled)
Then do this:
cp /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/fcitxplatforminputcontextplugin.so ~/.local/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforminputcontexts
sudo chmod +x ~/.local/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforminputcontexts/fcitxplatforminputcontextplugin.so
I had a little research regarding this question, but i couldn't find any simple answer or tutorial to do this.
I'm trying to find a widget, that will make transition between pages using QPushButton. I have heard of QStackWidget, But i'm not exactly sure how to use it, I have found many tutorials, but all of them were hard to understand.
So for example, i have a window class:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
How can i add QStackWidget to this class? (Without any layouts)
If i do so, how can i switch to specific page using QPushButton? (With adding multiple widgets in one index)
extra: is it possible to add some kind of effect on transition? like fading, etc.
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.stacked_widget = QtGui.QStackedWidget()
self.button = QtGui.QPushButton("Next")
self.button.clicked.connect(self.__next_page)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.stacked_widget)
layout.addWidget(self.button)
widget = QtGui.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.stacked_widget.addWidget(QtGui.QLabel("Page 1"))
self.stacked_widget.addWidget(QtGui.QLabel("Page 2"))
self.stacked_widget.addWidget(QtGui.QLabel("Page 3"))
def __next_page(self):
idx = self.stacked_widget.currentIndex()
if idx < self.stacked_widget.count() - 1:
self.stacked_widget.setCurrentIndex(idx + 1)
else:
self.stacked_widget.setCurrentIndex(0)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
I've been learning python recently and now I wanted to (try to) create my first real application, a subtitle player for Linux. So far I've been using the Greenfish subtitle player, which is aimed at Windows users and not properly working in Linux.
I wanted to create the application in qt, since I discovered that transparent windows are not possible in tkinter, but if anybody knows a better framework please suggest!
Now before starting I've been researching the web for several hours to discover how to get my application to show over a full screened flash video and it seems like this is not possible. However the aforementioned GF subtitle player manages to do so in Windows, but not in Linux(maybe it's also because it's running through wine).
So my question is it possible to create a transparent application that remains over a fullscreened flash video and if so, could you point me in the right direction?
Thanks in advance.
edit:
here some example code I've been trying. The window produced by this piece of code does not stay above a fullscreened video
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
mywindow.show()
Update for PyQt5 pip install PyQt5
import sys
from PyQt5 import QtGui, QtCore, uic
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowFlags(
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
QtCore.QSize(220, 32),
QtWidgets.qApp.desktop().availableGeometry()
))
def mousePressEvent(self, event):
QtWidgets.qApp.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
mywindow = MainWindow()
mywindow.show()
app.exec_()
The example code below will create a centred, frameless window that should stay on top of all other windows on Linux (you can click on the window to close it).
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowFlags(
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.setGeometry(QtGui.QStyle.alignedRect(
QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
QtCore.QSize(220, 32),
QtGui.qApp.desktop().availableGeometry()))
def mousePressEvent(self, event):
QtGui.qApp.quit()
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
I'm a beginner in PyQt and I have an image known as add.gif. I need to put this image in a QPushButton but I don't know how.
Example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(24,24))
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Here is the same example from #NorthCat but for PyQt5:
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(200,200))
layout = QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Assuming pyqt supports gif pictures, this should work
icon = QtGui.QPixmap('add.gif')
button = QtGui.QPushButton()
button.setIcon(icon)
QPushButton
Push buttons display a textual label, and optionally a small icon.
These can be set using the constructors and changed later using
setText() and setIcon(). If the button is disabled, the appearance of
the text and icon will be manipulated with respect to the GUI style to
make the button look "disabled".