Problems with multiple windows in Pyqt5

Asked

Viewed 66 times

0

Hi, I’m having trouble with multiple windows on Pyqt5. I created a graphical interface using Qt Designer, which consists of two Frames, in order to generate two windows for the user. The initial window (frame_1) contains only one button that only serves to open window 2. Window 2 (frame_2) contains a lineEdit, where the user type any name. In this same window, there is a button that, when clicked, reads the name typed in the lineEdit and assigns it to a name variable. Then the typed name is printed on the screen. There is also a button that serves to close window 2 and return to the initial window.

Follows the Python code and the interface created, showing the two windows:

from PyQt5 import uic, QtWidgets
import sys

def janela2():
    meuApp.frame_1.close()
    meuApp.frame_2.show()
    meuApp.pushButton_2.clicked.connect(adiciona_nome)
    meuApp.pushButton_3.clicked.connect(fecha_janela2)
    
def adiciona_nome():
    nome = meuApp.lineEdit.text()
    print(nome)
    
def fecha_janela2():
    meuApp.lineEdit.setText('')
    meuApp.frame_2.close()
    meuApp.frame_1.show()
    
app = QtWidgets.QApplication(sys.argv)
meuApp = uic.loadUi('meuApp.ui')
meuApp.show()
meuApp.frame_2.close()
meuApp.pushButton.clicked.connect(janela2)
app.exec_()

interface criada, mostrando as duas frames

I performed the following tests:

  1. I opened window 2, typed Name1, clicked on Add button, closed window;
  2. I opened window 2, typed Name2, clicked on Add button, closed window;
  3. I opened window 2, typed Name3, clicked on Add button, closed window.

Upshot:

Nome1
Nome2
Nome2
Nome3
Nome3
Nome3

The second time the window2 is used, Name2 is printed 2 times, the third time, Name3 is printed 3 times. What’s going on there? Although window 2 is closed, the process is not interrupted, so the second time window 2 opens, a second process starts. How to close window 2 while interrupting the process?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.