pyqt6 cannot open a second QMainWindow from primary QMainWindow - pyqt

I have been searching and searching, but cant seem to find any examples that match what I am doing. Essentially I have two QMainWindow UIs that I built using the designer and used the cli tool to convert them to python files. The main window loads perfectly and works as intended. After the user makes some selections and clicks a button, I am wanting a child window to open to perform actions based on the selection in the parent window. The problem is the child window will not open. There are no errors thrown, the window just will not open. Im at a loss and Ive asked some other people and they are also not sure why it isnt opening, so Im hoping the brain trust around here might be able to help me out. Using pseudo code to simplify everything. If I swap out the UIs in __main__ the second UI loads so I know its code is good. If I can get this to work the next thing I am needing is to be able to send a variable over when the window opens, which I think I can do in the setup UI function when I call it. Any and all help is appreciated as I have tried a lot of variations and either get the same results, or python crashes with I click the open child window button.
main.py contents:
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 100)
MainWindow.setMinimumSize(QtCore.QSize(500, 100))
MainWindow.setMaximumSize(QtCore.QSize(500, 100))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbl_title = QtWidgets.QLabel(self.centralwidget)
self.lbl_title.setGeometry(QtCore.QRect(0, 0, 500, 25))
self.lbl_title.setMinimumSize(QtCore.QSize(500, 25))
self.lbl_title.setMaximumSize(QtCore.QSize(500, 25))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(13)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.lbl_title.setFont(font)
self.lbl_title.setAutoFillBackground(False)
self.lbl_title.setStyleSheet("background-color: rgb(11, 91, 172);\n"
"font: 13pt \"Verdana\";\n"
"color: white;")
self.lbl_title.setFrameShape(QtWidgets.QFrame.Shape.Box)
self.lbl_title.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.lbl_title.setLineWidth(2)
self.lbl_title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.lbl_title.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.NoTextInteraction)
self.lbl_title.setObjectName("lbl_title")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(29, 30, 442, 66))
self.widget.setObjectName("widget")
self.grd_main = QtWidgets.QGridLayout(self.widget)
self.grd_main.setContentsMargins(0, 0, 0, 0)
self.grd_main.setObjectName("grd_main")
self.cb_cb1 = QtWidgets.QComboBox(self.widget)
self.cb_cb1.setObjectName("cb_cb1")
self.grd_main.addWidget(self.cb_cb1, 1, 2, 1, 1)
self.lbl_select = QtWidgets.QLabel(self.widget)
self.lbl_select.setAlignment(
QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignTrailing
| QtCore.Qt.AlignmentFlag.AlignVCenter)
self.lbl_select.setObjectName("lbl_select")
self.grd_main.addWidget(self.lbl_select, 1, 0, 1, 2)
self.btn_load_objects = QtWidgets.QPushButton(self.widget)
self.btn_load_objects.setObjectName("btn_load_objects")
self.btn_load_objects.clicked.connect(self.load_object_button_clicked)
self.grd_main.addWidget(self.btn_load_objects, 0, 3, 1, 1)
self.btn_openchild = QtWidgets.QPushButton(self.widget)
self.btn_openchild.setEnabled(False)
self.btn_openchild.setObjectName("btn_openchild")
self.btn_openchild.clicked.connect(self.openchild_button_clicked)
self.grd_main.addWidget(self.btn_openchild, 1, 3, 1, 1)
self.cb_cb2 = QtWidgets.QComboBox(self.widget)
self.cb_cb2.setObjectName("cb_cb2")
self.grd_main.addWidget(self.cb_cb2, 0, 2, 1, 1)
self.lbl_select2 = QtWidgets.QLabel(self.widget)
self.lbl_select2.setAlignment(
QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignTrailing
| QtCore.Qt.AlignmentFlag.AlignVCenter)
self.lbl_select2.setObjectName("lbl_select2")
self.grd_main.addWidget(self.lbl_select2, 0, 0, 1, 2)
MainWindow.setCentralWidget(self.centralwidget)
self.actionExit = QtGui.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "I am the parent application window"))
self.lbl_title.setText(_translate("MainWindow", "Main Application"))
self.lbl_select.setText(_translate("MainWindow", "Select an object:"))
self.btn_load_objects.setText(_translate("MainWindow", "Load"))
self.btn_openchild.setText(_translate("MainWindow", "Open Child Window"))
self.lbl_select2.setText(_translate("MainWindow", "Select an option to load:"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
# Get list of cb2
self.select2_list = <list obtained from file>
self.cb_cb2.addItems(self.select2_list)
def load_object_button_clicked(self):
self.cb_cb1.addItems(<items retrieved using a call with self.cb_cb2.currentText()>)
self.btn_openchild.setEnabled(True)
def openchild_button_clicked(self):
child_window = QtWidgets.QMainWindow()
child_ui = Ui_ChildWindow()
child_ui.setupUi(child_window)
child_window.show()
class Ui_ChildWindow(object):
def setupUi(self, ChildWindow):
ChildWindow.setObjectName("ChildWindow")
ChildWindow.resize(730, 600)
ChildWindow.setMinimumSize(QtCore.QSize(730, 600))
ChildWindow.setMaximumSize(QtCore.QSize(730, 600))
self.centralwidget = QtWidgets.QWidget(ChildWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbl_child_title = QtWidgets.QLabel(self.centralwidget)
self.lbl_child_title.setGeometry(QtCore.QRect(0, 0, 730, 25))
self.lbl_child_title.setMinimumSize(QtCore.QSize(730, 25))
self.lbl_child_title.setMaximumSize(QtCore.QSize(730, 25))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(13)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.lbl_child_title.setFont(font)
self.lbl_child_title.setAutoFillBackground(False)
self.lbl_child_title.setStyleSheet("background-color: rgb(11, 91, 172);\n"
"font: 13pt \"Verdana\";\n"
"color: white;")
self.lbl_child_title.setFrameShape(QtWidgets.QFrame.Shape.Box)
self.lbl_child_title.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
self.lbl_child_title.setLineWidth(2)
self.lbl_child_title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.lbl_child_title.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.NoTextInteraction)
self.lbl_child_title.setObjectName("lbl_child_title")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(82, 40, 567, 180))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.grd_runactions = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.grd_runactions.setContentsMargins(0, 0, 0, 0)
self.grd_runactions.setObjectName("grd_runactions")
self.btn_runprechecks = QtWidgets.QPushButton(self.gridLayoutWidget)
self.btn_runprechecks.setObjectName("btn_runprechecks")
self.grd_runactions.addWidget(self.btn_runprechecks, 0, 1, 1, 1)
self.lbl_precheckstatus = QtWidgets.QLabel(self.gridLayoutWidget)
self.lbl_precheckstatus.setObjectName("lbl_precheckstatus")
self.grd_runactions.addWidget(self.lbl_precheckstatus, 1, 1, 1, 1)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
self.grd_runactions.addItem(spacerItem, 2, 1, 1, 1)
self.btn_runmanual = QtWidgets.QPushButton(self.gridLayoutWidget)
self.btn_runmanual.setObjectName("btn_runmanual")
self.grd_runactions.addWidget(self.btn_runmanual, 4, 2, 1, 1)
self.btn_dryrun = QtWidgets.QPushButton(self.gridLayoutWidget)
self.btn_dryrun.setEnabled(False)
self.btn_dryrun.setObjectName("btn_dryrun")
self.grd_runactions.addWidget(self.btn_dryrun, 3, 0, 1, 1)
self.btn_runauto = QtWidgets.QPushButton(self.gridLayoutWidget)
self.btn_runauto.setEnabled(False)
self.btn_runauto.setObjectName("btn_runauto")
self.grd_runactions.addWidget(self.btn_runauto, 4, 0, 1, 1)
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 230, 711, 341))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.lbl_actionlog = QtWidgets.QLabel(self.verticalLayoutWidget)
self.lbl_actionlog.setAlignment(QtCore.Qt.AlignmentFlag.AlignBottom|QtCore.Qt.AlignmentFlag.AlignHCenter)
self.lbl_actionlog.setObjectName("lbl_actionlog")
self.verticalLayout.addWidget(self.lbl_actionlog)
self.txt_actionlog = QtWidgets.QTextEdit(self.verticalLayoutWidget)
self.txt_actionlog.setEnabled(True)
self.txt_actionlog.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.TextSelectableByKeyboard|QtCore.Qt.TextInteractionFlag.TextSelectableByMouse)
self.txt_actionlog.setObjectName("txt_actionlog")
self.verticalLayout.addWidget(self.txt_actionlog)
ChildWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(ChildWindow)
self.statusbar.setObjectName("statusbar")
ChildWindow.setStatusBar(self.statusbar)
self.retranslateUi(ChildWindow)
QtCore.QMetaObject.connectSlotsByName(ChildWindow)
def retranslateUi(self, ChildWindow):
_translate = QtCore.QCoreApplication.translate
ChildWindow.setWindowTitle(_translate("ChildWindow", "I am the child window of the parent window"))
self.lbl_child_title.setText(_translate("ChildWindow", "Child Window"))
self.btn_runprechecks.setText(_translate("ChildWindow", "Run Pre-Checks"))
self.lbl_precheckstatus.setText(_translate("ChildWindow", "Precheck Status: Not Run"))
self.btn_runmanual.setText(_translate("ChildWindow", "Run Manual"))
self.btn_dryrun.setText(_translate("ChildWindow", "Perform Dry Run"))
self.btn_runauto.setText(_translate("ChildWindow", "Run Auto"))
self.lbl_actionlog.setText(_translate("ChildWindow", "Action Log"))
...
button click functions, etc
...
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainwindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(mainwindow)
mainwindow.show()
sys.exit(app.exec())

Related

How to open multiple instances of one QDialog object in PyQt5? [duplicate]

I'm using PyQt5 designer to make a little application. I mostly work with designer rather than building from scratch so this might be a silly question. Basically i have a main menu and a "create" button that when clicked will open up the other pyqt5 .py file (also created in designer) i have been working on.
So far i complied the ui files into .py files and imported the popup window that i want to be able to generate more than one of and then i use this code to some success:
I have not added any additonal code to the converted ui files excpect this function:
Here is some code from my mainmenu and popoutwindow .py files:
this is the setupUi being called from my file im trying to replicate
class BookPopout(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(772, 685)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lastNameLabel = QtWidgets.QLabel(self.centralwidget)
self.lastNameLabel.setGeometry(QtCore.QRect(20, 110, 60, 17))
self.lastNameLabel.setObjectName("lastNameLabel")
self.cityEntry = QtWidgets.QLineEdit(self.centralwidget)
self.cityEntry.setGeometry(QtCore.QRect(20, 310, 391, 27))
self.cityEntry.setReadOnly(True)
self.cityEntry.setObjectName("cityEntry")
self.bookTable = QtWidgets.QTableWidget(self.centralwidget)
self.bookTable.setGeometry(QtCore.QRect(470, 10, 281, 481))
font = QtGui.QFont()
font.setPointSize(7)
self.bookTable.setFont(font)
self.bookTable.setAutoFillBackground(False)
self.bookTable.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.bookTable.setRowCount(1)
self.bookTable.setColumnCount(1)
self.bookTable.setObjectName("bookTable")
item = QtWidgets.QTableWidgetItem()
self.bookTable.setItem(0, 0, item)
self.bookTable.horizontalHeader().setVisible(False)
self.bookTable.horizontalHeader().setCascadingSectionResizes(False)
self.bookTable.horizontalHeader().setDefaultSectionSize(100)
self.bookTable.horizontalHeader().setStretchLastSection(True)
self.addressLineOneEntry = QtWidgets.QLineEdit(self.centralwidget)
self.addressLineOneEntry.setGeometry(QtCore.QRect(20, 190, 391, 27))
self.addressLineOneEntry.setReadOnly(True)
self.addressLineOneEntry.setObjectName("addressLineOneEntry")
self.stateEntry = QtWidgets.QLineEdit(self.centralwidget)
self.stateEntry.setGeometry(QtCore.QRect(20, 380, 391, 27))
self.stateEntry.setReadOnly(True)
self.stateEntry.setObjectName("stateEntry")
self.firstNameEntry = QtWidgets.QLineEdit(self.centralwidget)
self.firstNameEntry.setGeometry(QtCore.QRect(20, 70, 391, 27))
self.firstNameEntry.setReadOnly(True)
self.firstNameEntry.setObjectName("firstNameEntry")
self.streetAddressLabel = QtWidgets.QLabel(self.centralwidget)
self.streetAddressLabel.setGeometry(QtCore.QRect(20, 170, 83, 17))
self.streetAddressLabel.setObjectName("streetAddressLabel")
self.streetAddressTwoLabel = QtWidgets.QLabel(self.centralwidget)
self.streetAddressTwoLabel.setGeometry(QtCore.QRect(20, 240, 220, 17))
self.streetAddressTwoLabel.setObjectName("streetAddressTwoLabel")
self.phoneNumberLabel = QtWidgets.QLabel(self.centralwidget)
self.phoneNumberLabel.setGeometry(QtCore.QRect(20, 480, 87, 17))
self.phoneNumberLabel.setObjectName("phoneNumberLabel")
self.addressLineTwoEntry = QtWidgets.QLineEdit(self.centralwidget)
self.addressLineTwoEntry.setGeometry(QtCore.QRect(20, 260, 391, 27))
self.addressLineTwoEntry.setReadOnly(True)
self.addressLineTwoEntry.setObjectName("addressLineTwoEntry")
self.phoneNumberEntry = QtWidgets.QLineEdit(self.centralwidget)
self.phoneNumberEntry.setGeometry(QtCore.QRect(20, 500, 391, 27))
self.phoneNumberEntry.setReadOnly(True)
self.phoneNumberEntry.setObjectName("phoneNumberEntry")
self.firstNameLabel = QtWidgets.QLabel(self.centralwidget)
self.firstNameLabel.setGeometry(QtCore.QRect(20, 40, 62, 17))
self.firstNameLabel.setObjectName("firstNameLabel")
self.cityLabel = QtWidgets.QLabel(self.centralwidget)
self.cityLabel.setGeometry(QtCore.QRect(20, 290, 23, 17))
self.cityLabel.setObjectName("cityLabel")
self.stateLabel = QtWidgets.QLabel(self.centralwidget)
self.stateLabel.setGeometry(QtCore.QRect(20, 350, 29, 17))
self.stateLabel.setObjectName("stateLabel")
self.zipEntry = QtWidgets.QLineEdit(self.centralwidget)
self.zipEntry.setGeometry(QtCore.QRect(20, 440, 391, 27))
self.zipEntry.setReadOnly(True)
self.zipEntry.setObjectName("zipEntry")
self.zipLabel = QtWidgets.QLabel(self.centralwidget)
self.zipLabel.setGeometry(QtCore.QRect(20, 410, 18, 17))
self.zipLabel.setObjectName("zipLabel")
self.emailEntry = QtWidgets.QLineEdit(self.centralwidget)
self.emailEntry.setGeometry(QtCore.QRect(20, 550, 391, 27))
self.emailEntry.setObjectName("emailEntry")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 530, 80, 17))
self.label.setObjectName("label")
self.confirmNewPersonButton = QtWidgets.QPushButton(self.centralwidget)
self.confirmNewPersonButton.setGeometry(QtCore.QRect(20, 590, 85, 27))
self.confirmNewPersonButton.setObjectName("confirmNewPersonButton")
self.lastNameEntry = QtWidgets.QLineEdit(self.centralwidget)
self.lastNameEntry.setEnabled(True)
self.lastNameEntry.setGeometry(QtCore.QRect(20, 130, 391, 27))
self.lastNameEntry.setText("")
self.lastNameEntry.setReadOnly(True)
self.lastNameEntry.setObjectName("lastNameEntry")
self.sortComboBox = QtWidgets.QComboBox(self.centralwidget)
self.sortComboBox.setGeometry(QtCore.QRect(500, 530, 101, 31))
self.sortComboBox.setObjectName("sortComboBox")
self.sortByLabel = QtWidgets.QLabel(self.centralwidget)
self.sortByLabel.setGeometry(QtCore.QRect(570, 500, 71, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.sortByLabel.setFont(font)
self.sortByLabel.setObjectName("sortByLabel")
self.sortButton = QtWidgets.QPushButton(self.centralwidget)
self.sortButton.setGeometry(QtCore.QRect(600, 530, 111, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.sortButton.setFont(font)
self.sortButton.setObjectName("sortButton")
self.addNewPersonButton = QtWidgets.QPushButton(self.centralwidget)
self.addNewPersonButton.setGeometry(QtCore.QRect(40, 10, 101, 27))
self.addNewPersonButton.setObjectName("addNewPersonButton")
self.editEntryButton = QtWidgets.QPushButton(self.centralwidget)
self.editEntryButton.setGeometry(QtCore.QRect(170, 10, 111, 27))
self.editEntryButton.setObjectName("editEntryButton")
self.confirmEditButton = QtWidgets.QPushButton(self.centralwidget)
self.confirmEditButton.setGeometry(QtCore.QRect(20, 590, 111, 27))
self.confirmEditButton.setObjectName("confirmEditButton")
self.deleteEntryButton = QtWidgets.QPushButton(self.centralwidget)
self.deleteEntryButton.setGeometry(QtCore.QRect(300, 10, 111, 27))
self.deleteEntryButton.setObjectName("deleteEntryButton")
self.sortByLabel_2 = QtWidgets.QLabel(self.centralwidget)
self.sortByLabel_2.setGeometry(QtCore.QRect(530, 570, 131, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.sortByLabel_2.setFont(font)
self.sortByLabel_2.setObjectName("sortByLabel_2")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(500, 600, 101, 31))
self.lineEdit.setObjectName("lineEdit")
self.sortButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.sortButton_2.setGeometry(QtCore.QRect(600, 600, 111, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.sortButton_2.setFont(font)
self.sortButton_2.setObjectName("sortButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setEnabled(True)
self.menubar.setGeometry(QtCore.QRect(0, 0, 772, 27))
self.menubar.setObjectName("menubar")
self.menuOpen = QtWidgets.QMenu(self.menubar)
self.menuOpen.setObjectName("menuOpen")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionSave_as = QtWidgets.QAction(MainWindow)
self.actionSave_as.setObjectName("actionSave_as")
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setObjectName("actionClose")
self.menuOpen.addAction(self.actionSave)
self.menuOpen.addSeparator()
self.menuOpen.addAction(self.actionSave_as)
self.menuOpen.addSeparator()
self.menuOpen.addAction(self.actionClose)
self.menubar.addAction(self.menuOpen.menuAction())
QtCore.QMetaObject.connectSlotsByName(MainWindow)
Here is my mainmenu py file:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(255, 410)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.createNewBookButton = QtWidgets.QPushButton(self.centralwidget)
self.createNewBookButton.setGeometry(QtCore.QRect(40, 220, 171, 41))
self.createNewBookButton.setObjectName("createNewBookButton")
self.createNewBookButton.clicked.connect(self.openNewBook)
self.openExistingBookButton = QtWidgets.QPushButton(self.centralwidget)
self.openExistingBookButton.setGeometry(QtCore.QRect(40, 280, 171, 41))
self.openExistingBookButton.setObjectName("openExistingBookButton")
self.quitProgramButton = QtWidgets.QPushButton(self.centralwidget)
self.quitProgramButton.setGeometry(QtCore.QRect(40, 340, 171, 41))
self.quitProgramButton.setObjectName("quitProgramButton")
self.mainImage = QtWidgets.QGraphicsView(self.centralwidget)
self.mainImage.setGeometry(QtCore.QRect(10, 10, 231, 192))
self.mainImage.setObjectName("mainImage")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def openNewBook(self):
self.popWin = QtWidgets.QMainWindow()
self.bookUI = bookPopout.BookPopout()
self.bookUI.setupUi(self.popWin)
self.popWin.show()
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_())
This works great for opening up ONE extra window, which stays open while my main window is open which is what i want, but then when i go to click to make another window appear, it just replaces the one i already had up, so i can only seem to have one extra at a time.
I figure that my old window is being referenced by self.popWin , and then being replaced over and over so my question i guess is what is the best way to go about "generating" windows instead of just referencing the same window over and over.. i tried to remove the "self" part but then the window just dosent even appear when i go to create a new window so i am kind of lost
for example i would expect to be able to write something like this but im not really sure how to go about doing so
currentWindows = []
newPopWin = QtWidgets.QMainWindow()
newBookUI = BookPopout()
newBookUI.setupUi(newPopWin)
newPopWin.show()
currentWindows.append(newPopWin)
this would be ideal so i could keep track of all current open windows so i may terminate them all once i close my main menu.
Any help would be appreciated to put me on the right track, i have tried looking up solutions but lots of examples are not using code converted from a UI file.
The problem is simple, we are overwriting an attribute and at that moment the previous element is deleted, for that reason only a window. As you point out the solution is to store it in a list.
But before that, I recommend you not to write your logic in the .py generated by Qt Designer since for example let's say that you want to modify something of the design, then when generating the new .py it will erase all your logic. What PyQt recommends is to use create another file where you generate the logic adviritiendote with the message: # WARNING! All changes made in this file will be lost!. So restore the previous files I called bookPopup.py and mainmenu.py by removing the openNewBook. Following the docs recommendation we obtain the following:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
import bookPopout
import mainmenu
class BookPopoutWindow(QtWidgets.QMainWindow, bookPopout.BookPopout):
def __init__(self, parent=None):
super(BookPopoutWindow, self).__init__(parent)
self.setupUi(self)
class MainMenu(QtWidgets.QMainWindow, mainmenu.Ui_MainWindow):
def __init__(self, parent=None):
super(MainMenu, self).__init__(parent)
self.setupUi(self)
self.createNewBookButton.clicked.connect(self.openNewBook)
self.popups = []
#QtCore.pyqtSlot()
def openNewBook(self):
popWin = BookPopoutWindow()
popWin.show()
self.popups.append(popWin)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainMenu()
w.show()
sys.exit(app.exec_())
PyQt5 Opening Multiple Windows/Widgets and Closing them
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QAction
import random
class Widget2(QWidget):
def __init__(self):
super().__init__()
w2btn = QPushButton('Another Widget ' +
str(random.randrange(5, 100, 5)), self)
w2btn.clicked.connect(self.w2btnclicked)
w2btn.resize(w2btn.sizeHint())
w2btn.move(50, 50)
def w2btnclicked(self):
print("Widget 2 btn clicked")
class Widget1(QWidget):
def __init__(self):
super().__init__()
self.start = 50
self.end = 50
quit = QAction("Quit", self)
quit.triggered.connect(self.close)
addbtn = QPushButton('Add Window', self)
addbtn.clicked.connect(self.addbtnclicked)
addbtn.resize(addbtn.sizeHint())
addbtn.move(50, 50)
quitbtn = QPushButton('Quit', self)
quitbtn.clicked.connect(QApplication.instance().quit)
quitbtn.resize(quitbtn.sizeHint())
quitbtn.move(50, 100)
self.popups = []
def addbtnclicked(self):
print("Add Button Clicked!!")
wdgt2 = Widget2()
wdgt2.show()
if self.start > 1600:
self.start = 50
self.end = self.end + 250
wdgt2.setGeometry(self.start, self.end, 200, 200)
self.popups.append(wdgt2)
self.start = self.start + 250
def closeEvent(self, event):
print("In Close Event")
QApplication.closeAllWindows()
def main():
app = QApplication(sys.argv)
ex = Widget1()
ex.show()
ex.setGeometry(800, 600, 200, 200)
sys.exit(app.exec_())
if __name__ == '__main__':
main()

How to create Windows GUI application to play videos

This is the Code i am trying below.
I want to play .avi, .mp4, etc.
I am able to select video but unable to play.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia, QtMultimediaWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.mediaPlayer = QtMultimedia.QMediaPlayer(None, QtMultimedia.QMediaPlayer.VideoSurface)
videoWidget = QtMultimediaWidgets.QVideoWidget()
MainWindow.setObjectName("MainWindow")
MainWindow.resize(881, 578)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout_2 = QtWidgets.QGridLayout(self.frame)
self.gridLayout_2.setObjectName("gridLayout_2")
self.widget = QVideoWidget(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setMinimumSize(QtCore.QSize(711, 471))
self.widget.setMaximumSize(QtCore.QSize(711, 471))
self.widget.setObjectName("widget")
self.gridLayout_2.addWidget(self.widget, 0, 0, 3, 1)
self.audiobar = QtWidgets.QSlider(self.frame)
self.audiobar.setMinimumSize(QtCore.QSize(22, 160))
self.audiobar.setMaximumSize(QtCore.QSize(22, 160))
self.audiobar.setOrientation(QtCore.Qt.Vertical)
self.audiobar.setObjectName("audiobar")
self.gridLayout_2.addWidget(self.audiobar, 2, 2, 1, 1)
self.videobar = QtWidgets.QSlider(self.frame)
self.videobar.setMinimumSize(QtCore.QSize(711, 22))
self.videobar.setMaximumSize(QtCore.QSize(711, 22))
self.videobar.setOrientation(QtCore.Qt.Horizontal)
self.videobar.setObjectName("videobar")
self.gridLayout_2.addWidget(self.videobar, 3, 0, 1, 1)
self.frame_2 = QtWidgets.QFrame(self.frame)
self.frame_2.setMinimumSize(QtCore.QSize(120, 111))
self.frame_2.setMaximumSize(QtCore.QSize(120, 111))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.playbtn = QtWidgets.QPushButton(self.frame_2)
self.playbtn.setGeometry(QtCore.QRect(20, 10, 75, 23))
self.playbtn.setObjectName("playbtn")
self.playbtn.setEnabled(False)
self.pausebtn = QtWidgets.QPushButton(self.frame_2)
self.pausebtn.setGeometry(QtCore.QRect(20, 40, 75, 23))
self.pausebtn.setObjectName("pausebtn")
self.stopbtn = QtWidgets.QPushButton(self.frame_2)
self.stopbtn.setGeometry(QtCore.QRect(20, 70, 75, 23))
self.stopbtn.setObjectName("stopbtn")
self.gridLayout_2.addWidget(self.frame_2, 0, 2, 1, 1)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout_2.addItem(spacerItem, 0, 1, 1, 1)
self.gridLayout.addWidget(self.frame, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 881, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setObjectName("actionClose")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionClose)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.mediaPlayer.setVideoOutput(videoWidget)
self.actionOpen.triggered.connect(self.openFile)
self.playbtn.clicked.connect(self.play)
self.mediaPlayer.setVideoOutput(videoWidget)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MPlayer"))
self.playbtn.setText(_translate("MainWindow", "Play"))
self.pausebtn.setText(_translate("MainWindow", "Pause"))
self.stopbtn.setText(_translate("MainWindow", "Stop"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionClose.setText(_translate("MainWindow", "Close"))
def openFile(self):
fileName, _ = QFileDialog.getOpenFileName(None, "Select Video", " ", "Video Files (*.mp4 *.avi)",
QtCore.QDir.homePath())
if fileName != '':
self.mediaPlayer.setMedia(
QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(fileName)))
self.playbtn.setEnabled(True)
self.statusbar.showMessage(fileName)
self.play()
def play(self):
if self.mediaPlayer.state() == QtMultimedia.QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
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 set the video output to videoWidget, but you didn't add it to the interface.
You already created a video widget, though, and it is self.widget, so you just have to use that (and only once):
self.mediaPlayer.setVideoOutput(self.widget)
Note that you are using the output of pyuic for your program, which is highly discouraged. Follow the guidelines about using Designer instead.

PyQt5 UI crashes while code is running a function

im pretty new to working with pyqt5 in python, everything is kinda running how it is supposed to be. the only issue i have is that when im triggering to start the function stepperaction(self, index) with the "START" button, i can see the code running in the console, but the UI crashes while the code is running if i click anywhere on the ui with my mouse. i would like to be able to press the "STOP" button in the UI even while the function is running. How am i able to fix this?
# -*- coding: utf-8 -*-
# Created by: PyQt5 UI code generator 5.13.0
import time
import sys
# import RPi.GPIO as GPIO
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QVariant
stepcount = 0
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(557, 320)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setEnabled(True)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(360, 40, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(360, 70, 75, 23))
self.pushButton_4.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(270, 70, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(150, 40, 101, 22))
self.comboBox.setEditable(False)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("", QVariant(10*1600))
self.comboBox.addItem("", QVariant(20))
self.comboBox.addItem("", QVariant(30))
self.comboBox.addItem("", QVariant(40))
self.comboBox.addItem("", QVariant(50))
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 30, 121, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(20, 250, 118, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(270, 40, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(400, 252, 71, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(480, 252, 51, 21))
self.label_3.setObjectName("label_3")
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 557, 21))
self.menubar.setObjectName("menubar")
self.menuSmart_Spray_Booth_Configuration = QtWidgets.QMenu(self.menubar)
self.menuSmart_Spray_Booth_Configuration.setObjectName("menuSmart_Spray_Booth_Configuration")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuSmart_Spray_Booth_Configuration.menuAction())
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
self.comboBox.activated.connect(self.handleactivated)
self.pushButton.clicked.connect(lambda: self.stepperaction(index=self.comboBox.currentIndex()))
self.pushButton_2.clicked.connect(lambda: self.stop())
def handleactivated(self, index):
print(self.comboBox.itemText(index), "handle")
print(self.comboBox.itemData(index), "handle")
self.label_3.setText(self.comboBox.itemText(index))
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "Smart Spray Booth - Configuration"))
self.pushButton.setText(_translate("mainWindow", "START"))
self.pushButton_2.setText(_translate("mainWindow", "STOP"))
self.pushButton_3.setText(_translate("mainWindow", "Nullpunkt"))
self.comboBox.setItemText(0, _translate("mainWindow", "5cm"))
self.comboBox.setItemText(1, _translate("mainWindow", "10cm"))
self.comboBox.setItemText(2, _translate("mainWindow", "15cm"))
self.comboBox.setItemText(3, _translate("mainWindow", "20cm"))
self.comboBox.setItemText(4, _translate("mainWindow", "25cm"))
self.label.setText(_translate("mainWindow", "Bauteilgröße:"))
self.label_2.setText(_translate("mainWindow", "Bauteilgröße:"))
self.label_3.setText(_translate("mainWindow", "0 cm"))
self.menuSmart_Spray_Booth_Configuration.setTitle(_translate("mainWindow", "Smart Spray Booth - Configuration"))
if self.comboBox.currentTextChanged:
print(self.comboBox.currentData(), "from if")
def stop(self):
print("programm wird beendet")
sys.exit()
def stepperaction(self, index):
# GPIO.setmode(GPIO.BOARD) # read the pin as board instead of BCM pin
global stepcount
stepcount = self.comboBox.itemData(index)
print(stepcount, "this is given step")
# return stepcount
FastSpeed = 0.0001 # old = 0.001 Change this depends on your stepper motor
LowSpeed = 0.0001 # old = 0.001 Change this depends on your stepper motor
counter = 0
countery = 0
while True and counter < 3:
print("Move Up", stepcount, "steps")
for i in range(stepcount):
countery = countery + 1
print(countery)
time.sleep(LowSpeed)
print(countery)
time.sleep(LowSpeed)
time.sleep(1)
print("Move Down", stepcount, "steps")
for i in range(stepcount):
countery = countery + 1
print(countery)
time.sleep(FastSpeed)
print(countery)
time.sleep(FastSpeed)
time.sleep(1)
counter += 1
print("success")
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_())
I can't test on PyQt5, but on PySide you just need to pass the name of the function to connect, not execute it. It might also be better to avoid using lambdas in QT slots.
Try this modification:
....
self.pushButton.clicked.connect(self.stepperaction)
....
def stepperaction(self, index=None):
if index is None:
index = self.comboBox.currentIndex()
# GPIO.setmode(GPIO.BOARD) # read the pin as board instead of BCM pin
global stepcount
....

how to send variable from .ui file to another .ui file in the main python file

i have two .ui files that converted to python using pyuic5. first converted ui file is (LoginWindowUI.py) as below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LoginWindow(object):
def setupUi(self, LoginWindow):
LoginWindow.setObjectName("LoginWindow")
LoginWindow.setWindowModality(QtCore.Qt.NonModal)
LoginWindow.setEnabled(True)
LoginWindow.resize(446, 182)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(LoginWindow.sizePolicy().hasHeightForWidth())
LoginWindow.setSizePolicy(sizePolicy)
LoginWindow.setMouseTracking(False)
LoginWindow.setLayoutDirection(QtCore.Qt.RightToLeft)
LoginWindow.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
LoginWindow.setDocumentMode(False)
self.centralwidget = QtWidgets.QWidget(LoginWindow)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(194, 30, 187, 77))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.password_ldt = QtWidgets.QLineEdit(self.layoutWidget)
self.password_ldt.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
self.password_ldt.setText("")
self.password_ldt.setEchoMode(QtWidgets.QLineEdit.Password)
self.password_ldt.setCursorPosition(0)
self.password_ldt.setAlignment(QtCore.Qt.AlignCenter)
self.password_ldt.setObjectName("password_ldt")
self.gridLayout.addWidget(self.password_ldt, 1, 1, 1, 1)
self.password_lbl = QtWidgets.QLabel(self.layoutWidget)
self.password_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.password_lbl.setObjectName("password_lbl")
self.gridLayout.addWidget(self.password_lbl, 1, 0, 1, 1)
self.username_lbl = QtWidgets.QLabel(self.layoutWidget)
self.username_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.username_lbl.setObjectName("username_lbl")
self.gridLayout.addWidget(self.username_lbl, 0, 0, 1, 1)
self.username_ldt = QtWidgets.QLineEdit(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.username_ldt.sizePolicy().hasHeightForWidth())
self.username_ldt.setSizePolicy(sizePolicy)
self.username_ldt.setLocale(QtCore.QLocale(QtCore.QLocale.Persian, QtCore.QLocale.Iran))
self.username_ldt.setText("")
self.username_ldt.setEchoMode(QtWidgets.QLineEdit.Normal)
self.username_ldt.setAlignment(QtCore.Qt.AlignCenter)
self.username_ldt.setObjectName("username_ldt")
self.gridLayout.addWidget(self.username_ldt, 0, 1, 1, 1)
self.login_btn = QtWidgets.QPushButton(self.layoutWidget)
self.login_btn.setCursor(QtGui.QCursor(QtCore.Qt.ClosedHandCursor))
self.login_btn.setAutoDefault(False)
self.login_btn.setDefault(False)
self.login_btn.setObjectName("login_btn")
self.gridLayout.addWidget(self.login_btn, 2, 1, 1, 1)
self.description_lbl = QtWidgets.QLabel(self.centralwidget)
self.description_lbl.setGeometry(QtCore.QRect(30, 40, 141, 41))
self.description_lbl.setText("")
self.description_lbl.setAlignment(QtCore.Qt.AlignCenter)
self.description_lbl.setWordWrap(True)
self.description_lbl.setObjectName("description_lbl")
LoginWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(LoginWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 446, 21))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
LoginWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(LoginWindow)
self.statusbar.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.statusbar.setObjectName("statusbar")
LoginWindow.setStatusBar(self.statusbar)
self.action_cra = QtWidgets.QAction(LoginWindow)
self.action_cra.setObjectName("action_cra")
self.action_db = QtWidgets.QAction(LoginWindow)
self.action_db.setObjectName("action_db")
self.action_exit = QtWidgets.QAction(LoginWindow)
self.action_exit.setObjectName("action_exit")
self.menu.addAction(self.action_cra)
self.menu.addAction(self.action_db)
self.menu.addSeparator()
self.menu.addAction(self.action_exit)
self.menubar.addAction(self.menu.menuAction())
self.retranslateUi(LoginWindow)
self.action_exit.triggered.connect(LoginWindow.close)
QtCore.QMetaObject.connectSlotsByName(LoginWindow)
LoginWindow.setTabOrder(self.username_ldt, self.password_ldt)
LoginWindow.setTabOrder(self.password_ldt, self.login_btn)
def retranslateUi(self, LoginWindow):
_translate = QtCore.QCoreApplication.translate
self.password_ldt.setToolTip(_translate("LoginWindow", "رمز عبور خود را وارد کنید."))
self.password_lbl.setText(_translate("LoginWindow", " رمز عبور "))
self.username_lbl.setText(_translate("LoginWindow", "نام کاربری"))
self.username_ldt.setToolTip(_translate("LoginWindow", "لطفاً نام کاربری خود را وارد نمایید."))
self.login_btn.setText(_translate("LoginWindow", "ورود"))
self.menu.setTitle(_translate("LoginWindow", "درباره ما"))
self.action_cra.setText(_translate("LoginWindow", "A"))
self.action_db.setText(_translate("LoginWindow", "B"))
self.action_exit.setText(_translate("LoginWindow", "خروج"))
self.action_exit.setStatusTip(_translate("LoginWindow", "خروج"))
self.action_exit.setShortcut(_translate("LoginWindow", "Ctrl+Q"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
LoginWindow = QtWidgets.QMainWindow()
ui = Ui_LoginWindow()
ui.setupUi(LoginWindow)
LoginWindow.show()
sys.exit(app.exec_())
and the second converted .ui file is (MainWindowUI.py) as below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(452, 432)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.cralogo_lbl = QtWidgets.QLabel(self.centralwidget)
self.cralogo_lbl.setGeometry(QtCore.QRect(90, 30, 171, 161))
self.cralogo_lbl.setText("")
self.cralogo_lbl.setPixmap(QtGui.QPixmap("../../.designer/backup/ICONS/CraLogo.png"))
self.cralogo_lbl.setScaledContents(True)
self.cralogo_lbl.setObjectName("cralogo_lbl")
self.exit_btn = QtWidgets.QPushButton(self.centralwidget)
self.exit_btn.setGeometry(QtCore.QRect(140, 340, 125, 40))
self.exit_btn.setMinimumSize(QtCore.QSize(125, 40))
self.exit_btn.setLayoutDirection(QtCore.Qt.LeftToRight)
self.exit_btn.setCheckable(False)
self.exit_btn.setChecked(False)
self.exit_btn.setObjectName("exit_btn")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(20, 10, 389, 152))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.mobile_btn = QtWidgets.QPushButton(self.layoutWidget)
self.mobile_btn.setEnabled(False)
self.mobile_btn.setMinimumSize(QtCore.QSize(125, 40))
self.mobile_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.mobile_btn.setCheckable(False)
self.mobile_btn.setChecked(False)
self.mobile_btn.setObjectName("mobile_btn")
self.gridLayout.addWidget(self.mobile_btn, 1, 0, 1, 1)
self.dataentry_btn = QtWidgets.QPushButton(self.layoutWidget)
self.dataentry_btn.setMinimumSize(QtCore.QSize(125, 40))
self.dataentry_btn.setLayoutDirection(QtCore.Qt.LeftToRight)
self.dataentry_btn.setCheckable(False)
self.dataentry_btn.setChecked(False)
self.dataentry_btn.setObjectName("dataentry_btn")
self.gridLayout.addWidget(self.dataentry_btn, 0, 2, 1, 1)
self.village_btn = QtWidgets.QPushButton(self.layoutWidget)
self.village_btn.setEnabled(False)
self.village_btn.setMinimumSize(QtCore.QSize(125, 40))
self.village_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.village_btn.setCheckable(False)
self.village_btn.setChecked(False)
self.village_btn.setObjectName("village_btn")
self.gridLayout.addWidget(self.village_btn, 1, 1, 1, 1)
self.fix_btn = QtWidgets.QPushButton(self.layoutWidget)
self.fix_btn.setEnabled(False)
self.fix_btn.setMinimumSize(QtCore.QSize(125, 40))
self.fix_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.fix_btn.setCheckable(False)
self.fix_btn.setChecked(False)
self.fix_btn.setObjectName("fix_btn")
self.gridLayout.addWidget(self.fix_btn, 0, 0, 1, 1)
self.post_btn = QtWidgets.QPushButton(self.layoutWidget)
self.post_btn.setEnabled(False)
self.post_btn.setMinimumSize(QtCore.QSize(125, 40))
self.post_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.post_btn.setCheckable(False)
self.post_btn.setChecked(False)
self.post_btn.setObjectName("post_btn")
self.gridLayout.addWidget(self.post_btn, 2, 0, 1, 1)
self.city_btn = QtWidgets.QPushButton(self.layoutWidget)
self.city_btn.setEnabled(False)
self.city_btn.setMinimumSize(QtCore.QSize(125, 40))
self.city_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
self.city_btn.setCheckable(False)
self.city_btn.setChecked(False)
self.city_btn.setObjectName("city_btn")
self.gridLayout.addWidget(self.city_btn, 0, 1, 1, 1)
self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget1.setGeometry(QtCore.QRect(20, 190, 389, 134))
self.layoutWidget1.setObjectName("layoutWidget1")
self.gridLayout_2 = QtWidgets.QGridLayout(self.layoutWidget1)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.rep4_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep4_btn.setEnabled(False)
self.rep4_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep4_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep4_btn.setCheckable(False)
self.rep4_btn.setObjectName("rep4_btn")
self.gridLayout_2.addWidget(self.rep4_btn, 0, 0, 1, 1)
self.rep1_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep1_btn.setEnabled(False)
self.rep1_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep1_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep1_btn.setCheckable(False)
self.rep1_btn.setObjectName("rep1_btn")
self.gridLayout_2.addWidget(self.rep1_btn, 0, 1, 1, 1)
self.report_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.report_btn.setMinimumSize(QtCore.QSize(125, 40))
self.report_btn.setMaximumSize(QtCore.QSize(125, 40))
self.report_btn.setCheckable(False)
self.report_btn.setObjectName("report_btn")
self.gridLayout_2.addWidget(self.report_btn, 0, 2, 1, 1)
self.rep5_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep5_btn.setEnabled(False)
self.rep5_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep5_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep5_btn.setCheckable(False)
self.rep5_btn.setObjectName("rep5_btn")
self.gridLayout_2.addWidget(self.rep5_btn, 1, 0, 1, 1)
self.rep2_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep2_btn.setEnabled(False)
self.rep2_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep2_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep2_btn.setCheckable(False)
self.rep2_btn.setObjectName("rep2_btn")
self.gridLayout_2.addWidget(self.rep2_btn, 1, 1, 1, 1)
self.rep6_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep6_btn.setEnabled(False)
self.rep6_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep6_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep6_btn.setCheckable(False)
self.rep6_btn.setObjectName("rep6_btn")
self.gridLayout_2.addWidget(self.rep6_btn, 2, 0, 1, 1)
self.rep3_btn = QtWidgets.QPushButton(self.layoutWidget1)
self.rep3_btn.setEnabled(False)
self.rep3_btn.setMinimumSize(QtCore.QSize(125, 40))
self.rep3_btn.setMaximumSize(QtCore.QSize(125, 40))
self.rep3_btn.setCheckable(False)
self.rep3_btn.setObjectName("rep3_btn")
self.gridLayout_2.addWidget(self.rep3_btn, 2, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 452, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.exit_btn.clicked.connect(MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.cralogo_lbl.setStatusTip(_translate("MainWindow", "www.cra.ir"))
self.exit_btn.setText(_translate("MainWindow", "خروج"))
self.mobile_btn.setText(_translate("MainWindow", "4"))
self.dataentry_btn.setText(_translate("MainWindow", "A"))
self.village_btn.setText(_translate("MainWindow", "2"))
self.fix_btn.setText(_translate("MainWindow", "3"))
self.post_btn.setText(_translate("MainWindow", "5"))
self.city_btn.setText(_translate("MainWindow", "1"))
self.rep4_btn.setText(_translate("MainWindow", "4"))
self.rep1_btn.setText(_translate("MainWindow", "1"))
self.report_btn.setText(_translate("MainWindow", "B"))
self.rep5_btn.setText(_translate("MainWindow", "5"))
self.rep2_btn.setText(_translate("MainWindow", "2"))
self.rep6_btn.setText(_translate("MainWindow", "6"))
self.rep3_btn.setText(_translate("MainWindow", "3"))
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_())
and my main python file is:
from LoginWindowUI import Ui_LoginWindow
from MainWindowUI import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class Main_Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, db_user, db_pass, parent=None):
super(Main_Window, self).__init__(parent)
self.setupUi(self)
class LoginWindow(QtWidgets.QMainWindow, Ui_LoginWindow):
logged = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(LoginWindow, self).__init__(parent)
self.setupUi(self)
self.login_btn.clicked.connect(self.authenticate)
#QtCore.pyqtSlot()
def authenticate(self):
self.db_user = self.username_ldt.text()
self.db_pass = self.password_ldt.text()
if self.db_user == 'admin' and self.db_pass=='admin':
self.logged.emit()
self.close()
def call_Main_Window(username,password):
w = Main_Window(username, password)
w.setWindowTitle=username
w.show()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
login = LoginWindow()
login.show()
login.logged.connect(lambda :call_Main_Window(login.db_user, login.db_pass))
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I want that when user enter "admin","admin" in username and password this window colsed and open second ui file .
The main problem is that "w" is a local variable that will be deleted after running the function. One possible solution is to create the second window and not show it, in that second window you will have a method that receives the credentials and shows the window. In the case of the first window it is advisable to modify the signal so that it transmits both data.
from LoginWindowUI import Ui_LoginWindow
from MainWindowUI import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class Main_Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Main_Window, self).__init__(parent)
self.setupUi(self)
#QtCore.pyqtSlot(str, str)
def setCredentials(self, db_user, db_pass):
print(db_user, db_pass)
self.show()
class LoginWindow(QtWidgets.QMainWindow, Ui_LoginWindow):
logged = QtCore.pyqtSignal(str, str)
def __init__(self, parent=None):
super(LoginWindow, self).__init__(parent)
self.setupUi(self)
self.login_btn.clicked.connect(self.authenticate)
#QtCore.pyqtSlot()
def authenticate(self):
self.db_user = self.username_ldt.text()
self.db_pass = self.password_ldt.text()
if self.db_user == 'admin' and self.db_pass=='admin':
self.logged.emit(self.db_user, self.db_pass)
self.close()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
w = Main_Window()
login = LoginWindow()
login.logged.connect(w.setCredentials)
login.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

linking pyQt files don't relate

I have a pyQt script (created with pyuic5 in terminal) that creates a window with some functions. This works and script is below:
from PyQt5 import QtCore, QtGui, QtWidgets
from Main import Ui_Main
from resources.icons import *
import sys
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent) #pyQt5
self.ui = Ui_Main()
self.ui.setupUi(self)
self.ui.setupUi
self.ui.retranslateUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
# measExit = self.ui.menuJBae_Meas.addAction(self.ui.actionExit)
# measExit.triggered.connect(self.Exit)
def Exit(self):
choice = QtGui.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainapp = MainW()
mainapp.show()
sys.exit(app.exec_())
The problem raises after this. I want to create some functions. I started with the a quit function in the menu.
I can't get my exit script linked to the script. I think it becomes because the standard file have an object main where all sub objects are related to.
I can't figure out how I can relate the my script (Now the upper one) with the standard script from pyuic5 (script below)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Main(object):
def setupUi(self, Main):
Main.setObjectName("Main")
Main.resize(885, 600)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(Main.sizePolicy().hasHeightForWidth())
Main.setSizePolicy(sizePolicy)
Main.setStyleSheet("font: 9pt \"SansSerif\";")
self.Central = QtWidgets.QWidget(Main)
self.Central.setObjectName("Central")
self.Logo = QtWidgets.QGraphicsView(self.Central)
self.Logo.setGeometry(QtCore.QRect(10, 10, 240, 121))
self.Logo.setMinimumSize(QtCore.QSize(240, 120))
self.Logo.setMaximumSize(QtCore.QSize(16777215, 350))
self.Logo.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedKingdom))
self.Logo.setFrameShadow(QtWidgets.QFrame.Raised)
self.Logo.setObjectName("Logo")
self.test = QtWidgets.QToolButton(self.Central)
self.test.setGeometry(QtCore.QRect(10, 520, 80, 23))
self.test.setObjectName("test")
self.start = QtWidgets.QToolButton(self.Central)
self.start.setGeometry(QtCore.QRect(110, 520, 131, 23))
self.start.setObjectName("start")
self.layoutWidget = QtWidgets.QWidget(self.Central)
self.layoutWidget.setGeometry(QtCore.QRect(260, 520, 521, 27))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.progressBar = QtWidgets.QProgressBar(self.layoutWidget)
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.horizontalLayout.addWidget(self.progressBar)
self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.layoutWidget)
self.dateTimeEdit.setObjectName("dateTimeEdit")
self.horizontalLayout.addWidget(self.dateTimeEdit)
self.plotTop = QtWidgets.QGraphicsView(self.Central)
self.plotTop.setGeometry(QtCore.QRect(260, 10, 521, 240))
self.plotTop.setMinimumSize(QtCore.QSize(0, 240))
self.plotTop.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.plotTop.setFrameShadow(QtWidgets.QFrame.Sunken)
self.plotTop.setObjectName("plotTop")
self.PlotBottom = QtWidgets.QGraphicsView(self.Central)
self.PlotBottom.setGeometry(QtCore.QRect(260, 260, 521, 240))
self.PlotBottom.setMinimumSize(QtCore.QSize(0, 240))
self.PlotBottom.setObjectName("PlotBottom")
self.layoutWidget1 = QtWidgets.QWidget(self.Central)
self.layoutWidget1.setGeometry(QtCore.QRect(800, 10, 28, 231))
self.layoutWidget1.setObjectName("layoutWidget1")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget1)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
spacerItem = QtWidgets.QSpacerItem(20, 180, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.ViewPropTop = QtWidgets.QToolButton(self.layoutWidget1)
self.ViewPropTop.setObjectName("ViewPropTop")
self.verticalLayout.addWidget(self.ViewPropTop)
self.zoomTop = QtWidgets.QToolButton(self.layoutWidget1)
self.zoomTop.setObjectName("zoomTop")
self.verticalLayout.addWidget(self.zoomTop)
self.toolTop = QtWidgets.QToolButton(self.layoutWidget1)
self.toolTop.setObjectName("toolTop")
self.verticalLayout.addWidget(self.toolTop)
self.layoutWidget2 = QtWidgets.QWidget(self.Central)
self.layoutWidget2.setGeometry(QtCore.QRect(800, 260, 28, 231))
self.layoutWidget2.setObjectName("layoutWidget2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
spacerItem1 = QtWidgets.QSpacerItem(20, 180, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_2.addItem(spacerItem1)
self.ViewPropBBottom = QtWidgets.QToolButton(self.layoutWidget2)
self.ViewPropBBottom.setObjectName("ViewPropBBottom")
self.verticalLayout_2.addWidget(self.ViewPropBBottom)
self.ZoomBottom = QtWidgets.QToolButton(self.layoutWidget2)
self.ZoomBottom.setObjectName("ZoomBottom")
self.verticalLayout_2.addWidget(self.ZoomBottom)
self.toolBottom = QtWidgets.QToolButton(self.layoutWidget2)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../resources/icons/WrenchSm.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.toolBottom.setIcon(icon)
self.toolBottom.setObjectName("toolBottom")
self.verticalLayout_2.addWidget(self.toolBottom)
self.layoutWidget3 = QtWidgets.QWidget(self.Central)
self.layoutWidget3.setGeometry(QtCore.QRect(10, 170, 231, 271))
self.layoutWidget3.setObjectName("layoutWidget3")
self.gridLayout_3 = QtWidgets.QGridLayout(self.layoutWidget3)
self.gridLayout_3.setContentsMargins(0, 0, 0, 0)
self.gridLayout_3.setObjectName("gridLayout_3")
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.TypeLab = QtWidgets.QLabel(self.layoutWidget3)
self.TypeLab.setObjectName("TypeLab")
self.verticalLayout_3.addWidget(self.TypeLab)
self.TypeSel = QtWidgets.QComboBox(self.layoutWidget3)
self.TypeSel.setMinimumSize(QtCore.QSize(140, 0))
self.TypeSel.setStatusTip("")
self.TypeSel.setObjectName("TypeSel")
self.verticalLayout_3.addWidget(self.TypeSel)
self.gridLayout_3.addLayout(self.verticalLayout_3, 0, 0, 2, 1)
self.label = QtWidgets.QLabel(self.layoutWidget3)
self.label.setObjectName("label")
self.gridLayout_3.addWidget(self.label, 0, 1, 1, 1)
self.Level = QtWidgets.QDial(self.layoutWidget3)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.Level.sizePolicy().hasHeightForWidth())
self.Level.setSizePolicy(sizePolicy)
self.Level.setMinimumSize(QtCore.QSize(75, 75))
self.Level.setStatusTip("")
self.Level.setAccessibleDescription("")
self.Level.setObjectName("Level")
self.gridLayout_3.addWidget(self.Level, 1, 1, 2, 1)
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.lengthLab = QtWidgets.QLabel(self.layoutWidget3)
self.lengthLab.setObjectName("lengthLab")
self.verticalLayout_4.addWidget(self.lengthLab)
self.length = QtWidgets.QComboBox(self.layoutWidget3)
self.length.setMinimumSize(QtCore.QSize(140, 0))
self.length.setObjectName("length")
self.verticalLayout_4.addWidget(self.length)
self.gridLayout_3.addLayout(self.verticalLayout_4, 2, 0, 1, 1)
self.verticalLayout_5 = QtWidgets.QVBoxLayout()
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.itterationsLab = QtWidgets.QLabel(self.layoutWidget3)
self.itterationsLab.setObjectName("itterationsLab")
self.verticalLayout_5.addWidget(self.itterationsLab)
self.Itterations = QtWidgets.QComboBox(self.layoutWidget3)
self.Itterations.setMinimumSize(QtCore.QSize(140, 0))
self.Itterations.setAccessibleDescription("")
self.Itterations.setObjectName("Itterations")
self.verticalLayout_5.addWidget(self.Itterations)
self.gridLayout_3.addLayout(self.verticalLayout_5, 3, 0, 1, 1)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.startLab = QtWidgets.QLabel(self.layoutWidget3)
self.startLab.setObjectName("startLab")
self.gridLayout.addWidget(self.startLab, 0, 0, 1, 1)
self.startSlid = QtWidgets.QSlider(self.layoutWidget3)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.startSlid.sizePolicy().hasHeightForWidth())
self.startSlid.setSizePolicy(sizePolicy)
self.startSlid.setMinimumSize(QtCore.QSize(140, 0))
self.startSlid.setMaximum(100000)
self.startSlid.setOrientation(QtCore.Qt.Horizontal)
self.startSlid.setObjectName("startSlid")
self.gridLayout.addWidget(self.startSlid, 1, 0, 1, 1)
self.startSpin = QtWidgets.QDoubleSpinBox(self.layoutWidget3)
self.startSpin.setMinimumSize(QtCore.QSize(65, 0))
self.startSpin.setDecimals(0)
self.startSpin.setMaximum(100000.0)
self.startSpin.setProperty("value", 1.0)
self.startSpin.setObjectName("startSpin")
self.gridLayout.addWidget(self.startSpin, 1, 1, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout, 4, 0, 1, 2)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.endLab = QtWidgets.QLabel(self.layoutWidget3)
self.endLab.setObjectName("endLab")
self.gridLayout_2.addWidget(self.endLab, 0, 0, 1, 1)
self.endSlid = QtWidgets.QSlider(self.layoutWidget3)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.endSlid.sizePolicy().hasHeightForWidth())
self.endSlid.setSizePolicy(sizePolicy)
self.endSlid.setMinimumSize(QtCore.QSize(140, 0))
self.endSlid.setMaximum(100000)
self.endSlid.setOrientation(QtCore.Qt.Horizontal)
self.endSlid.setObjectName("endSlid")
self.gridLayout_2.addWidget(self.endSlid, 1, 0, 1, 1)
self.endSpin = QtWidgets.QDoubleSpinBox(self.layoutWidget3)
self.endSpin.setMinimumSize(QtCore.QSize(65, 0))
self.endSpin.setDecimals(0)
self.endSpin.setMaximum(100000.0)
self.endSpin.setProperty("value", 100000.0)
self.endSpin.setObjectName("endSpin")
self.gridLayout_2.addWidget(self.endSpin, 1, 1, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout_2, 5, 0, 1, 2)
self.layoutWidget.raise_()
self.Logo.raise_()
self.test.raise_()
self.start.raise_()
self.plotTop.raise_()
self.layoutWidget.raise_()
self.PlotBottom.raise_()
self.layoutWidget.raise_()
self.layoutWidget.raise_()
Main.setCentralWidget(self.Central)
self.menubar = QtWidgets.QMenuBar(Main)
self.menubar.setGeometry(QtCore.QRect(0, 0, 885, 20))
self.menubar.setObjectName("menubar")
self.menuJBae_Meas = QtWidgets.QMenu(self.menubar)
self.menuJBae_Meas.setObjectName("menuJBae_Meas")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
Main.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Main)
self.statusbar.setObjectName("statusbar")
Main.setStatusBar(self.statusbar)
self.actionExit = QtWidgets.QAction(Main)
self.actionExit.setEnabled(True)
self.actionExit.setObjectName("actionExit")
self.actionSave = QtWidgets.QAction(Main)
self.actionSave.setObjectName("actionSave")
self.actionSave_All = QtWidgets.QAction(Main)
self.actionSave_All.setObjectName("actionSave_All")
self.actionPreferences = QtWidgets.QAction(Main)
self.actionPreferences.setObjectName("actionPreferences")
self.actionSave_As = QtWidgets.QAction(Main)
self.actionSave_As.setObjectName("actionSave_As")
self.actionDelete_Measurement = QtWidgets.QAction(Main)
self.actionDelete_Measurement.setObjectName("actionDelete_Measurement")
self.actionOpen = QtWidgets.QAction(Main)
self.actionOpen.setObjectName("actionOpen")
self.actionNew = QtWidgets.QAction(Main)
self.actionNew.setObjectName("actionNew")
self.menuJBae_Meas.addAction(self.actionNew)
self.menuJBae_Meas.addAction(self.actionOpen)
self.menuJBae_Meas.addAction(self.actionSave)
self.menuJBae_Meas.addAction(self.actionSave_As)
self.menuJBae_Meas.addAction(self.actionSave_All)
self.menuJBae_Meas.addSeparator()
self.menuJBae_Meas.addAction(self.actionPreferences)
self.menuJBae_Meas.addAction(self.actionExit)
self.menuEdit.addAction(self.actionDelete_Measurement)
self.menubar.addAction(self.menuJBae_Meas.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.retranslateUi(Main)
QtCore.QMetaObject.connectSlotsByName(Main)
Main.setTabOrder(self.TypeSel, self.length)
Main.setTabOrder(self.length, self.Itterations)
Main.setTabOrder(self.Itterations, self.startSlid)
Main.setTabOrder(self.startSlid, self.startSpin)
Main.setTabOrder(self.startSpin, self.endSlid)
Main.setTabOrder(self.endSlid, self.endSpin)
Main.setTabOrder(self.endSpin, self.Level)
Main.setTabOrder(self.Level, self.start)
Main.setTabOrder(self.start, self.test)
Main.setTabOrder(self.test, self.ViewPropTop)
Main.setTabOrder(self.ViewPropTop, self.zoomTop)
Main.setTabOrder(self.zoomTop, self.toolTop)
Main.setTabOrder(self.toolTop, self.ViewPropBBottom)
Main.setTabOrder(self.ViewPropBBottom, self.ZoomBottom)
Main.setTabOrder(self.ZoomBottom, self.toolBottom)
Main.setTabOrder(self.toolBottom, self.plotTop)
Main.setTabOrder(self.plotTop, self.PlotBottom)
Main.setTabOrder(self.PlotBottom, self.dateTimeEdit)
Main.setTabOrder(self.dateTimeEdit, self.Logo)
def retranslateUi(self, Main):
_translate = QtCore.QCoreApplication.translate
Main.setWindowTitle(_translate("Main", "Main"))
self.test.setToolTip(_translate("Main", "Open Menu Test Signal"))
self.test.setText(_translate("Main", "Test"))
self.start.setToolTip(_translate("Main", "Start Measurement"))
self.start.setText(_translate("Main", "Start"))
self.ViewPropTop.setText(_translate("Main", "..."))
self.zoomTop.setText(_translate("Main", "..."))
self.toolTop.setText(_translate("Main", "..."))
self.ViewPropBBottom.setText(_translate("Main", "..."))
self.ZoomBottom.setText(_translate("Main", "..."))
self.toolBottom.setText(_translate("Main", "..."))
self.TypeLab.setText(_translate("Main", "Type"))
self.TypeSel.setToolTip(_translate("Main", "Select Signal Type"))
self.label.setText(_translate("Main", "Level"))
self.Level.setToolTip(_translate("Main", "Set Volume Level"))
self.lengthLab.setText(_translate("Main", "Length"))
self.length.setToolTip(_translate("Main", "Select Length of Signal in k samples"))
self.itterationsLab.setText(_translate("Main", "Itterations"))
self.Itterations.setToolTip(_translate("Main", "Select number of itterations"))
self.startLab.setText(_translate("Main", "Start"))
self.startSlid.setToolTip(_translate("Main", "Select Start Frequency"))
self.startSpin.setToolTip(_translate("Main", "Set Start Frequency"))
self.endLab.setText(_translate("Main", "End"))
self.endSlid.setToolTip(_translate("Main", "Select End Frequency"))
self.endSpin.setToolTip(_translate("Main", "Set End Frequency"))
self.menuJBae_Meas.setTitle(_translate("Main", "&File"))
self.menuEdit.setTitle(_translate("Main", "Edit"))
self.actionExit.setText(_translate("Main", "&Quit"))
self.actionExit.setIconText(_translate("Main", "Quit"))
self.actionExit.setToolTip(_translate("Main", "Exit Meas"))
self.actionExit.setShortcut(_translate("Main", "Ctrl+Q"))
self.actionSave.setText(_translate("Main", "&Save"))
self.actionSave.setIconText(_translate("Main", "&Save"))
self.actionSave.setToolTip(_translate("Main", "Save Measurements"))
self.actionSave.setShortcut(_translate("Main", "Ctrl+S"))
self.actionSave_All.setText(_translate("Main", "Save All"))
self.actionSave_All.setToolTip(_translate("Main", "Save All Measurements"))
self.actionSave_All.setShortcut(_translate("Main", "Ctrl+Shift+S"))
self.actionPreferences.setText(_translate("Main", "&Preferences"))
self.actionPreferences.setIconText(_translate("Main", "&Preferences"))
self.actionPreferences.setToolTip(_translate("Main", "Preferences Meas"))
self.actionPreferences.setShortcut(_translate("Main", "Ctrl+I"))
self.actionSave_As.setText(_translate("Main", "Save As"))
self.actionSave_As.setToolTip(_translate("Main", "Save Measurements As"))
self.actionDelete_Measurement.setText(_translate("Main", "Delete Measurement"))
self.actionOpen.setText(_translate("Main", "&Open"))
self.actionOpen.setIconText(_translate("Main", "&Open"))
self.actionOpen.setShortcut(_translate("Main", "Ctrl+O"))
self.actionNew.setText(_translate("Main", "&New"))
self.actionNew.setIconText(_translate("Main", "&New"))
self.actionNew.setShortcut(_translate("Main", "Ctrl+N"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Main = QtWidgets.QMainWindow()
ui = Ui_Main()
ui.setupUi(Main)
Main.show()
sys.exit(app.exec_())
How get I this second script can use the objects from main ?
All of the widgets added in Qt Designer become attributes of the object assigned to self.ui, which is just a simple namespace. So you can access actionExit and connect it to a slot, like this:
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.ui = Ui_Main()
self.ui.setupUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
self.ui.actionExit.triggered.connect(self.Exit)
But note that the Exit method has some errors, and should instead look like this:
def Exit(self):
choice = QtWidgets.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes:
self.close()
If you want to avoid having to type self.ui all the time, you can simplify things a bit by using the multi-inheritance approach:
class MainW(QtWidgets.QMainWindow, Ui_Main):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.setupUi(self)
# self.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.setWindowTitle("Meas Sound Measurement Tool")
self.progressBar.setProperty("value", 1)
self.actionExit.triggered.connect(self.Exit)
Now all the widgets added in Qt Designer are direct attributes of the MainW object.

Resources