0

This is a simplified version of a bigger program.

I made a GUI with the help of Qt Designer. Here is it:

PyQt5 GUI

The main window has two Frames, frame_1 and frame_2. First screen (frame_1) is the main window and contains just a pushbutton to open the second screen. The second screeen (frame_2) contains a lineEdit and two buttons. Here is the Python code to use the GUI:

from PyQt5 import uic, QtWidgets
import sys, os

def screen2():
    myApp.frame_1.close()
    myApp.frame_2.show()
    myApp.pushButton_2.clicked.connect(add_name)
    myApp.pushButton_3.clicked.connect(close_screen2)
    
def add_name():
    name = myApp.lineEdit.text()
    print(name)
    
def close_screen2():
    myApp.lineEdit.setText('')
    myApp.frame_2.close()
    myApp.frame_1.show()
    
app = QtWidgets.QApplication.instance()
if app is None:
    app = QtWidgets.QApplication(sys.argv)
myApp = uic.loadUi('myApp.ui')
myApp.show()
myApp.frame_2.close()
myApp.pushButton.clicked.connect(screen2)
app.exec_()

The program start with the main screen. The user click the button to open the second screen. Then he enter a name in the lineEdit and then click on Ok. The name will be assigned to a variable name and then printed. For the last, the user click on Close to return to the main sreen.

I made a test by open the second screen, entering a name and closing the screen, by three times. Here is the results:

Name1
Name2
Name2
Name3
Name3
Name3

One can see that in the first run the name entered is printed once. In the second run it's printed twice. In third run it's printed three times. It seems that something don't stopped after the first run so that in the second run two things happen at the same time and in the third run three things happen at the same time. what is going on? Someone could explain me?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Dexter
  • 1
  • 1
  • Signal connections are not unique (unless explicitly required by using the [`Qt.UniqueConnection`](https://doc.qt.io/qt-5/qt.html#ConnectionType-enum) argument), so you should not connect everytime `screen2` is called, otherwise the result is that every time you connect the same functions again and again. Move those `clicked' along with the last one. See [Slot is being called multiple times every time a signal is emitted](https://stackoverflow.com/questions/10975247/slot-is-being-called-multiple-times-every-time-a-signal-is-emitted) which is marked duplicate to this question. – musicamante Apr 13 '21 at 18:32
  • Thanks for the answer, now I understood. I used the disconnect() method and it worked. – Dexter Apr 14 '21 at 09:49

0 Answers0