I want to have a table widget side by side to a video widget and a webview widget. The video and webview widget to be stacked vertically.
I tried to first have table widget and video widget side by side but the video widget is being hidden by the table widget. I'm able to hear audio so the video seems to be running but just doesn't seem to be displaying the video part.
What is wrong in the code?
Pasting the sample code. I'm yet to add the web widget.
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QTableWidget,QVBoxLayout,
QTableWidgetItem, QLabel, QHBoxLayout,QGridLayout)
class Window(QWidget):
def __init__(self,):
super().__init__()
table1 = QTableWidget()
table1.setRowCount(2)
table1.setColumnCount(2)
table1.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
table1.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
table1.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
table1.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
self.VideoWidget = QVideoWidget()
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.player.setMedia(QMediaContent(QUrl.fromLocalFile("test.mp4")))
self.player.play()
self.player.setVideoOutput(self.VideoWidget)
self.layout = QHBoxLayout()
self.layout.addWidget(table1)
self.layout.addWidget(self.VideoWidget)
self.setLayout(self.layout)
self.move(0,0)
self.resize(320, 240)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
app.exec_()
I was able to solve the problem by using QSplitter.
The code is as below:
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QTableWidget,QVBoxLayout,
QTableWidgetItem, QHBoxLayout,QSplitter,QGroupBox)
from PyQt5.QtWebKit import *
from PyQt5.QtWebKitWidgets import *
class Window(QWidget):
def __init__(self,):
super().__init__()
self.v_layout = QVBoxLayout(self)
self.splitter = QSplitter(QtCore.Qt.Horizontal)
self.left = QGroupBox('Left')
self.table1 = QTableWidget()
self.table1.setRowCount(2)
self.table1.setColumnCount(2)
self.table1.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
self.table1.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
self.table1.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
self.table1.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
self.left_layout = QVBoxLayout(self.left)
self.left_layout.addWidget(self.table1)
self.right = QGroupBox('Right')
self.VideoWidget = QVideoWidget()
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.player.setMedia(QMediaContent(QUrl.fromLocalFile("test.mp4")))
self.player.play()
self.player.setVideoOutput(self.VideoWidget)
self.webview = QWebView()
url = "https://www.google.com"
self.webview.load(QUrl(url))
self.right_layout = QVBoxLayout(self.right)
self.right_layout.addWidget(self.webview)
self.right_layout.addWidget(self.VideoWidget)
self.splitter.addWidget(self.left)
self.splitter.addWidget(self.right)
self.v_layout.addWidget(self.splitter)
self.resize(840,680)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
app.exec_()
Related
I am trying to edit the text in 'QTextEdit' but I got the error which is in the title above. I know that I shouldn't edit the widgets in the main app from another thread and I searched for some answers, But I didn't get the full answer that I want
This is a sample that produce the same error that I have
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore
import sys
import time
import threading
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.move(50, 220)
self.resize(500, 500)
self.text_edit = QTextEdit(self)
self.text_edit.resize(200, 200)
self.btn = QPushButton('start', self)
self.btn.move(10, 250)
self.btn.clicked.connect(lambda x : self.thread_creator(self.test))
self.btn2 = QPushButton('print', self)
self.btn2.move(10, 290)
self.btn2.clicked.connect(lambda x : print('dad'))
def test(self):
for i in range(99):
self.text_edit.setText(f'dad {i}')
time.sleep(0.1)
def thread_creator(self, function):
self.thread = threading.Thread(target=function)
self.thread.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
ex.show()
sys.exit(app.exec_())
I am trying to simulate save functionality(Tool-Save) using Qtest but not able to find any pointer
#!/usr/bin/python
from PySide2.QtCore import Qt, QEvent
from PySide2.QtWidgets import QApplication
from PySide2.QtGui import (QTextCharFormat, QIcon, QKeySequence,
QBrush, QColor, QTextDocument, QFont,
QTextCursor, QTextBlockFormat, QFontDatabase)
from PySide2.QtWidgets import (QPlainTextEdit, QSizePolicy, QApplication, QLabel, QGridLayout, QMessageBox, QToolBar, QTextEdit, QCheckBox, QAction,
QTableWidget, QTableWidgetItem, QHeaderView, QMenu,
QWidget)
from PySide2.QtCore import Qt, QSize, QRegExp, QFile, QTextStream, QRect
from PySide2.QtWidgets import (QMainWindow, QVBoxLayout,
QPlainTextEdit, QGridLayout, QGroupBox,
QListWidget, QHBoxLayout, QLabel, QLineEdit,
QMenuBar, QPushButton, QMessageBox, QDialog,
QTextEdit, QVBoxLayout, QWidget, QFormLayout,
QCheckBox, QDialogButtonBox,
QTableWidget, QTableWidgetItem, QHeaderView)
import sys
class Window(QMainWindow):
def __init__(self, app):
super().__init__()
def create_widget(self):
self.create_menu()
self.plain_textedit = QPlainTextEdit()
self.plain_textedit.setMouseTracking(True)
self.plain_textedit.viewport().installEventFilter(self)
main_layout = QVBoxLayout()
main_layout.addWidget(self.toolbar)
main_layout.addWidget(self.plain_textedit)
self.window = QWidget()
self.window.setLayout(main_layout)
self.setCentralWidget(self.window)
def create_menu(self):
"""
create menu bar into window
"""
self.toolbar = QToolBar()
self.toolbar.setStyleSheet("QToolBar {background: #ff8000}")
self.addToolBar(Qt.TopToolBarArea, self.toolbar)
menu = QMenu("&Tool", self)
self.menuBar().addMenu(menu)
self.actionSave = QAction(QIcon.fromTheme('document-save'),
"&Save", self, shortcut=QKeySequence(Qt.CTRL+Qt.Key_W),
triggered=self.save_data, enabled=True)
self.toolbar.addAction(self.actionSave)
menu.addAction(self.actionSave)
menu.addSeparator()
def save_data(self):
pass
def eventFilter(self, obj, event):
if obj is self.plain_textedit.viewport():
print ("plain_1")
def main():
app = QApplication([])
window = Window(app)
window.create_widget()
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())
#!/usr/bin/python
import os
env = os.environ
import unittest
import logging
from PySide2.QtWidgets import QApplication
from PySide2.QtTest import QTest
from PySide2.QtCore import Qt, QEvent
from debug2 import Window
class test(unittest.TestCase):
def setUp(self):
self.app = QApplication.instance()
if self.app is None:
self.app = QApplication([])
self.window = Window(self.app)
self.window.create_widget()
self.window.show()
#to open gui and helpful for debug
#self.app.exec_()
def tearDown(self):
self.app.deleteLater()
def test_save_data(self):
actions = self.window.menuBar().actions()
for action in actions:
sub_commands = action.menu().actions()
for sub_command in sub_commands:
if sub_command.text() == "&Save":
print (dir(QTest))
QTest.mouseClick(self.window.window, Qt.LeftButton)
#print (sub_command, sub_command.objectName(), sub_command.text())
if __name__ == '__main__':
unittest.main()
I'm unsure why all aspects of my GUI are showing up apart from the spin box (the code for it is within the home function).
I've tried moving it to the init(self) function, but that doesn't work. I thought it would be intuitive for it to be within the home function as that is where all my other GUI (e.g. buttons) resides.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox
from temperature import MplWindow
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow()
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox()
self.doubleSpinBox.setGeometry(180, 110, 62, 22)
self.show()
def opengraph(self):
self.matplWindow.funAnimation()
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())
I worked it out - I moved the code to the init function.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox, QLabel, QVBoxLayout
from temperature import MplWindow # +++
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow() # +++
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
# vBoxLayout = QVBoxLayout()
self.label = QLabel("Set point Temp:", self)
self.label.move(50,150)
self.spinBox = QDoubleSpinBox(self)
self.spinBox.move(70,150)
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.show()
def opengraph(self):
self.matplWindow.funAnimation() # +++
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())
( How can i link two file ( two dialog) )
when i click on Open Second i want to go to another page ( window)
Here is the code for two file
Home.py
from PyQt5 import QtGui
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
btn = QPushButton('Open Second', self)
if __name__ == '__main__':
app = QApplication(sys.argv)
MW = MainWindow()
MW.show()
sys.exit(app.exec_())
SecondWindow.py
from PyQt5 import QtGui
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel
class SecondWindow(QMainWindow):
def __init__(self):
super(SecondWindow, self).__init__()
if __name__ == '__main__':
app = QApplication(sys.argv)
MW = SecondWindow()
MW.show()
sys.exit(app.exec_())
New to PyQt5... Here is a very basic question.
I would like to add an image inside the layout of a widget. This widget is the Main Window / root widget of my application. I use the following code, but I get an error message.
import sys
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(300,300,300,220)
self.setWindowTitle("Hello !")
oImage = QImage("backgound.png")
oLayout = QVBoxLayout()
oLayout.addWidget(oImage)
self.setLayout(oLayout)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
oMainwindow = MainWindow()
sys.exit(app.exec_())
TypeError: QBoxLayout.addWidget(QWidget, int stretch=0, Qt.Alignment alignment=0): argument 1 has unexpected type 'QImage'
Apparently a QLayoutWidget does not accept a QImage as an input. Is there a workaround to have an image appear as a brackground in a QWidget ?
The QVBoxLayout class lines up widgets vertically.
documentation QVBoxLayout
QImage is no widget.
on many widgets e.g. QmainWindow, QLabel you can use
widget.setStyleSheet(„ background-image: url(backgound.png);“)
on my machine this doesn't work with QWidget. In this case you can use the following rewrite of your code:
import sys
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QImage, QPalette, QBrush
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setGeometry(100,100,300,200)
oImage = QImage("test.png")
sImage = oImage.scaled(QSize(300,200)) # resize Image to widgets size
palette = QPalette()
palette.setBrush(QPalette.Window, QBrush(sImage))
self.setPalette(palette)
self.label = QLabel('Test', self) # test, if it's really backgroundimage
self.label.setGeometry(50,50,200,50)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
oMainwindow = MainWindow()
sys.exit(app.exec_())