简体   繁体   English

PyQt5:如何使单击后的按钮关闭GUI

[英]PyQt5 : How to make a button close the gui after clicking

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: 但是,当我单击“运行”后,它会一次又一次地打印“启动程序”,并且GUI窗口不会消失:

截图

How can I make the button print it once and go on with the program ? 如何使按钮打印一次并继续执行程序?

I am using PyQt5 and Python 3.4.0 我正在使用PyQt5和Python 3.4.0

Suppose that the QPushButton is inside the main widget (in the example QWidget ), to close the window we use the close() 假设QPushButton在主窗口小部件内(在示例QWidget ),要关闭窗口,我们使用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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM