Skip to content Skip to sidebar Skip to footer

In Pyqt, Button Click Connect Not Working In The New Window

I have two windows in my code first I want to open class first() window which contains btn1. When I click btn1, I want to open a new window and replace it with my previous window (

Solution 1:

Try it:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *

class Ui_MainWindow1(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(264, 248)
        MainWindow.setMinimumSize(QtCore.QSize(264, 248))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.submitBtn = QtWidgets.QPushButton(self.centralwidget)
        self.submitBtn.setObjectName("submitBtn")
        self.gridLayout.addWidget(self.submitBtn, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "first MainWindow"))    
        self.submitBtn.setText(_translate("MainWindow", "Go second"))

class Ui_MainWindow2(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(264, 248)
        MainWindow.setMinimumSize(QtCore.QSize(264, 248))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "second MainWindow"))    
        self.pushButton.setText(_translate("MainWindow", "hi"))

class first(QMainWindow, Ui_MainWindow1):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.submitBtn.clicked.connect(self.loginFunc)

    def loginFunc(self):
        self.displayUi = second() 
        self.hide()
        self.displayUi.show()

class second(QMainWindow, Ui_MainWindow2):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.dispTable)

    def dispTable(self):
        print("hi")

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    MainWindow = first() 
    MainWindow.show()
    sys.exit(app.exec_())

enter image description here

Post a Comment for "In Pyqt, Button Click Connect Not Working In The New Window"