我正在尝试使用PyQt5创建一个简单的GUI。我正在从Spyder(Anaconda最新版本,python 3.7)在Windows 10中运行代码。这是我的代码:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button test'
self.left = 50
self.top = 50
self.width = 720
self.height = 800
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100,70)
button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
print('PyQt5 button click')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
app.exec_()
弹出一个窗口。现在,如果我通过单击关闭按钮(GUI的右上角)来关闭GUI,Spyder IP控制台将无法恢复正常状态。它冻结了。我应该在代码中使用什么来解决该问题?
谢谢, 莫妮
答案 0 :(得分:1)
关闭窗口似乎没有关闭QApplication对象。为此,请在on_Click定义之后添加这两行,以覆盖主窗口的closeEvent函数
def closeEvent(self,event):
QApplication.quit()
这将关闭窗口和QApplication对象,并且控件应返回到控制台
答案 1 :(得分:0)
您可以尝试查看以下线程中的答案: Can't Kill PyQT Window after closing it. Which requires me to restart the kernal
似乎与您的问题和解决方案相似。