0

I'm developing a simple application using PyQt. Basically, when a I press the button "Go Back to Screen 1" on screen 2 my application goes to screen 1. However, it executes the method of screen 2 two times.

The best way to understand the problem is:

  1. When the application starts click on button Screen 2
  2. Click the button Go Back To Screen 1
  3. Press ok
  4. Click on the button Screen 2 again
  5. Click the button Go Back To Screen 1
  6. Press ok

Doing this process, the pop will show 2 times on screen 1 (the method test is called 3 times instead 2)

Why is this happening since I clicked only two times on the Button Screen 2? I've debugged the code and didn't find a way to fix this. The MVCE:

main.py:

import sys
from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5.QtWidgets import QMainWindow

from Ui_MainWindow import Ui_MainWindow

class MainWindow:
    def __init__(self):
        self.main_win = QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.main_win)

        self.ui.stackedWidget.setCurrentWidget(self.ui.page)

        self.ui.pushButton.clicked.connect(self.showScreen1)
        self.ui.pushButton_2.clicked.connect(self.showScreen2)

    def show(self):
        self.main_win.show()

    def test(self):
        print("Just a test")
        self.showPopUp()
        self.ui.stackedWidget.setCurrentWidget(self.ui.page)

    def showPopUp(self):
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Information)
        msg.setText("A window")
        msg.setInformativeText("A window")
        msg.setWindowTitle("AVISO")
        msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        retval = msg.exec_()


    def showScreen1(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.page)

    def showScreen2(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.page_2)
        self.ui.pushButton_3.clicked.connect(self.test)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_win = MainWindow()
    main_win.show()
    sys.exit(app.exec_())

Ui_MainWindow.py:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(485, 239)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
        self.stackedWidget.setGeometry(QtCore.QRect(20, 20, 451, 171))
        self.stackedWidget.setObjectName("stackedWidget")
        self.page = QtWidgets.QWidget()
        self.page.setObjectName("page")
        self.label = QtWidgets.QLabel(self.page)
        self.label.setGeometry(QtCore.QRect(180, 80, 91, 16))
        font = QtGui.QFont()
        font.setPointSize(15)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.stackedWidget.addWidget(self.page)
        self.page_2 = QtWidgets.QWidget()
        self.page_2.setObjectName("page_2")
        self.label_2 = QtWidgets.QLabel(self.page_2)
        self.label_2.setGeometry(QtCore.QRect(180, 30, 91, 16))
        font = QtGui.QFont()
        font.setPointSize(15)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.page_2)
        self.pushButton_3.setGeometry(QtCore.QRect(160, 130, 121, 23))
        self.pushButton_3.setObjectName("pushButton_3")
        self.stackedWidget.addWidget(self.page_2)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 200, 221, 31))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(250, 200, 221, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        self.stackedWidget.setCurrentIndex(1)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "SCREEN 1"))
        self.label_2.setText(_translate("MainWindow", "SCREEN 2"))
        self.pushButton_3.setText(_translate("MainWindow", "Go back to Screen 1"))
        self.pushButton.setText(_translate("MainWindow", "SCREEN 1"))
        self.pushButton_2.setText(_translate("MainWindow", "SCREEN 2"))
  • move `self.ui.pushButton_3.clicked.connect(self.test)` after `self.ui.pushButton_2.clicked.connect(self.showScreen2)` – eyllanesc May 22 '21 at 18:51
  • @eyllanesc all signals must go on `__init__`? – Iago May 22 '21 at 18:57
  • All the signals must be done once, in your case more connections are being added each time the showScreen2 function is invoked. The signals do not identify if they have already connected to the same function. – eyllanesc May 22 '21 at 19:01

0 Answers0