I am trying to build a GUI using PySide6 on Windows 10 - Python V 3.10.2.
Basically, I wanted to create a vertical box on left with buttons to switch between the stacked widgets. And a video widget with play button & scroll bar on the right side with a stacked widget underneath the video widget. On the stacked widget, I intend to put some buttons which will link files to play on the video widget.
The Problem: The video widget does works perfectly when commenting out the stacked widget. However, if I add the stacked widget, sound does play nicely but the videowidget can't be seen.
See the code snippet below.
class mainwindow(QMainWindow):
def __init__(self):
super().__init__()
self.audio = QAudioOutput()
self.video = QVideoWidget()
self.player = QMediaPlayer()
self.player.setAudioOutput(self.audio)
self.player.setVideoOutput(self.video)
side_layout = QVBoxLayout()
side_layout.addWidget(QLabel('First Label'))
side_layout.addWidget(QPushButton('First Button'))
# I have not named buttons for now for conciseness.
vid_control_layout = QHBoxLayout()
play_btn = QPushButton('Play')
play_btn.clicked.connect(self.play_video)
vid_control_layout.addWidget(play_btn)
vid_control_layout.addWidget(QSlider(orientation = Qt.Horizontal))
stack_widget = QStackedWidget()
stack_widget.addWidget(QWidget())
stack_widget.addWidget(QWidget())
video_layout = QVBoxLayout()
video_layout.addWidget(self.video)
video_layout.addLayout(vid_control_layout)
video_layout.addWidget(stack_widget)
mainlayout = QHBoxLayout()
mainlayout.addLayout(side_layout)
mainlayout.addLayout(video_layout)
mainwidget = QWidget()
mainwidget.setLayout(mainlayout)
self.setCentralWidget(mainwidget)
def play_video(self):
self.player.setSource('111.mp4')
self.player.play()
app = QApplication(sys.argv)
win = mainwindow()
win.show()
sys.exit(app.exec())
I am a self learner of Python and started learning PyQt5 quite recently. As there was too many version problem in PyQt5 and QMediaPlayer, I switched to PySide6.
Let me know if this detail will help in fixing the problem.
Related
I've been trying to figure this one out, the reason I don't want to use a layout is because the widgets scale with the window when resizing and I want them to stay put, I have made it where I can drag and drop the widgets but with them being in a layout it messes it up, please can someone help me figure this out.
I Want to be able to add lets say a label widget, I want to have it where I can press a button and it will create a new label widget in my window, I have done this part already but I want to be able to add widgets without the layout.
You just have to set another widget that is part of the window (or the window itself) as parent and make it visible with the show method:
import random
import sys
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
button = QtWidgets.QPushButton("Add", self)
button.clicked.connect(self.handle_clicked)
self.resize(640, 480)
def handle_clicked(self):
pos = QtCore.QPoint(*random.sample(range(400), 2))
label = QtWidgets.QLabel(self)
label.move(pos)
label.setText("{}-{}".format(pos.x(), pos.y()))
label.show()
def main():
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
I was hoping someone could help me with creating a custom title bar widget for a dock widget in a PyQt4 GUI program. All I want to do is emulate the exact same look and function of the default title bar, but with an extra, custom button. I couldn't find an easy way to do this as I don't know if there's a default title bar widget I can add stuff to, so I made a custom dock title bar widget:
from PyQt4 import QtGui, QtCore
class DockTitleBar(QtGui.QFrame):
def __init__(self, parent):
super(DockTitleBar, self).__init__(parent)
# Is this the only way to give the title bar a border?
self.setFrameStyle(QtGui.QFrame.Raised | QtGui.QFrame.StyledPanel)
# Layout for title box
layout = QtGui.QHBoxLayout(self)
layout.setSpacing(1)
layout.setMargin(1)
self.label = QtGui.QLabel(parent.windowTitle())
icon_size = QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_TitleBarNormalButton).actualSize(
QtCore.QSize(100, 100))
button_size = icon_size + QtCore.QSize(5, 5)
# Custom button I want to add
self.button = QtGui.QToolButton(self)
self.button.setAutoRaise(True)
self.button.setMaximumSize(button_size)
self.button.setIcon(QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_TitleBarContextHelpButton))
self.button.clicked.connect(self.do_something)
# Close dock button
self.close_button = QtGui.QToolButton(self)
self.close_button.setAutoRaise(True)
self.close_button.setMaximumSize(button_size)
self.close_button.setIcon(QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_DockWidgetCloseButton))
self.close_button.clicked.connect(self.close_parent)
# Setup layout
layout.addWidget(self.label)
layout.addStretch()
layout.addWidget(self.button)
layout.addWidget(self.close_button)
def do_something(self):
# Do something when custom button is pressed
pass
def close_parent(self):
self.parent().hide()
It seems to work okay, except for when the dock is dragged around in its floating state. Normally there are borders and even the title bar is highlighted, but with my janky version there is no frame for the floating dock so it's hard to tell where it is, and the title bar isn't highlighted. Is there something I can fix/add or should I be doing this in an entirely different way?
I am learning to make GUI's in PySide.
How do I re-size the buttons inside a QHBoxLayout()? I tried button_1.setFixedWidth() and button_1.setFixedHeight() these make the buttons non-scalable. button_1.move() also doesn't work.
Also I have created a function angles() which have Qlabel and QLineEdit, when I run the program, the button function is over-writing the angles function to display only buttons at right corner of the GUI.
And how to resize the length of the QLineEdit and for it to not extend the whole window?
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class MainWindow(QMainWindow):
#GUI Layout
def __init__(self,parent = None):
super(MainWindow, self).__init__(parent)
widget = QWidget()
self.setCentralWidget(widget)
self.setWindowTitle("Example")
self.setGeometry(400, 100, 1500, 800)
self.angles()
self.makebuttons()
def angles(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
Rotation = QLabel('Rotation:')
Tilt = QLabel('Tilt:')
RoatationEdit = QLineEdit()
TiltEdit = QLineEdit()
grid = QGridLayout()
grid.setSpacing(2)
grid.addWidget(Rotation,1,0)
grid.addWidget(RoatationEdit, 1, 1)
grid.addWidget(Tilt,2,0)
grid.addWidget(TiltEdit, 2, 1)
central_widget.setLayout(grid)
def makebuttons(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
hbox = QHBoxLayout()
button_1 = QPushButton("Button 1",self)
button_1.move(0,30)
hbox.addStretch(1)
button_2 = QPushButton("Button 2",self)
hbox.addStretch(1)
hbox.addWidget(button_1)
hbox.addWidget(button_2)
vbox = QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
central_widget.setLayout(vbox)
# central_widget.addLayout(vbox)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
If you want to resize use: button_1.setFixedSize({your scale}*button_1.size())
The makebuttons function creates another centralWidget by deleting all of the above, so you will not see what you did with angles.
To change the width of QLineEdit use {your QlineEdit} .setFixedWidth({your width})
I use Qt Designer for all of my Pyside GUI work, even if it's a fairly trivial program. It's much more than just a drag-and-drop WYSISYG tool. For things like push buttons in your example, you would be presented with a list of configurable properties including sizing parameters of the button as well as the ability to configure the layout.
So, my solution is to create your GUI in QT Designer then modify the layout there before using the pyside-uic tool to convert the code to python. Then just import the resulting python module into your code. From there you can still re-configure whatever you want later in your code if, for example, you need to change the appearance of your GUI during the course of your program.
I am using PyQt to create a desktop application. I am trying to create a button using hbox and vbox, but it doesn't display unless I give the specific command:
button1 = QtGui.QPushButton("Exit", self)
But, by doing this, the vbox and hbox functionality doesn't seem to function.
I need the button to be on the bottom-right corner of the window, which stays there even after window-resize.
With this code, it is positioned on the top-left corner.
from PyQt4 import QtGui, QtCore
import sys
class Trial(QtGui.QMainWindow):
def __init__(self):
super(Trial,self).__init__()
self.createUI()
def createUI(self):
button1 = QtGui.QPushButton("Exit",self)
button1.clicked.connect(self.close)
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1) #stretches it to the right end of the page
hbox.addWidget(button1)
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1) #stretches it to the bottom end of the page
vbox.addLayout(hbox)
self.setLayout(vbox)
button1.resize(button1.sizeHint())
self.setGeometry(300,200,750,450)
self.setWindowTitle('Testing')
self.show()
def main():
app= QtGui.QApplication(sys.argv)
w=Trial()
sys.exit(app.exec_())
if __name__=='__main__':
main()
If I use button1.move(420, 400), it moves the button to the position I want, but it doesn't stay there when I re-size the application window.
The example code doesn't work because you are trying to set a layout on the main window, which already has a layout.
Instead, you need to add a central widget, and then set the layout on that:
def createUI(self):
self.setCentralWidget(QtGui.QWidget(self))
...
vbox.addLayout(hbox)
self.centralWidget().setLayout(vbox)
self.setGeometry(300,200,750,450)
...
Whenever I create a PyQt4 application with secondary (child) windows they do not minimize to the Windows 7 taskbar. Only the main (parent) window shows up on the taskbar. When a child window is minimized it collapses to the bottom of the screen with only the titlebar (and titlebar buttons) showing. Here is some sample code to demo this behavoir:
import sys
from PyQt4 import QtGui, QtCore
class Parent(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Parent, self).__init__(parent)
w = QtGui.QWidget()
layout = QtGui.QVBoxLayout(w)
self.button = QtGui.QPushButton('Create Child')
self.text = QtGui.QTextEdit()
layout.addWidget(self.button)
layout.addWidget(self.text)
self.setCentralWidget(w)
self.setWindowTitle('Parent')
self.button.clicked.connect(self.createChild)
def createChild(self):
self.dialog = QtGui.QMainWindow(self)
#self.dialog.setParent(None)
self.dialog.setWindowTitle('Child')
self.dialog.show()
app = QtGui.QApplication(sys.argv)
p = Parent()
p.show()
sys.exit(app.exec_())
The only way I get my pyqt apps to behave the way I want is to set the parent of the children windows to None.
self.dialog.setParent(None)
Doing so makes keeping track of the children windows a lot more complicated than I feel it should be. Closing the main window does not close the secondary windows for example. With extra code this can work but it seems odd to have to break the parent relationship. Am I missing something?