0

Here is my code for the click button:

run_btn=QtWidgets.QPushButton("Run")
def main():
    print ('Starting Program')
run_btn.clicked.connect(main)

But after I click "Run", it just prints "Starting Program" again and again, and the GUI window doesn't disappear:

screenshot

How can I make the button print it once and go on with the program ?

I am using PyQt5 and Python 3.4.0

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Itay Katsnelson
  • 69
  • 1
  • 2
  • 8

1 Answers1

1

Suppose that the QPushButton is inside the main widget (in the example QWidget), to close the window we use the close()

from PyQt5 import QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()

line1_edit = QtWidgets.QLineEdit()
line2_edit = QtWidgets.QLineEdit()

run_btn=QtWidgets.QPushButton("Run")
def main():
    print ('Starting Program')
    w.close()

run_btn.clicked.connect(main)

layout = QtWidgets.QVBoxLayout()
layout.addWidget(line1_edit)
layout.addWidget(line2_edit)
layout.addWidget(run_btn)
w.setLayout(layout)
w.show()
sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Yo man what does the sys.ext(app.exec()) do ? and thanks for the help :) – Itay Katsnelson Mar 09 '17 at 12:03
  • It sets the exit code of your Python script to the exit code of the Qt app. So if something bad happens, you get a nonzero exit code. (In Bash, you can run `echo $?` to print the exit code of the previous command.) – DDR Aug 29 '18 at 22:38
  • When running from Python IDE such as Spyder, the Python console is still operating even after the app closed. Is there any way to force app.quit() to be called after the button is pushed ? – Adrien Mau Jun 20 '23 at 12:08