I'm dealing with my first Pyqt5 app and I'm having an hard time understanding how to make my widgets change size according to the change of the window size (like when the window is Maximized). This is my code:
import sys
from PyQt5.QtWidgets import (
QApplication,
QHBoxLayout,
QVBoxLayout,
QFrame,
QListWidget,
QPlainTextEdit,
QWidget,
)
# from PyQt5 import QtCore, QtGui,
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Title")
self.centralwidget = QWidget(self)
self.centralwidgetHorizontalLayout = QHBoxLayout(self.centralwidget)
self.citationsFrame = QFrame(self.centralwidget)
self.citationsFrameVerticalLayout = QVBoxLayout(self.citationsFrame)
self.citationPlainTextWidget = QPlainTextEdit(self.citationsFrame)
self.citationsFrameVerticalLayout.addWidget(self.citationPlainTextWidget)
self.citationListWidget = QListWidget(self.citationsFrame)
self.citationsFrameVerticalLayout.addWidget(self.citationListWidget)
self.centralwidgetHorizontalLayout.addWidget(self.citationsFrame)
self.centralwidgetHorizontalLayout.addWidget(self.citationsFrame)
self.referencesFrame = QFrame(self.centralwidget)
self.referencesFrameHorizontalLayout = QHBoxLayout(self.referencesFrame)
self.referencesListWidget = QListWidget(self.referencesFrame)
self.referencesListWidget.setDragEnabled(True)
self.referencesFrameHorizontalLayout.addWidget(self.referencesListWidget)
self.centralwidgetHorizontalLayout.addWidget(self.referencesFrame)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
What am I doing wrong?
Related
I have a problem with my python code, it's a QTablewidget with Qpushbuttons and Qline_edits inside each cell, We want a file path (after selecting this file from the file browser) to be written in the line edit when we click on its pushbutton and to store the output files so we can use them after.
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QPushButton,
QFileDialog,
QGridLayout,
QLineEdit,
QTableWidget,
QHBoxLayout,
QWidget,
)
from PyQt5.QtCore import pyqtSlot
import sys
from functools import partial
global list_C
list_C=[]
class boxlayout(QWidget):
def __init__(self):
super(boxlayout,self).__init__()
layout=QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
boxlayout.le=QLineEdit()
boxlayout.btn=QPushButton()
layout.addWidget(boxlayout.le)
layout.addWidget(boxlayout.btn)
self.setLayout(layout)
class Main(QMainWindow):
def __init__(self):
super().__init__()
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
mainlayout = QGridLayout(centralWidget)
self.table = QTableWidget(self)
self.table.resize(640, 480)
self.table.setColumnCount(3)
self.table.setRowCount(4)
mainlayout.addWidget(self.table)
save_button=QPushButton('OK')
mainlayout.addWidget(save_button)
save_button.clicked.connect(self.save_as_list)
for i in range(4):
for j in range(3):
self.table.setCellWidget(i,j, boxlayout())
boxlayout.btn.clicked.connect(partial(self.open_dialog, boxlayout.le))
#pyqtSlot(QLineEdit)
def open_dialog(self, le: QLineEdit):
file_name = QFileDialog.getOpenFileName(
self,
"Open File",
"${HOME}",
"All Files (*)",
)
le.setText(file_name[0])
def save_as_list(self):
for i in range(4):
for j in range(3):
item=self.table.cellWidget(i,j).le.text()
list_C.append(item)
print(list_C) #output=['last le value','last le value',...]
if __name__ == "__main__":
app = QApplication(sys.argv)
main_gui = Main()
main_gui.show()
sys.exit(app.exec())
Here is a minimal example:
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QPushButton,
QFileDialog,
QFrame,
QGridLayout,
QLineEdit,
)
from PyQt5.QtCore import pyqtSlot
import sys
from functools import partial
class Main(QMainWindow):
def __init__(self):
super().__init__()
top_frame = QFrame(self)
self.setCentralWidget(top_frame)
self.grid = QGridLayout(top_frame)
for i in range(10):
btn = QPushButton(top_frame)
le = QLineEdit(top_frame)
btn.clicked.connect(partial(self.open_dialog, le))
btn.setText("open file system dialog")
self.grid.addWidget(btn, i, 0)
self.grid.addWidget(le, i, 1)
#pyqtSlot(QLineEdit)
def open_dialog(self, le: QLineEdit):
file_name = QFileDialog.getOpenFileName(
self,
"Open File",
"${HOME}",
"All Files (*)",
)
le.setText(file_name[0])
if __name__ == "__main__":
app = QApplication(sys.argv)
main_gui = Main()
main_gui.show()
sys.exit(app.exec())
The key thing here is to pass the QLineEdit to the file selector method.
Here we are doing it with: lambda: self.open_dialog(le)
Keep gif running while GUI starts. Is that possible? I have read many reporitys but none with the true and understandable answer.
I have prepared a code example that shows the problem.
import sys
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5 import QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QSize, QThread
class Main_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(500, 500))
self.setWindowTitle("Main Window")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
gif = QLabel(self)
gif.setGeometry(0,0,500,500)
self.movie = QMovie(r"C:\Users\...\Pictures\Icon_LOAD.gif")
gif.setMovie(self.movie)
self.movie.start()
# #Call event handler to process the queue, but it is shocking, dirty and unsuitable
#app.processEvents()
self.show()
for i in range(0,1000000):
print(i)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWin = Main_Window()
sys.exit(app.exec_())
Now it works smooth
import sys
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5 import QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QSize
from threading import Thread
class Main_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(500, 500))
self.setWindowTitle("Main Window")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
gif = QLabel(self)
gif.setGeometry(0,0,500,500)
self.movie = QMovie(r"gif.gif")
gif.setMovie(self.movie)
self.movie.start()
self.show()
Thread(target=self.function_on_thread).start()
def function_on_thread(self):
for i in range(0,1000000):
print(i)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWin = Main_Window()
sys.exit(app.exec_())
from PyQt5 import QtMultimedia, QtMultimediaWidgets
from PyQt5.QtCore import QDir, Qt, QUrl, QSizeF
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer, QMediaPlaylist
from PyQt5.QtMultimediaWidgets import QVideoWidget, QGraphicsVideoItem
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget, QStackedWidget,
QStackedLayout, QGraphicsView, QGraphicsScene)
from PyQt5.QtWidgets import QMainWindow,QWidget, QPushButton, QAction
from PyQt5.QtGui import QIcon
import sys, glob
class VerySimpleMediaPlayer(QWidget):
def __init__(self, parent):
super().__init__(parent)
self.media_player = QtMultimedia.QMediaPlayer(self)
self.media_widget = QtMultimediaWidgets.QVideoWidget(self)
self.media_player.setVideoOutput(self.media_widget)
self.media_widget.show()
self.media_player.setMedia(QMediaContent(QUrl.fromLocalFile('avideo.mp4')))
self.media_player.mediaStatusChanged.connect(self.look)
layout = QVBoxLayout()
self.real = QStackedWidget()
self.button = QPushButton()
self.button.setText('Push Me PLS!')
self.real.addWidget(self.button)
self.button.clicked.connect(self.clicked)
self.real.addWidget(self.media_widget)
layout.addWidget(self.real)
self.setLayout(layout)
self.awidget = QWidget()
bx = QVBoxLayout()
lbl = QLabel()
lbl.setText('Video Has Finished!')
bx.addWidget(lbl)
self.awidget.setLayout(bx)
self.real.addWidget(self.awidget)
def clicked(self):
self.media_player.play()
self.real.setCurrentIndex(self.real.currentIndex() + 1)
def look(self, state):
if state == 7:
self.real.setCurrentIndex(self.real.currentIndex() + 1)
app = QApplication(sys.argv)
player = QMainWindow()
player.setCentralWidget(VerySimpleMediaPlayer(player))
player.show()
sys.exit(app.exec_())
It works good but the only problem is that the videos are playing horizontally even if the video is a vertical video like 800 x 1600.
Is there any way to rotate videos either automatically or non-automatically?
i would like to embed the glb file to pyqt gui with vtk python library. I wrote pieces of code but it does not embed the sketch to the pyqt gui. Everytime there have been placing in the second windows form. Here is the example:
And here is the my source code that i am trying to embed it to gui:
import sys
import vtk
try:
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtWidgets import QWidget, QSizePolicy, QPushButton, QVBoxLayout, QFrame
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, pyqtSignal, QTimer, QObject, QSize, QEvent
except ImportError:
raise ImportError("Cannot load either PyQt5")
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
importer = vtk.vtkGLTFImporter()
importer.SetFileName("uydu.glb")
importer.Read()
global vtk_render_window
vtk_renderer = importer.GetRenderer()
vtk_render_window = importer.GetRenderWindow()
self.setGeometry(190, 300, 300, 200)
self.setWindowTitle('Simple menu')
self.show()
self.frame = QFrame()
self.vl = QVBoxLayout()
vtk_render_window_interactor = QVTKRenderWindowInteractor(self.frame)
vtk_render_window_interactor.SetRenderWindow(vtk_render_window)
colors = vtk.vtkNamedColors()
vtk_renderer.SetBackground(colors.GetColor3d('White'))
actor = vtk.vtkActor()
actor.GetProperty().SetDiffuse(0.8)
actor.GetProperty().SetDiffuseColor(colors.GetColor3d('Black'))
actor.GetProperty().SetSpecular(0.3)
actor.GetProperty().SetSpecularPower(60.0)
vtk_render_window.SetSize(600, 600)
vtk_render_window.SetWindowName('3D Visualizer')
vtk_render_window_interactor.Initialize()
# Add callback for getting data from Arduino
#vtk_render_window_interactor.CreateRepeatingTimer(1000)
#vtk_render_window_interactor.AddObserver("TimerEvent", information_callback)
global vtk_actors
vtk_actors = vtk_renderer.GetActors
self.vl.addWidget(vtk_render_window_interactor)
self.renderer = vtk.vtkRenderer()
self.renderer.SetBackground(.3, .4, .5 )
self.renwin = vtk_render_window_interactor.GetRenderWindow()
self.renwin.AddRenderer(self.renderer)
# An interactor
self.inter = self.renwin.GetInteractor()
# add the custom style
self.style = MouseInteractorHighLightActor()
self.style.SetDefaultRenderer(self.renderer)
vtk_render_window_interactor.GetRenderWindow().AddRenderer(self.renderer)
#self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.inter.SetInteractorStyle(self.style)
#self.iren.SetInteractorStyle(self.inter)
#self.iren = self.vtkWidget.GetRenderWindow().SetInteractor(self.inter)
self.renderer.ResetCamera()
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.show()
self.inter.Initialize()
self.inter.Start()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Menu()
sys.exit(app.exec_())
You have to pass the renderWindow of vtkGLTFImporter as rw parameter to the QVTKRenderWindowInteractor widget:
import sys
import vtk
try:
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtWidgets import QWidget, QVBoxLayout
except ImportError:
raise ImportError("Cannot load either PyQt5")
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(190, 300, 300, 200)
self.setWindowTitle("Simple menu")
self.container = QWidget()
vl = QVBoxLayout(self.container)
self.setCentralWidget(self.container)
self.resize(640, 480)
importer = vtk.vtkGLTFImporter()
importer.SetFileName("uydu.glb")
importer.Read()
renderer = importer.GetRenderer()
render_window = importer.GetRenderWindow()
vtk_widget = QVTKRenderWindowInteractor(rw=render_window)
vl.addWidget(vtk_widget)
vtk_widget.Initialize()
vtk_widget.Start()
colors = vtk.vtkNamedColors()
renderer.SetBackground(colors.GetColor3d("White"))
renderer.ResetCamera()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Menu()
ex.show()
sys.exit(app.exec_())
This question already has answers here:
PySide (or PyQt) signals and slots basics
(3 answers)
Closed 4 years ago.
In this code i want to make a GUI of QSlider with PyQt5. What all I want is to print changing value as i move the slider. But here, it only prints 90 which is the primary value of the slider.
N.B: I know that continuously changing value can be printed from valuechange function. But I need to print the value only from init function.
The code:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout,
QGroupBox,QMenu, QPushButton,
QRadioButton, QVBoxLayout,
QWidget, QSlider,QLabel)
class sliderdemo(QWidget):
def __init__(self, parent = None):
super(sliderdemo, self).__init__(parent)
layout = QVBoxLayout()
self.sl = QSlider(Qt.Horizontal)
self.sl.setMinimum(0)
self.sl.setMaximum(180)
self.sl.setValue(90)
self.sl.setTickPosition(QSlider.TicksBelow)
self.sl.setTickInterval(10)
layout.addWidget(self.sl)
self.sl.valueChanged.connect(self.valuechange)
self.setLayout(layout)
self.setWindowTitle("slider")
print(self.valuechange())
def valuechange(self):
self.size = self.sl.value()
return self.size
def main():
app = QApplication(sys.argv)
ex = sliderdemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If you really want, you can :-)
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout,
QGroupBox,QMenu, QPushButton,
QRadioButton, QVBoxLayout,
QWidget, QSlider,QLabel)
class sliderdemo(QWidget):
def __init__(self, vSl=90, parent=None):
super(sliderdemo, self).__init__(parent)
layout = QVBoxLayout()
self.sl = QSlider(Qt.Horizontal)
self.sl.setMinimum(0)
self.sl.setMaximum(180)
self.sl.setValue(vSl)
self.sl.setTickPosition(QSlider.TicksBelow)
self.sl.setTickInterval(10)
layout.addWidget(self.sl)
self.sl.valueChanged[int].connect(self.valuechange)
self.setLayout(layout)
self.setWindowTitle("slider")
#print(self.valuechange())
print("__init__vSl -> ", vSl)
def valuechange(self, value):
#self.size = self.sl.value()
self.__init__(value)
#return self.size
def main():
app = QApplication(sys.argv)
ex = sliderdemo(90)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()