How can I set the sizePolicy of my mainWindow? - python

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_())

Related

How do I go about creating a dynamic grid QScrollarea in PyQt5?

I'm developing some custom software that involves creating a workspace and working in it. I want to display all the existing workSpace directories in a dynamic scrollarea grid comprised of buttons that changes the positions of the buttons depending on the amount of screen real estate the scrollarea is taking up so that as many buttons as possible are inserted in the highest possible row.( So basically like the file explorer changes the layout of the folders and files to fit the screen in a grid depending on how you resize the window) I tried doing this using a gridlayout inside the scrollarea. I also tried to add qhboxlayouts in a qvboxlayout to no avail since I had no idea how to check whether there is space for another button to remove one from a lower qhboxlayout and append to a higher one.
This is my current code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
class Ui_AutoCal(object):
def setupUi(self, AutoCal):
AutoCal.setObjectName("AutoCal")
AutoCal.resize(513, 551)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(AutoCal.sizePolicy().hasHeightForWidth())
AutoCal.setSizePolicy(sizePolicy)
AutoCal.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
AutoCal.setStyleSheet("*{\n"
"background-color: rgb(54, 54, 54);\n"
"color:rgb(255,255,255)\n"
"}\n"
"\n"
"QPushButton\n"
"{\n"
"border-radius: 25px;\n"
"}\n"
"\n"
"QLineEdit\n"
"{\n"
"color:rgb(0,0,0)\n"
"}")
self.centralwidget = QtWidgets.QWidget(AutoCal)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
self.groupBox.setSizePolicy(sizePolicy)
self.groupBox.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox.setObjectName("groupBox")
self.verticalLayout_6 = QtWidgets.QVBoxLayout(self.groupBox)
self.verticalLayout_6.setObjectName("verticalLayout_6")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label = QtWidgets.QLabel(self.groupBox)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setObjectName("label")
self.horizontalLayout_3.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.groupBox)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
self.lineEdit.setSizePolicy(sizePolicy)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_3.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.groupBox)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setObjectName("pushButton")
# self.pushButton.clicked.connect(self.openFileNameDialog )
self.horizontalLayout_3.addWidget(self.pushButton)
self.verticalLayout_6.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_2 = QtWidgets.QLabel(self.groupBox)
self.label_2.setObjectName("label_2")
self.horizontalLayout_4.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_4.addWidget(self.lineEdit_2)
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_4.addWidget(self.pushButton_2)
self.verticalLayout_6.addLayout(self.horizontalLayout_4)
self.verticalLayout.addWidget(self.groupBox)
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setEnabled(True)
self.groupBox_2.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox_2.setObjectName("groupBox_2")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.groupBox_2)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.scrollArea = QtWidgets.QScrollArea(self.groupBox_2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scrollArea.sizePolicy().hasHeightForWidth())
self.scrollArea.setSizePolicy(sizePolicy)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.gridLayout = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
# self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 467, 331))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.fillScrollArea()
self.verticalLayout_5.addWidget(self.scrollArea)
self.verticalLayout_3.addWidget(self.groupBox_2)
self.verticalLayout.addLayout(self.verticalLayout_3)
AutoCal.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(AutoCal)
self.menubar.setGeometry(QtCore.QRect(0, 0, 513, 22))
self.menubar.setObjectName("menubar")
self.menuWorkSpace = QtWidgets.QMenu(self.menubar)
self.menuWorkSpace.setObjectName("menuWorkSpace")
self.menuRecipients = QtWidgets.QMenu(self.menubar)
self.menuRecipients.setObjectName("menuRecipients")
AutoCal.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(AutoCal)
self.statusbar.setObjectName("statusbar")
AutoCal.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuWorkSpace.menuAction())
self.menubar.addAction(self.menuRecipients.menuAction())
self.retranslateUi(AutoCal)
QtCore.QMetaObject.connectSlotsByName(AutoCal)
def retranslateUi(self, AutoCal):
_translate = QtCore.QCoreApplication.translate
AutoCal.setWindowTitle(_translate("AutoCal", "AutoCal"))
self.groupBox.setTitle(_translate("AutoCal", "Create New WorkSpace"))
self.label.setText(_translate("AutoCal", "Choose WorkSpace Directory"))
self.pushButton.setText(_translate("AutoCal", "Browse"))
self.label_2.setText(_translate("AutoCal", "WorkSpace Name"))
self.pushButton_2.setText(_translate("AutoCal", "Create"))
self.groupBox_2.setTitle(_translate("AutoCal", "Choose Existing WorkSpace"))
self.menuWorkSpace.setTitle(_translate("AutoCal", "WorkSpace"))
self.menuRecipients.setTitle(_translate("AutoCal", "Recipients"))
def fillScrollArea(self):
for i in range(50):
for j in range(50):
self.gridLayout.addWidget(QtWidgets.QPushButton("Test"), i, j)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
AutoCal = QtWidgets.QMainWindow()
ui = Ui_AutoCal()
ui.setupUi(AutoCal)
AutoCal.show()
sys.exit(app.exec_())
This produces the following scrollArea (ignore the forms):
Current Scroll Area
The main problem is that i need the buttons to always be in the visible section of the scrollArea, whereas as you can see many of them are hidden behind the scrollarea and their text is cut off. I know this is because of the positioning that my function applies to the grid layout but I'm not aware of any other way to position them. I also need the buttons to be dynamic in the sense that if horizontal space becomes greater in a higher row, the ones in the lower row will take up that space. Because of this I dont need horizontal scrolling either, which is why it's disabled.
My issue is now resolved. While flow layout was useful, it was much more intuitive to simply use QListWidget. Many thanks to both eyllanesc and musicamante for their help.
For anyone who has the same issue, the desired result can be achieved by simply creating a QlistWidget and adding items to it:
size = QSize(100,100)
self.listWidget = QListWidget()
size = QtCore.QSize(80,80)
self.listWidget.setResizeMode(QListView.Adjust)
self.listWidget.setIconSize(size)
self.listWidget.itemDoubleClicked.connect(self.itemClicked)
self.listWidget.setViewMode(QListView.IconMode)

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 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)

PyQt5 QPushbutton to store ariables Python

Using PyQt5 QPlaintext to store variables. Example AO = userinput?
What I have so far minus the GUI setup. I need to store it as simple variables so i can output to a DOCX. I know I have to manually user input for every QPlaintextbox available. would I use the getText(), link it to the push button to generate the variables. Thank you for your time.
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1340, 922)
self.OPORD = QtWidgets.QTabWidget(Dialog)
self.OPORD.setGeometry(QtCore.QRect(0, 0, 1231, 991))
self.OPORD.setTabBarAutoHide(False)
self.OPORD.setObjectName("OPORD")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
self.label = QtWidgets.QLabel(self.tab_4)
self.label.setGeometry(QtCore.QRect(280, 240, 311, 331))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("WAATS_Final.png"))
self.label.setObjectName("label")
self.OPORD.addTab(self.tab_4, "")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.ORDERS = QtWidgets.QStackedWidget(self.tab)
self.ORDERS.setGeometry(QtCore.QRect(0, 0, 1141, 1080))
self.ORDERS.setMaximumSize(QtCore.QSize(1920, 1080))
self.ORDERS.setObjectName("ORDERS")
self.stackedWidgetPage1 = QtWidgets.QWidget()
self.stackedWidgetPage1.setObjectName("stackedWidgetPage1")
self.WAATS4 = QtWidgets.QCheckBox(self.stackedWidgetPage1)
self.WAATS4.setGeometry(QtCore.QRect(390, 50, 141, 20))
self.WAATS4.setMaximumSize(QtCore.QSize(1920, 1080))
self.WAATS4.setObjectName("WAATS4")
self.label_2 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_2.setGeometry(QtCore.QRect(0, 70, 271, 21))
self.label_2.setMaximumSize(QtCore.QSize(1920, 1080))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_9 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_9.setGeometry(QtCore.QRect(240, 0, 181, 21))
self.label_9.setMaximumSize(QtCore.QSize(1920, 1080))
self.label_9.setText("")
self.label_9.setObjectName("label_9")
self.num = QtWidgets.QLineEdit(self.stackedWidgetPage1)
self.num.setGeometry(QtCore.QRect(0, 30, 181, 31))
self.num.setMaximumSize(QtCore.QSize(1920, 1080))
self.num.setObjectName("num")
self.aoi = QtWidgets.QPlainTextEdit(self.stackedWidgetPage1)
self.aoi.setGeometry(QtCore.QRect(0, 110, 431, 71))
self.aoi.setMaximumSize(QtCore.QSize(1920, 1080))
self.aoi.setObjectName("aoi")
self.COPORD = QtWidgets.QPushButton(self.widget)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.OPORD.setTabText(self.OPORD.indexOf(self.tab_4), _translate("Dialog", "Welcome"))
self.WAATS4.setText(_translate("Dialog", "TASS"))
self.label_2.setText(_translate("Dialog", "Situation"))
self.num.setPlaceholderText(_translate("Dialog", "##-##"))
self.aoi.setPlainText(_translate("Dialog", "?"))
self.aoi.setPlaceholderText(_translate("Dialog", "AI"))
self.label_3.setText(_translate("Dialog", "Area of Interest"))
self.AO.setText(_translate("Dialog", "Area of Operation"))
self.ao.setPlaceholderText(_translate("Dialog", "AO"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
as I see on your code, to get string from the QLineEdit you can use this code.
self.num.text()
if you print that code, it will automatically print the inputed string of QLineEdit.
you can add a QPushButton then make a signal slot to do that as a form in commonly use.
like these:
self.button = QPushButton()
self.button.clicked.connect(self.getLineEdit)
def getLineEdit(self):
lineEditText = self.num.text()
print(lineEditText)

Categories