Could you please explain why the quit push button does not work properly in this case:
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
qbtn = QPushButton('Quit', self)
qbtn.clicked.connect(QCoreApplication.instance().quit)
qbtn.move(50, 50)
self.setGeometry(300, 300, 250, 150)
self.show()
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
But works if I include the last three lines in a function:
def fun():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
fun()
I don't understand the mechanics :(
I ran into the same problem while using linux. This code comes from a tutorial on zetcode.com for PyQt5 in the quitButton.py file. Here is the solution that I found works:
Instead of using:
qbtn.clicked.connect(QCoreApplication.instance().quit)
Replace it with this:
qbtn.clicked.connect(self.close)
After this, you shouldn't need to call it in a separate function. Hope this helps!
I tried this recently. I need a sure exit from within spyder and this always seems a problem with pyqt. I found qbtn.clicked.connect(QCoreApplication.instance().quit) will quit and leave window open. qbtn.clicked.connect(self.close) will close the window but leave the process running. qbtn.clicked.connect(QCoreApplication.instance().quit) qbtn.clicked.connect(self.close) sequentially will quit and close window. This is pyqt5 on Python 3.8.8. Hope this is helpful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.