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.
Related
I'm trying to create a small application in python3.8 and PyQt5 and i have a problem with setting size of the tableWidget .
Here is the code :
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(200, 200, 400, 400)
self.setWindowTitle('OIL-CheckOrder')
self.initUI()
def initUI(self):
w = QtWidgets.QWidget()
self.setCentralWidget(w)
grid = QtWidgets.QGridLayout(w)
self.textbox = QLineEdit(self)
self.textbox.setText('File')
self.textbox.setReadOnly(True)
self.textbox2 = QLineEdit(self)
self.textbox2.setText('Order number')
size = QSize(40, 40)
self.b1 = QtWidgets.QPushButton(self)
self.b1.setIcon(QIcon('./search.jpg'))
self.b1.setIconSize(size)
self.b1.clicked.connect(self.File_path)
self.run = QtWidgets.QPushButton(self)
self.run.setIcon(QIcon('./start.jpg'))
self.run.setIconSize(size)
self.run.clicked.connect(lambda path: self.loaddata(self.path))
self.createTable()
grid.addWidget(self.b1, 0, 0, QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
grid.addWidget(self.textbox, 1, 0, 1, 3)
grid.addWidget(self.textbox2, 2, 0, 1, 3)
grid.addWidget(self.run, 0, 2, QtCore.Qt.AlignRight | QtCore.Qt.AlignTop)
grid.addLayout(self.vbox, 3, 0, QtCore.Qt.AlignLeft)
def createTable(self, pdfsn =[]):
self.tableWidget = QTableWidget(self.centralWidget())
self.tableWidget.setColumnCount(3)
headers = ["PDF SN", "APPSHEET SN", "IS MATCH"]
self.tableWidget.setHorizontalHeaderLabels(headers)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.tableWidget)
self.setLayout(self.vbox)
The app looks now like on screen below :
APP_OIL
All i want to resize table as the blue line shows.
Is it possible ?
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())
I don't understand how to put data into QTableWidget row by row. I take data from lineEdit then put it into table, but when I try to add another person with data it rewrites the previous data, but I need to put it in the row below. I guess I should do something with "row"
Here is my code
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
import sqlite3
import sys
conn = sqlite3.connect('todo.db')
cur = conn.cursor()
cur.execute('CREATE TABLE if not exists disp_list(disp_name text, disp_surname text, disp_date text)')
conn.commit()
conn.close()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(960, 550)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(610, 261, 341, 241))
self.listWidget.setObjectName("listWidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(610, 180, 331, 71))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.label.setFont(font)
self.label.setWordWrap(True)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 40, 311, 41))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(340, 40, 311, 41))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(690, 40, 251, 41))
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton = QtWidgets.QPushButton(self.centralwidget, clicked=lambda: self.add_disp())
self.pushButton.setGeometry(QtCore.QRect(30, 100, 221, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(270, 100, 221, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(720, 100, 221, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 200, 591, 301))
self.tableWidget.setRowCount(130)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(3)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(18)
item.setFont(font)
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
self.tableWidget.horizontalHeader().setDefaultSectionSize(180)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 10, 291, 16))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(350, 10, 291, 16))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(690, 10, 251, 20))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(510, 100, 191, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 960, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def add_disp(self):
name = self.lineEdit.text()
surname = self.lineEdit_2.text()
date = self.lineEdit_3.text()
disps=[{"disp_name": name, "disp_surname": surname, "disp_date": date}]
for person in disps:
row = 1
self.tableWidget.setItem(row, 0, QtWidgets.QTableWidgetItem(person["disp_name"]))
self.tableWidget.setItem(row, 1, QtWidgets.QTableWidgetItem(person["disp_surname"]))
self.tableWidget.setItem(row, 2, QtWidgets.QTableWidgetItem(person["disp_date"]))
name = self.lineEdit.setText("")
surname = self.lineEdit_2.setText("")
date = self.lineEdit_3.setText("")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Список диспетчеров, у которых заканчивается срок"))
self.pushButton.setText(_translate("MainWindow", "Add ATC"))
self.pushButton_3.setText(_translate("MainWindow", "Delete from table"))
self.pushButton_4.setText(_translate("MainWindow", "Delete from database"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Surname"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Issue_date"))
self.label_2.setText(_translate("MainWindow", "Enter name"))
self.label_3.setText(_translate("MainWindow", "Enter surname"))
self.label_4.setText(_translate("MainWindow", "Enter date"))
self.pushButton_5.setText(_translate("MainWindow", "Save to database"))
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_())
Supposing that you want to insert items incrementally, you should create an instance attribute that will be used to start with the first row (which is 0, not 1, by the way) and then increment it by 1 every time.
This, anyway, should not be done with a file created by pyuic, because those files are meant to be left unmodified. Editing them is highly discouraged and considered bad practice for a lot of reasons. Read more about how to properly use those files when using Designer.
So, rebuild again your .ui file (in the next example I'm supposing you've generated a file named ui_mainwindow.py), and then create your actual script similar to the following:
from PyQt5 import QtCore, QtGui, QtWidgets
from ui_mainwindow import Ui_MainWindow
import sqlite3
import sys
conn = sqlite3.connect('todo.db')
cur = conn.cursor()
cur.execute('CREATE TABLE if not exists disp_list(disp_name text, disp_surname text, disp_date text)')
conn.commit()
conn.close()
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.currentRow = -1
self.pushButton.clicked.connect(self.add_disp)
def add_disp(self):
name = self.lineEdit.text()
surname = self.lineEdit_2.text()
date = self.lineEdit_3.text()
self.currentRow += 1
if self.currentRow > self.tableWidget.rowCount() - 1:
self.tableWidget.insertRow(self.tableWidget.rowCount())
self.tableWidget.setItem(self.currentRow, 0,
QtWidgets.QTableWidgetItem(name))
self.tableWidget.setItem(self.currentRow, 1,
QtWidgets.QTableWidgetItem(surname))
self.tableWidget.setItem(self.currentRow, 2,
QtWidgets.QTableWidgetItem(date))
self.lineEdit.clear()
self.lineEdit_2.clear()
self.lineEdit_3.clear()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
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.
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()