How to add media player in my QmainWindow widget? - python

THIS IS THE MAIN UI:
I want to add a media player in this black bordered box. I have no idea how to I integrate predesigned QMediaplayers into my window. This black box is a widget that I have set to promoted to QvideoWidget in order to use it but I have little to no idea how to use it.
MY MAIN WINDOW
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainscreen.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(862, 580)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.Header = QtWidgets.QFrame(self.centralwidget)
self.Header.setMaximumSize(QtCore.QSize(16777215, 50))
self.Header.setStyleSheet("background-color: rgb(239, 128, 0);")
self.Header.setFrameShape(QtWidgets.QFrame.WinPanel)
self.Header.setFrameShadow(QtWidgets.QFrame.Raised)
self.Header.setObjectName("Header")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.Header)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setSpacing(0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.Title_bar_container = QtWidgets.QFrame(self.Header)
self.Title_bar_container.setStyleSheet("background-color: rgb(239, 133, 0);")
self.Title_bar_container.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.Title_bar_container.setFrameShadow(QtWidgets.QFrame.Raised)
self.Title_bar_container.setObjectName("Title_bar_container")
self.Slider = QtWidgets.QFrame(self.Title_bar_container)
self.Slider.setGeometry(QtCore.QRect(0, 0, 71, 52))
self.Slider.setStyleSheet("background-color: rgb(239, 133, 0);")
self.Slider.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Slider.setFrameShadow(QtWidgets.QFrame.Raised)
self.Slider.setObjectName("Slider")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.Slider)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setSpacing(0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.pushButton_4 = QtWidgets.QPushButton(self.Slider)
self.pushButton_4.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton_4.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
" \n"
" background-color: rgb(243, 164, 50);\n"
"}")
self.pushButton_4.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon_pics/png/004-list.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_4.setIcon(icon)
self.pushButton_4.setIconSize(QtCore.QSize(24, 24))
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_4.addWidget(self.pushButton_4)
self.horizontalLayout_2.addWidget(self.Title_bar_container)
self.Top_right_buttons = QtWidgets.QFrame(self.Header)
self.Top_right_buttons.setMaximumSize(QtCore.QSize(100, 16777215))
self.Top_right_buttons.setStyleSheet("background-color: rgb(239, 133, 0);")
self.Top_right_buttons.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.Top_right_buttons.setFrameShadow(QtWidgets.QFrame.Raised)
self.Top_right_buttons.setObjectName("Top_right_buttons")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.Top_right_buttons)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setSpacing(0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.pushButton = QtWidgets.QPushButton(self.Top_right_buttons)
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setMouseTracking(False)
self.pushButton.setWhatsThis("")
self.pushButton.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
" \n"
" background-color: rgb(243, 164, 50);\n"
"}")
self.pushButton.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(":/icon_pics/png/001-minimize.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon1)
self.pushButton.setIconSize(QtCore.QSize(24, 24))
self.pushButton.setFlat(False)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_3.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.Top_right_buttons)
self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton_2.setStyleSheet("background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"\n"
"QPushButton::hover{\n"
" \n"
" background-color: rgb(243, 164, 50);\n"
"}")
self.pushButton_2.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(":/icon_pics/png/003-expand.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_2.setIcon(icon2)
self.pushButton_2.setIconSize(QtCore.QSize(24, 24))
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_3.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.Top_right_buttons)
self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton_3.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
" \n"
" background-color: rgb(243, 164, 50);\n"
"}")
self.pushButton_3.setText("")
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap(":/icon_pics/png/002-remove.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_3.setIcon(icon3)
self.pushButton_3.setIconSize(QtCore.QSize(24, 24))
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_3.addWidget(self.pushButton_3)
self.horizontalLayout_2.addWidget(self.Top_right_buttons)
self.verticalLayout.addWidget(self.Header)
self.Main_body = QtWidgets.QFrame(self.centralwidget)
self.Main_body.setStyleSheet("background-color: rgb(0, 0, 127);")
self.Main_body.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Main_body.setFrameShadow(QtWidgets.QFrame.Raised)
self.Main_body.setObjectName("Main_body")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.Main_body)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.Left_side_menu = QtWidgets.QFrame(self.Main_body)
self.Left_side_menu.setMaximumSize(QtCore.QSize(70, 16777215))
self.Left_side_menu.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
self.Left_side_menu.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Left_side_menu.setFrameShadow(QtWidgets.QFrame.Raised)
self.Left_side_menu.setObjectName("Left_side_menu")
self.horizontalLayout.addWidget(self.Left_side_menu)
self.Main_screen = QtWidgets.QFrame(self.Main_body)
self.Main_screen.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
self.Main_screen.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Main_screen.setFrameShadow(QtWidgets.QFrame.Raised)
self.Main_screen.setObjectName("Main_screen")
self.widget = QVideoWidget(self.Main_screen)
self.widget.setGeometry(QtCore.QRect(70, 50, 201, 161))
self.widget.setObjectName("widget")
self.horizontalLayout.addWidget(self.Main_screen)
self.Right_side_menu = QtWidgets.QFrame(self.Main_body)
self.Right_side_menu.setMaximumSize(QtCore.QSize(70, 16777215))
self.Right_side_menu.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
self.Right_side_menu.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Right_side_menu.setFrameShadow(QtWidgets.QFrame.Raised)
self.Right_side_menu.setObjectName("Right_side_menu")
self.horizontalLayout.addWidget(self.Right_side_menu)
self.verticalLayout.addWidget(self.Main_body)
self.Footer = QtWidgets.QFrame(self.centralwidget)
self.Footer.setMaximumSize(QtCore.QSize(16777215, 50))
self.Footer.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
self.Footer.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Footer.setFrameShadow(QtWidgets.QFrame.Raised)
self.Footer.setObjectName("Footer")
self.verticalLayout.addWidget(self.Footer)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
from PyQt5.QtMultimediaWidgets import QVideoWidget
import Icons_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

You have to create a new file where you create a class that inherits from the appropriate widget and use Ui_MainWindow to fill it, there you must create the QMediaPlayer to set it in the QVideoWidget.
Assuming the file provided by the OP is gui.py then the solution is:
import sys
from PyQt5 import QtCore, QtWidgets, QtMultimedia
from gui import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.player = QtMultimedia.QMediaPlayer(
self, QtMultimedia.QMediaPlayer.VideoSurface
)
url = QtCore.QUrl(
"https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1920_18MG.mp4"
)
self.player.setMedia(QtMultimedia.QMediaContent(url))
self.player.setVideoOutput(self.ui.widget)
self.player.play()
def main(args):
app = QtWidgets.QApplication(args)
widget = MainWindow()
widget.show()
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main(sys.argv)

Related

I want to add a Viewbox to my code to make an AutoPan for X axis (pyqtgraph)

This is a GUI where I plot data from serial communication.
I want to add an AutoPan for my GUI but I don't know how.
I read there is an autopan in Viewbox but don't know how to implement it.
The option of Autopan is available when I launch the gui buy right clicking on the gui and selectign it manually but I want it to be enabled by default .
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from random import randint
import serial
ser = serial.Serial('COM5',115200)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(769, 509)
MainWindow.setStyleSheet("\n""background-color: rgb(212, 212, 212);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.graphicsView = PlotWidget(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(80, 110, 561, 271))
self.graphicsView.setObjectName("graphicsView")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(260, 30, 211, 31))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 10, 151, 80))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(650, 110, 61, 21))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(650, 210, 121, 21))
self.label_4.setObjectName("label_4")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(650, 140, 69, 22))
self.comboBox.setStyleSheet("background-color: rgb(255, 255, 255);")
self.comboBox.setEditable(False)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(650, 250, 69, 22))
self.comboBox_2.setStyleSheet("background-color: rgb(255, 255, 255);")
self.comboBox_2.setEditable(False)
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setGeometry(QtCore.QRect(80, 410, 561, 51))
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName("splitter")
self.pushButton = QtWidgets.QPushButton(self.splitter)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: rgb(255, 255, 210);")
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.splitter)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setStyleSheet("background-color: rgb(255, 255, 210);")
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 769, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.graphicsView.setBackground('w')
self.retranslateUi(MainWindow)
self.comboBox.setCurrentIndex(1)
self.comboBox_2.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Queen Monitor"))
self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:18pt; font-weight:600;\">QUEEN MONITOR</span></p></body></html>"))
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><img src=\":/rf/queen.png\"/></p></body></html>"))
self.label_3.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600;\">Baud Rate</span></p></body></html>"))
self.label_4.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600;\">Sampling Frequency</span></p></body></html>"))
self.comboBox.setItemText(0, _translate("MainWindow", "4800"))
self.comboBox.setItemText(1, _translate("MainWindow", "115200"))
self.comboBox_2.setItemText(0, _translate("MainWindow", ".... Hz"))
self.comboBox_2.setItemText(1, _translate("MainWindow", ".... Hz"))
self.comboBox_2.setItemText(2, _translate("MainWindow", ".... Hz"))
self.pushButton.setText(_translate("MainWindow", "START"))
self.pushButton_2.setText(_translate("MainWindow", "CLEAR"))
self.pushButton.clicked.connect(lambda:self.start())
self.pushButton_2.clicked.connect(lambda:self.clear())
self.x = [0]*2 # 100 time points
self.y = [0]*2
pen = pg.mkPen(color=(0, 0, 255))
self.data_line = self.graphicsView.plot(self.x, self.y, pen=pen)
def start(self):
self.timer = QtCore.QTimer()
self.timer.setInterval(1)
self.timer.timeout.connect(self.draw)
self.timer.start()
def draw(self):
"""#self.x = self.x[1:] # Remove the first y element.
self.x.append(self.x[-1] + 1) # Add a new value 1 higher than the last.
#self.y = self.y[1:] # Remove the first
self.y.append( randint(0,200)) # Add a new random value.
self.data_line.setData(self.x, self.y) # Update the data."""
rawData = ser.readline().decode("ascii")
dataSplitted = rawData.split(",")
dataSplitted.pop()
dataSplitted.pop(0)
for self.data_raw in dataSplitted:
self.x.append(self.x[-1] + 1)
self.y.append(int(self.data_raw))
self.data_line.setData(self.x, self.y)
def clear(self):
self.graphicsView.clear()
from pyqtgraph import PlotWidget
import rf_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

trying to make rock paper scissors game using random module,ifelse and pyqt5 and when i execute this program only first condition is executed

import random
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 433)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMaximumSize(QtCore.QSize(641, 433))
MainWindow.setToolTipDuration(-1)
MainWindow.setStyleSheet("background-color: rgb(0, 0, 0);")
MainWindow.setAnimated(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 160, 91))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_4 = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label_4.setStyleSheet("background-color: rgb(255, 0, 0);\n"
"image: url(:/newPrefix/donkey kong.jfif);")
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.horizontalLayout.addWidget(self.label_4)
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(160, 0, 400, 91))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label_2.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"font: 48pt \"MS Shell Dlg 2\";\n"
"color: rgb(255, 255, 255);")
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.horizontalLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_3.setGeometry(QtCore.QRect(320, 0, 160, 91))
self.horizontalLayoutWidget_3.setObjectName("horizontalLayoutWidget_3")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_3)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_3 = QtWidgets.QLabel(self.horizontalLayoutWidget_3)
self.label_3.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"font: 48pt \"MS Shell Dlg 2\";\n"
"color: rgb(255, 255, 255);")
self.label_3.setObjectName("label_3")
self.horizontalLayout_3.addWidget(self.label_3)
self.horizontalLayoutWidget_4 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_4.setGeometry(QtCore.QRect(480, 0, 160, 91))
self.horizontalLayoutWidget_4.setObjectName("horizontalLayoutWidget_4")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_4)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget_4)
self.label.setStyleSheet("background-color: rgb(0, 85, 255);\n"
"image: url(:/newPrefix/OIP.jfif);")
self.label.setText("")
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.horizontalLayout_4.addLayout(self.verticalLayout)
self.horizontalLayoutWidget_5 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_5.setGeometry(QtCore.QRect(110, 90, 66, 31))
self.horizontalLayoutWidget_5.setObjectName("horizontalLayoutWidget_5")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_5)
self.horizontalLayout_5.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.lcdNumber = QtWidgets.QLCDNumber(self.horizontalLayoutWidget_5)
self.lcdNumber.setObjectName("lcdNumber")
self.horizontalLayout_5.addWidget(self.lcdNumber)
self.horizontalLayoutWidget_6 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_6.setGeometry(QtCore.QRect(420, 90, 66, 31))
self.horizontalLayoutWidget_6.setObjectName("horizontalLayoutWidget_6")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_6)
self.horizontalLayout_6.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.lcdNumber_2 = QtWidgets.QLCDNumber(self.horizontalLayoutWidget_6)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lcdNumber_2.sizePolicy().hasHeightForWidth())
self.lcdNumber_2.setSizePolicy(sizePolicy)
self.lcdNumber_2.setToolTipDuration(-1)
self.lcdNumber_2.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lcdNumber_2.setObjectName("lcdNumber_2")
self.horizontalLayout_6.addWidget(self.lcdNumber_2)
self.horizontalLayoutWidget_8 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_8.setGeometry(QtCore.QRect(190, 320, 245, 80))
self.horizontalLayoutWidget_8.setObjectName("horizontalLayoutWidget_8")
self.horizontalLayout_9 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_8)
self.horizontalLayout_9.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_9.setObjectName("horizontalLayout_9")
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget_8)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setStyleSheet("background-color: rgb(255, 170, 0);")
self.pushButton.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("rock.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon)
self.pushButton.setIconSize(QtCore.QSize(65, 47))
self.pushButton.setDefault(True)
self.pushButton.setFlat(False)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_9.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget_8)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth())
self.pushButton_2.setSizePolicy(sizePolicy)
self.pushButton_2.setStyleSheet("background-color: rgb(255, 170, 0);")
self.pushButton_2.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("paper.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_2.setIcon(icon1)
self.pushButton_2.setIconSize(QtCore.QSize(65, 47))
self.pushButton_2.setAutoDefault(True)
self.pushButton_2.setDefault(True)
self.pushButton_2.setFlat(False)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_9.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.horizontalLayoutWidget_8)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth())
self.pushButton_3.setSizePolicy(sizePolicy)
self.pushButton_3.setStyleSheet("background-color: rgb(255, 170, 0);")
self.pushButton_3.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("scissors.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_3.setIcon(icon2)
self.pushButton_3.setIconSize(QtCore.QSize(65, 47))
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_9.addWidget(self.pushButton_3)
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(0, 140, 221, 169))
self.label_7.setStyleSheet("background-color: rgb(255, 170, 0);")
self.label_7.setText("")
self.label_7.setPixmap(QtGui.QPixmap("rps.png"))
self.label_7.setScaledContents(True)
self.label_7.setObjectName("label_7")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(418, 140, 221, 169))
self.label_6.setStyleSheet("background-color: rgb(255, 170, 0);")
self.label_6.setText("")
self.label_6.setPixmap(QtGui.QPixmap("rps.png"))
self.label_6.setScaledContents(True)
self.label_6.setObjectName("label_6")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.showrocklabel_6)
self.pushButton_2.clicked.connect(self.showpaperlabel_6)
self.pushButton_3.clicked.connect(self.showscissorslabel_6)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", " V"))
self.label_3.setText(_translate("MainWindow", " S"))
def random1(self):
C = random.randint(1, 3)
print(C)
L=[self.label_7.setPixmap(QtGui.QPixmap("rock.png")),self.label_7.setPixmap(QtGui.QPixmap("paper.png")),self.label_7.setPixmap(QtGui.QPixmap("scissors.png"))]
L[C]
"""if C == 1:
self.label_7.setPixmap(QtGui.QPixmap("rock.png"))
if C == 2:
self.label_7.setPixmap(QtGui.QPixmap("paper.png"))
ctrc += 1
if C == 3:
self.label_7.setPixmap(QtGui.QPixmap("scissors.png"))"""
def showrocklabel_6(self):
self.label_6.setPixmap(QtGui.QPixmap("rock.png"))
self.random1()
def showpaperlabel_6(self):
self.label_6.setPixmap(QtGui.QPixmap("paper.png"))
self.random1()
def showscissorslabel_6(self):
self.label_6.setPixmap(QtGui.QPixmap("scissors.png"))
self.random1()
import assets_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Responsiveness, made in QT Designer, is not working. Python

I made an application design in QT Designer and saved it as a .ui file.
Next, converted .ui file into .py file with this command - pyuic5 -o design.py responsive_design.ui
After this, added this 👇
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
at the end of a file to make it work.
But, unfortunately, when my Python file starts, application opens with a static design, not responsive, as expected.
I see this -
Is it a QT bug or my mistake? Is it possible to resolve this problem?
My full code is below -
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'responsive_design.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
MainWindow.setMinimumSize(QtCore.QSize(800, 450))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 801, 451))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../Downloads/bg.png"))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(-1, 0, 801, 451))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap(""))
self.label_2.setScaledContents(False)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap(""))
self.label_3.setScaledContents(False)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.horizontalLayout.setStretch(0, 1)
self.horizontalLayout.setStretch(1, 4)
self.horizontalLayout.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_5 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.horizontalLayout_2.addWidget(self.label_5)
self.label_6 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(16)
self.label_6.setFont(font)
self.label_6.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName("label_6")
self.horizontalLayout_2.addWidget(self.label_6)
self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.horizontalLayout_2.addWidget(self.label_4)
self.horizontalLayout_2.setStretch(0, 1)
self.horizontalLayout_2.setStretch(1, 5)
self.horizontalLayout_2.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_7 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_7.setText("")
self.label_7.setObjectName("label_7")
self.horizontalLayout_3.addWidget(self.label_7)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.label_11 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_11.setFont(font)
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.gridLayout.addWidget(self.label_11, 2, 0, 1, 1)
self.label_10 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_10.setToolTip("")
self.label_10.setWhatsThis("")
self.label_10.setText("")
self.label_10.setPixmap(QtGui.QPixmap("../microphone.png"))
self.label_10.setAlignment(QtCore.Qt.AlignCenter)
self.label_10.setObjectName("label_10")
self.gridLayout.addWidget(self.label_10, 1, 0, 1, 1)
self.label_9 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_9.setText("")
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName("label_9")
self.gridLayout.addWidget(self.label_9, 0, 0, 1, 1)
self.horizontalLayout_3.addLayout(self.gridLayout)
self.label_8 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.horizontalLayout_3.addWidget(self.label_8)
self.horizontalLayout_3.setStretch(0, 5)
self.horizontalLayout_3.setStretch(1, 4)
self.horizontalLayout_3.setStretch(2, 5)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_14 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_14.setText("")
self.label_14.setObjectName("label_14")
self.horizontalLayout_4.addWidget(self.label_14)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.label_16 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_16.setText("")
self.label_16.setObjectName("label_16")
self.gridLayout_2.addWidget(self.label_16, 2, 0, 1, 1)
self.label_15 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(12)
self.label_15.setFont(font)
self.label_15.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_15.setAlignment(QtCore.Qt.AlignCenter)
self.label_15.setObjectName("label_15")
self.gridLayout_2.addWidget(self.label_15, 1, 0, 1, 1)
self.label_13 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_13.setFont(font)
self.label_13.setAlignment(QtCore.Qt.AlignCenter)
self.label_13.setObjectName("label_13")
self.gridLayout_2.addWidget(self.label_13, 0, 0, 1, 1)
self.gridLayout_2.setRowStretch(0, 1)
self.gridLayout_2.setRowStretch(1, 3)
self.horizontalLayout_4.addLayout(self.gridLayout_2)
self.label_12 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_12.setText("")
self.label_12.setObjectName("label_12")
self.horizontalLayout_4.addWidget(self.label_12)
self.horizontalLayout_4.setStretch(0, 4)
self.horizontalLayout_4.setStretch(1, 5)
self.horizontalLayout_4.setStretch(2, 4)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.verticalLayout.setStretch(0, 2)
self.verticalLayout.setStretch(1, 8)
self.verticalLayout.setStretch(2, 2)
self.verticalLayout.setStretch(3, 6)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_6.setText(_translate("MainWindow", "Output"))
self.label_11.setText(_translate("MainWindow", "I'm listening when microphone is visible"))
self.label_15.setText(_translate("MainWindow", "SAID"))
self.label_13.setText(_translate("MainWindow", "I Think You Said:"))
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
first I installed the centrallayout, then I loaded all the elements to the layouts and I loaded these layouts to the centrallayout.
check the differences and edit it yourself
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'responsive_design.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox, QVBoxLayout, QScrollArea, QWidget
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
MainWindow.setMinimumSize(QtCore.QSize(800, 450))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel()
self.label.setPixmap(QtGui.QPixmap("../Downloads/bg.png"))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.centralLayout = QVBoxLayout(self.centralwidget)
self.centralLayout.setContentsMargins(0,0,0,0)
self.centralLayout.addWidget(self.label)
self.verticalLayout = QtWidgets.QVBoxLayout(self.label)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel()
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap(""))
self.label_2.setScaledContents(False)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label_3 = QtWidgets.QLabel()
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap(""))
self.label_3.setScaledContents(False)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.horizontalLayout.setStretch(0, 1)
self.horizontalLayout.setStretch(1, 4)
self.horizontalLayout.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_5 = QtWidgets.QLabel()
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.horizontalLayout_2.addWidget(self.label_5)
self.label_6 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(16)
self.label_6.setFont(font)
self.label_6.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName("label_6")
self.horizontalLayout_2.addWidget(self.label_6)
self.label_4 = QtWidgets.QLabel()
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.horizontalLayout_2.addWidget(self.label_4)
self.horizontalLayout_2.setStretch(0, 1)
self.horizontalLayout_2.setStretch(1, 5)
self.horizontalLayout_2.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_7 = QtWidgets.QLabel()
self.label_7.setText("")
self.label_7.setObjectName("label_7")
self.horizontalLayout_3.addWidget(self.label_7)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.label_11 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_11.setFont(font)
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.gridLayout.addWidget(self.label_11, 2, 0, 1, 1)
self.label_10 = QtWidgets.QLabel()
self.label_10.setToolTip("")
self.label_10.setWhatsThis("")
self.label_10.setText("")
self.label_10.setPixmap(QtGui.QPixmap("../microphone.png"))
self.label_10.setAlignment(QtCore.Qt.AlignCenter)
self.label_10.setObjectName("label_10")
self.gridLayout.addWidget(self.label_10, 1, 0, 1, 1)
self.label_9 = QtWidgets.QLabel()
self.label_9.setText("")
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName("label_9")
self.gridLayout.addWidget(self.label_9, 0, 0, 1, 1)
self.horizontalLayout_3.addLayout(self.gridLayout)
self.label_8 = QtWidgets.QLabel()
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.horizontalLayout_3.addWidget(self.label_8)
self.horizontalLayout_3.setStretch(0, 5)
self.horizontalLayout_3.setStretch(1, 4)
self.horizontalLayout_3.setStretch(2, 5)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_14 = QtWidgets.QLabel()
self.label_14.setText("")
self.label_14.setObjectName("label_14")
self.horizontalLayout_4.addWidget(self.label_14)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.label_16 = QtWidgets.QLabel()
self.label_16.setText("")
self.label_16.setObjectName("label_16")
self.gridLayout_2.addWidget(self.label_16, 2, 0, 1, 1)
self.label_15 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(12)
self.label_15.setFont(font)
self.label_15.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_15.setAlignment(QtCore.Qt.AlignCenter)
self.label_15.setObjectName("label_15")
self.gridLayout_2.addWidget(self.label_15, 1, 0, 1, 1)
self.label_13 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_13.setFont(font)
self.label_13.setAlignment(QtCore.Qt.AlignCenter)
self.label_13.setObjectName("label_13")
self.gridLayout_2.addWidget(self.label_13, 0, 0, 1, 1)
self.gridLayout_2.setRowStretch(0, 1)
self.gridLayout_2.setRowStretch(1, 3)
self.horizontalLayout_4.addLayout(self.gridLayout_2)
self.label_12 = QtWidgets.QLabel()
self.label_12.setText("")
self.label_12.setObjectName("label_12")
self.horizontalLayout_4.addWidget(self.label_12)
self.horizontalLayout_4.setStretch(0, 4)
self.horizontalLayout_4.setStretch(1, 5)
self.horizontalLayout_4.setStretch(2, 4)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.verticalLayout.setStretch(0, 2)
self.verticalLayout.setStretch(1, 8)
self.verticalLayout.setStretch(2, 2)
self.verticalLayout.setStretch(3, 6)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_6.setText(_translate("MainWindow", "Output"))
self.label_11.setText(_translate("MainWindow", "I'm listening when microphone is visible"))
self.label_15.setText(_translate("MainWindow", "SAID"))
self.label_13.setText(_translate("MainWindow", "I Think You Said:"))
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

How get a frameless window in pyqt5?

I am new to PyQt5 and am having a hard time figuring out how will I convert the code accordingly so that it is frameless. I have used flags but it keeps giving me an error that the "UI_MainWindow object has no attribute 'SetWindowFlags'. The problem is in the execution part but I'm unable to figure out how to make the alterations.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainScreen.UI'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QSizeGrip
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(688, 525)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.Header = QtWidgets.QFrame(self.centralwidget)
self.Header.setMaximumSize(QtCore.QSize(16777215, 50))
self.Header.setStyleSheet("background-color: rgb(239, 128, 0);")
self.Header.setFrameShape(QtWidgets.QFrame.WinPanel)
self.Header.setFrameShadow(QtWidgets.QFrame.Raised)
self.Header.setObjectName("Header")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.Header)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setSpacing(0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.Title_bar_container = QtWidgets.QFrame(self.Header)
self.Title_bar_container.setStyleSheet("background-color: rgb(239, 133, 0);")
self.Title_bar_container.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.Title_bar_container.setFrameShadow(QtWidgets.QFrame.Raised)
self.Title_bar_container.setObjectName("Title_bar_container")
self.Slider = QtWidgets.QFrame(self.Title_bar_container)
self.Slider.setGeometry(QtCore.QRect(0, 0, 71, 52))
self.Slider.setStyleSheet("background-color: rgb(239, 133, 0);")
self.Slider.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Slider.setFrameShadow(QtWidgets.QFrame.Raised)
self.Slider.setObjectName("Slider")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.Slider)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setSpacing(0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.pushButton_4 = QtWidgets.QPushButton(self.Slider)
self.pushButton_4.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton_4.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
" \n"
" background-color: rgb(243, 164, 50);\n"
"}")
self.pushButton_4.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon_pics/png/004-list.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_4.setIcon(icon)
self.pushButton_4.setIconSize(QtCore.QSize(24, 24))
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_4.addWidget(self.pushButton_4)
self.horizontalLayout_2.addWidget(self.Title_bar_container)
self.Top_right_buttons = QtWidgets.QFrame(self.Header)
self.Top_right_buttons.setMaximumSize(QtCore.QSize(100, 16777215))
self.Top_right_buttons.setStyleSheet("background-color: rgb(239, 133, 0);")
self.Top_right_buttons.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.Top_right_buttons.setFrameShadow(QtWidgets.QFrame.Raised)
self.Top_right_buttons.setObjectName("Top_right_buttons")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.Top_right_buttons)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setSpacing(0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.pushButton = QtWidgets.QPushButton(self.Top_right_buttons)
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setMouseTracking(False)
self.pushButton.setWhatsThis("")
self.pushButton.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
" \n"
" background-color: rgb(243, 164, 50);\n"
"}")
self.pushButton.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(":/icon_pics/png/001-minimize.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon1)
self.pushButton.setIconSize(QtCore.QSize(24, 24))
self.pushButton.setFlat(False)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_3.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.Top_right_buttons)
self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton_2.setStyleSheet("background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"\n"
"QPushButton::hover{\n"
" \n"
" background-color: rgb(243, 164, 50);\n"
"}")
self.pushButton_2.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(":/icon_pics/png/003-expand.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_2.setIcon(icon2)
self.pushButton_2.setIconSize(QtCore.QSize(24, 24))
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_3.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.Top_right_buttons)
self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton_3.setStyleSheet("\n"
"QPushButton{\n"
"background-color: rgb(239, 133, 0);\n"
"border:none;\n"
"}\n"
"\n"
"QPushButton::hover{\n"
" \n"
" background-color: rgb(243, 164, 50);\n"
"}")
self.pushButton_3.setText("")
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap(":/icon_pics/png/002-remove.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_3.setIcon(icon3)
self.pushButton_3.setIconSize(QtCore.QSize(24, 24))
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_3.addWidget(self.pushButton_3)
self.horizontalLayout_2.addWidget(self.Top_right_buttons)
self.verticalLayout.addWidget(self.Header)
self.Main_body = QtWidgets.QFrame(self.centralwidget)
self.Main_body.setStyleSheet("background-color: rgb(0, 0, 127);")
self.Main_body.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Main_body.setFrameShadow(QtWidgets.QFrame.Raised)
self.Main_body.setObjectName("Main_body")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.Main_body)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.Left_side_menu = QtWidgets.QFrame(self.Main_body)
self.Left_side_menu.setMaximumSize(QtCore.QSize(70, 16777215))
self.Left_side_menu.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
self.Left_side_menu.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Left_side_menu.setFrameShadow(QtWidgets.QFrame.Raised)
self.Left_side_menu.setObjectName("Left_side_menu")
self.horizontalLayout.addWidget(self.Left_side_menu)
self.Main_screen = QtWidgets.QFrame(self.Main_body)
self.Main_screen.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
self.Main_screen.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Main_screen.setFrameShadow(QtWidgets.QFrame.Raised)
self.Main_screen.setObjectName("Main_screen")
self.horizontalLayout.addWidget(self.Main_screen)
self.Right_side_menu = QtWidgets.QFrame(self.Main_body)
self.Right_side_menu.setMaximumSize(QtCore.QSize(70, 16777215))
self.Right_side_menu.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
self.Right_side_menu.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Right_side_menu.setFrameShadow(QtWidgets.QFrame.Raised)
self.Right_side_menu.setObjectName("Right_side_menu")
self.horizontalLayout.addWidget(self.Right_side_menu)
self.verticalLayout.addWidget(self.Main_body)
self.Footer = QtWidgets.QFrame(self.centralwidget)
self.Footer.setMaximumSize(QtCore.QSize(16777215, 50))
self.Footer.setStyleSheet("background-color: rgb(239, 128, 0);\n"
"background-color: rgb(243, 164, 50);")
self.Footer.setFrameShape(QtWidgets.QFrame.NoFrame)
self.Footer.setFrameShadow(QtWidgets.QFrame.Raised)
self.Footer.setObjectName("Footer")
self.verticalLayout.addWidget(self.Footer)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
flags = QtCore.Qt.WindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
self.setWindowFlags(flags)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
import Icons_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The problem comes from the fact that you're trying to edit a file that is not intended to be modified (exactly as the warning in the header says).
setWindowFlags is a function of QWidget classes. The self used there refers to the Ui_MainWindow instance, which is a simple python object (the "Form class").
Theoretically, you can just use that function against the MainWindow argument of setupUi (which is a QWidget subclass, so it implements that function):
MainWindow.setWindowFlags(flags)
But, no. Don't.
Editing those files is considered bad practice, and trying to do it without knowing what (nor why) you're doing always leads to misunderstandings, unexpected behavior and bugs, just like in your case, not considering the fact that whenever you need to change something in the ui you then have to painfully merge the new ui file with the existing code.
Regenerate that file with pyuic, and leave it as it is. Then create a new script, which will be your actual program script, and do the only thing that is expected with those files: import it.
from PyQt5 import QtWidgets
from file_generated_by_pyuic import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowFlags(
self.windowFlags() |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.WindowStaysOnTopHint
)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
# note the lower case "mainWindow", indicating it's an *instance*
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
In the example above it's implied that you used pyuic to create a file named file_generated_by_pyuic.py.
Read more about this topic in the official guidelines about using Designer.

How can I set the sizePolicy of my mainWindow?

I made a simple mainWindow with the help of the qt designer and got the whole code using the pyuic5 option in the cmd.
I added this lineto the main part:
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
to be able to see my window correctly (without this its a whole mass).
In the properties inside qt designer i changed the sizePolicy values to be expanding, but when i try to run my application the sizePolicy is not working, it all stays in the left upper corner.
This is the code i got from the pyuic5:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CompanyCommanderLogin(object):
def setupUi(self, CompanyCommanderLogin):
CompanyCommanderLogin.setObjectName("CompanyCommanderLogin")
CompanyCommanderLogin.resize(252, 227)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(CompanyCommanderLogin.sizePolicy().hasHeightForWidth())
CompanyCommanderLogin.setSizePolicy(sizePolicy)
self.centralwidget = QtWidgets.QWidget(CompanyCommanderLogin)
self.centralwidget.setObjectName("centralwidget")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(10, 10, 231, 171))
self.groupBox.setObjectName("groupBox")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(20, 40, 71, 16))
self.label.setObjectName("label")
self.label_3 = QtWidgets.QLabel(self.groupBox)
self.label_3.setGeometry(QtCore.QRect(20, 70, 41, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.groupBox)
self.label_4.setGeometry(QtCore.QRect(20, 100, 41, 16))
self.label_4.setObjectName("label_4")
self.lineEdit_x_location = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_x_location.setGeometry(QtCore.QRect(100, 70, 51, 20))
self.lineEdit_x_location.setObjectName("lineEdit_x_location")
self.lineEdit_y_location = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_y_location.setGeometry(QtCore.QRect(100, 100, 51, 20))
self.lineEdit_y_location.setObjectName("lineEdit_y_location")
self.pushButton_login = QtWidgets.QPushButton(self.groupBox)
self.pushButton_login.setGeometry(QtCore.QRect(140, 140, 71, 21))
self.pushButton_login.setStyleSheet("background-color: rgb(0, 209, 255);\n"
"color: rgb(255, 255, 255);\n"
"background-color: rgb(0, 0, 127);")
self.pushButton_login.setObjectName("pushButton_login")
self.comboBox = QtWidgets.QComboBox(self.groupBox)
self.comboBox.setGeometry(QtCore.QRect(100, 40, 51, 21))
self.comboBox.setObjectName("comboBox")
CompanyCommanderLogin.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(CompanyCommanderLogin)
self.menubar.setGeometry(QtCore.QRect(0, 0, 252, 18))
self.menubar.setObjectName("menubar")
CompanyCommanderLogin.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(CompanyCommanderLogin)
self.statusbar.setObjectName("statusbar")
CompanyCommanderLogin.setStatusBar(self.statusbar)
self.retranslateUi(CompanyCommanderLogin)
QtCore.QMetaObject.connectSlotsByName(CompanyCommanderLogin)
def retranslateUi(self, CompanyCommanderLogin):
_translate = QtCore.QCoreApplication.translate
CompanyCommanderLogin.setWindowTitle(_translate("CompanyCommanderLogin", "Company Commander Login"))
self.groupBox.setTitle(_translate("CompanyCommanderLogin", "Login"))
self.label.setText(_translate("CompanyCommanderLogin", "<html><head/><body><p><span style=\" font-size:9pt;\">Company Number:</span></p></body></html>"))
self.label_3.setText(_translate("CompanyCommanderLogin", "<html><head/><body><p><span style=\" font-size:9pt;\">X Location:</span></p></body></html>"))
self.label_4.setText(_translate("CompanyCommanderLogin", "<html><head/><body><p><span style=\" font-size:9pt;\">Y Location:</span></p></body></html>"))
self.pushButton_login.setText(_translate("CompanyCommanderLogin", "Login"))
if __name__ == "__main__":
import sys
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QtWidgets.QApplication(sys.argv)
CompanyCommanderLogin = QtWidgets.QMainWindow()
ui = Ui_CompanyCommanderLogin()
ui.setupUi(CompanyCommanderLogin)
CompanyCommanderLogin.show()
sys.exit(app.exec_())
What could be the problem with the sizePolicy? Is there another way to make my mainWindow the expand?
Picture for example (Here the login is in the left upper corner, I want it to be the whole window size)
As the docs points out:
The size policy of a widget is an expression of its willingness to be resized in various ways, and affects how the widget is treated by the layout engine. Each widget returns a QSizePolicy that describes the horizontal and vertical resizing policy it prefers when being laid out. You can change this for a specific widget by changing its QWidget::sizePolicy property.
The QSizePolicy work only in layouts, and in your case there are none.
So the solution is to create your design using the QXLayout, if you want to use it in Qt Designer you should read the Qt Designer Manual and especially section Using Layouts in Qt Designer.
In this case, since you have not provided the .ui that would allow me to use Qt Designer, I will quickly provide the solution from scratch
from PyQt5 import QtCore, QtGui, QtWidgets
class CompanyCommanderLogin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.groupbox = QtWidgets.QGroupBox(self.tr("Login"))
self.company_cb = QtWidgets.QComboBox()
sp = self.company_cb.sizePolicy()
sp.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
self.company_cb.setSizePolicy(sp)
self.xlocation_le = QtWidgets.QLineEdit()
self.ylocation_le = QtWidgets.QLineEdit()
self.login_button = QtWidgets.QPushButton(self.tr("Login"))
self.login_button.setStyleSheet(
"background-color: rgb(0, 209, 255);\n"
"color: rgb(255, 255, 255);\n"
"background-color: rgb(0, 0, 127);"
)
font = self.font()
font.setPointSize(9)
self.setFont(font)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(self.groupbox)
flay = QtWidgets.QFormLayout()
flay.addRow(
self.tr("Company Number:"), self.company_cb,
)
flay.addRow(
self.tr("X Location:"), self.xlocation_le,
)
flay.addRow(
self.tr("Y Location:"), self.ylocation_le,
)
lay = QtWidgets.QVBoxLayout()
lay.addLayout(flay)
lay.addWidget(self.login_button, alignment=QtCore.Qt.AlignHCenter)
lay.addStretch()
self.groupbox.setLayout(lay)
if __name__ == "__main__":
import sys
if hasattr(QtCore.Qt, "AA_EnableHighDpiScaling"):
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QtWidgets.QApplication(sys.argv)
w = CompanyCommanderLogin()
w.show()
sys.exit(app.exec_())

Categories