How get a frameless window in pyqt5? - python

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.

Related

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 to add media player in my QmainWindow widget?

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)

How to stop a function without closing the application?

So, I want to close the function on_click_on_start which has while loop in it. And, dont want to close the window after hitting the stop button. Right now, I was using sys.exit() which crashed the program for some reason but, when I used only quit() it was closing the entire app. I am doing this on pyQt5. I am noob to GUI so please help me out. This is extra text which I have added so that I can post the question. Please ignore this.
import res1
import res
from PyQt5 import QtCore, QtGui, QtWidgets
import os
import pyaudio
import numpy as np
import pyautogui
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(800, 600)
MainWindow.setMaximumSize(QtCore.QSize(800, 600))
font = QtGui.QFont()
font.setFamily("Californian FB")
font.setPointSize(14)
MainWindow.setFont(font)
MainWindow.setMouseTracking(True)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(
"../../../../Pictures/ugo_trans.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
MainWindow.setAutoFillBackground(True)
MainWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setAutoFillBackground(False)
self.centralwidget.setStyleSheet("background-color: rgb(255, 0, 0);")
self.centralwidget.setObjectName("centralwidget")
self.Backgroundimage = QtWidgets.QLabel(self.centralwidget)
self.Backgroundimage.setEnabled(True)
self.Backgroundimage.setGeometry(QtCore.QRect(9, 9, 782, 582))
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
self.Backgroundimage.sizePolicy().hasHeightForWidth())
self.Backgroundimage.setSizePolicy(sizePolicy)
self.Backgroundimage.setMaximumSize(QtCore.QSize(800, 600))
self.Backgroundimage.setStyleSheet("")
self.Backgroundimage.setText("")
self.Backgroundimage.setPixmap(
QtGui.QPixmap(":/newPrefix/Untitled-1.png"))
self.Backgroundimage.setScaledContents(True)
self.Backgroundimage.setObjectName("Backgroundimage")
self.Start = QtWidgets.QPushButton(self.centralwidget)
self.Start.setGeometry(QtCore.QRect(60, 240, 221, 111))
font = QtGui.QFont()
font.setPointSize(16)
self.Start.setFont(font)
self.Start.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);")
self.Start.setObjectName("Start")
self.Stop = QtWidgets.QPushButton(self.centralwidget)
self.Stop.setGeometry(QtCore.QRect(510, 240, 221, 111))
font = QtGui.QFont()
font.setPointSize(16)
self.Stop.setFont(font)
self.Stop.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);")
self.Stop.setObjectName("Stop")
self.Title = QtWidgets.QLabel(self.centralwidget)
self.Title.setGeometry(QtCore.QRect(210, 70, 361, 61))
font = QtGui.QFont()
font.setPointSize(48)
self.Title.setFont(font)
self.Title.setStyleSheet("color: rgb(255, 255, 255);\n"
"background-color: rgb(0, 0, 0, 60);")
self.Title.setObjectName("Title")
MainWindow.setCentralWidget(self.centralwidget)
self.Start.clicked.connect(self.on_click_on_start) # button pressed on start
self.Stop.clicked.connect(self.on_click_on_stop) # button pressed on stop and it only stops the on_click_on_start
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def on_click_on_start(self):
CHUNK = 2**11
RATE = 48016
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True,
frames_per_buffer=CHUNK)
start = time.time()
while 1:
data = np.fromstring(stream.read(CHUNK), dtype=np.int16)
peak = np.average(np.abs(data))*2
bars = 100*peak/2**16
if bars > 1:
pyautogui.keyDown("NUM0")
start = time.time()
print('down')
else:
pyautogui.keyUp("NUM0")
print('up')
if time.time() - start > 5:
stream.stop_stream()
stream.close()
p.terminate()
break
def on_click_on_stop(self):
# Help me with this function
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Talk to Mute"))
self.Start.setText(_translate("MainWindow", "Start"))
self.Stop.setText(_translate("MainWindow", "Stop"))
self.Title.setText(_translate("MainWindow", "Talk to Mute"))
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_())
Generally using variables is easiest way to manage status. You can use variable for update running status.
running_status = false
Use like
self.running_status = true
in on_click_on_start.
Use
self.running_status = false
in on_click_on_stop. So now you can call your function if running_status is true.

Going back from new window to main window

I have 2 windows, MainScreen and AddFW.
Whenever I go from MainScreen -> AddFW, it works (the MainScreen hides and the AddFW appears).
But when I try going back from AddFW to MainScreen, the window crashes and I get an error: "NameError: name 'AddFW' is not defined".
What am I doing wrong?
MainScreen.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLineEdit, QPushButton
from PyQt5 import *
from PyQt5 import QtGui
from AddFW import *
import pyodbc
server = '**'
database = '**'
username = '**'
password = '**'
conn = pyodbc.connect('DRIVER={**};'
'SERVER=' + server + ';'
'DATABASE=' + database + ';'
'UID=' + username + ';'
'PWD=' + password)
class Ui_MainWindow(object):
def OpenAddFW(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_AddFW()
self.ui.setupUi(self.window)
self.window.show()
MainWindow.hide()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1000, 600)
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 0, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
MainWindow.setPalette(palette)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(410, 40, 201, 41))
font = QtGui.QFont()
font.setFamily("Imprint MT Shadow")
font.setPointSize(27)
font.setBold(False)
font.setUnderline(True)
font.setWeight(50)
self.label.setFont(font)
self.label.setObjectName("label")
self.InsertResultsBtn = QtWidgets.QPushButton(self.centralwidget)
self.InsertResultsBtn.setGeometry(QtCore.QRect(58, 226, 160, 130))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.InsertResultsBtn.setFont(font)
self.InsertResultsBtn.setObjectName("InsertResultsBtn")
self.SearchResultsBtn = QtWidgets.QPushButton(self.centralwidget)
self.SearchResultsBtn.setGeometry(QtCore.QRect(300, 226, 160, 130))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.SearchResultsBtn.setFont(font)
self.SearchResultsBtn.setObjectName("SearchResultsBtn")
self.CompareResultsBtn = QtWidgets.QPushButton(self.centralwidget)
self.CompareResultsBtn.setGeometry(QtCore.QRect(541, 226, 160, 130))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.CompareResultsBtn.setFont(font)
self.CompareResultsBtn.setObjectName("CompareResultsBtn")
self.AddFWBtn = QtWidgets.QPushButton(self.centralwidget)
self.AddFWBtn.setGeometry(QtCore.QRect(783, 226, 160, 130))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.AddFWBtn.setFont(font)
self.AddFWBtn.setObjectName("AddFWBtn")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setEnabled(True)
self.label_2.setGeometry(QtCore.QRect(420, 540, 181, 16))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 0, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
self.label_2.setPalette(palette)
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TestTrack"))
self.InsertResultsBtn.setText(_translate("MainWindow", "Insert Results"))
self.SearchResultsBtn.setText(_translate("MainWindow", "Search Results"))
self.CompareResultsBtn.setText(_translate("MainWindow", "Compare Results"))
self.AddFWBtn.setText(_translate("MainWindow", "Add Firmware"))
self.label_2.setText(_translate("MainWindow", "George Hanna & Nadav Benun"))
self.AddFWBtn.clicked.connect(self.OpenAddFW)
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_())
AddFW
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLineEdit
from PyQt5 import *
from PyQt5 import QtGui
from MainScreen import *
import pyodbc
server = '**'
database = '**'
username = '**'
password = '**'
conn = pyodbc.connect('DRIVER={**};'
'SERVER=' + server + ';'
'DATABASE=' + database + ';'
'UID=' + username + ';'
'PWD=' + password)
def create(conn2, AddToDataBase):
cursor = conn2.cursor()
cursor.execute('insert into dbo.Firmware(Firmware) values(?);', AddToDataBase)
conn2.commit()
print('Added!')
class Ui_AddFW(object):
def AddFWFunc(self):
create(conn, self.FirmwareInsert.text())
def BackToMain(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.window)
self.window.show()
AddFW.hide()
def setupUi(self, AddFW):
AddFW.setObjectName("AddFW")
AddFW.resize(1000, 600)
self.centralwidget = QtWidgets.QWidget(AddFW)
self.centralwidget.setObjectName("centralwidget")
self.AddFWButton = QtWidgets.QPushButton(self.centralwidget)
self.AddFWButton.setGeometry(QtCore.QRect(470, 260, 121, 28))
self.AddFWButton.setObjectName("AddFWButton")
self.Back = QtWidgets.QPushButton(self.centralwidget)
self.Back.setGeometry(QtCore.QRect(40, 40, 51, 28))
self.Back.setObjectName("Back")
self.FirmwareLabel = QtWidgets.QLabel(self.centralwidget)
self.FirmwareLabel.setGeometry(QtCore.QRect(320, 170, 60, 16))
self.FirmwareLabel.setObjectName("FirmwareLabel")
self.Title = QtWidgets.QLabel(self.centralwidget)
self.Title.setGeometry(QtCore.QRect(370, 50, 321, 16))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.Title.setFont(font)
self.Title.setObjectName("Title")
self.FirmwareInsert = QtWidgets.QLineEdit(self.centralwidget)
self.FirmwareInsert.setGeometry(QtCore.QRect(400, 160, 281, 31))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.FirmwareInsert.sizePolicy().hasHeightForWidth())
self.FirmwareInsert.setSizePolicy(sizePolicy)
self.FirmwareInsert.setObjectName("FirmwareInsert")
AddFW.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(AddFW)
self.statusbar.setObjectName("statusbar")
AddFW.setStatusBar(self.statusbar)
self.retranslateUi(AddFW)
QtCore.QMetaObject.connectSlotsByName(AddFW)
def retranslateUi(self, AddFW):
_translate = QtCore.QCoreApplication.translate
AddFW.setWindowTitle(_translate("AddFW", "MainWindow"))
self.AddFWButton.setText(_translate("AddFW", "Insert to DataBase"))
self.Back.setText(_translate("AddFW", "Back"))
self.FirmwareLabel.setText(_translate("AddFW", "Firmware:"))
self.Title.setText(_translate("AddFW", "Add Firmware to the DataBase"))
self.AddFWButton.clicked.connect(self.AddFWFunc)
self.Back.clicked.connect(self.BackToMain)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
AddFW = QtWidgets.QMainWindow()
ui = Ui_AddFW()
ui.setupUi(AddFW)
AddFW.show()
sys.exit(app.exec_())
This happens because Add_FW exists only within the setupUi and retranslateUi methods. It works in MainWindow because you are declaring it below in
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow() <- HERE
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
One quick fix is to pass the AddFW object through connect this way :
self.Back.clicked.connect(lambda: self.BackToMain(AddFW))
But a better way to deal with this would be to inherit QMainWindow in both Ui_AddFW and Ui_MainWindow class due to which they both become an instance of QMainWindow and you wont have to pass the QMainWindow when you are instantiating it.
Check this out: PyQt class inheritance
Finally,
Instead of showing and hiding screens within each object, I would suggest creating a main.py and handle the show/hide logic there this way:
main.py
from AddFW import Ui_AddFW
from MainScreen import Ui_MainWindow
def handle_back_to_main():
add_fw.hide()
main_screen.show()
def handle_back_to_add_fw():
main_screen.hide()
add_fw.show()
add_fw = Ui_AddFW()
main_screen = Ui_MainWindow()
add_fw.Back.clicked.connect(handle_back_to_main)
main_screen.AddFWBtn.clicked.connect(handle_back_to_add_fw)

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