简体   繁体   English

执行关闭窗口命令后,Pyqt终端挂起

[英]Pyqt Terminal hangs after excuting close window command

I have read lots of threads online, but still I could not find the solution. 我已经在线阅读了很多线程,但是仍然找不到解决方案。 My question should be very simple: how to close a Pyqt window WITHOUT clicking a button or using a timer. 我的问题应该非常简单:如何在不单击按钮或不使用计时器的情况下关闭Pyqt窗口。 The code I tried is pasted below 我尝试过的代码粘贴在下面

from PyQt4 import QtGui, QtCore
import numpy as np
import progressMeter_simple
import sys
import time
import pdb
class ProgressMeter(progressMeter_simple.Ui_Dialog, QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        progressMeter_simple.Ui_Dialog.__init__(self)
        self.setupUi(self)
        self.progressBar.setRange(0, 0)
        QtGui.QApplication.processEvents()
    def termination(self):
        time.sleep(5)
        self.close()
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    Dialog = ProgressMeter()
    Dialog.show()
    Dialog.termination()
    sys.exit(app.exec_())

My Pyqt GUI is designed using Qt designer, and it is nothing but a progress bar that keeps moving from left to right (busy indication). 我的Pyqt GUI是使用Qt设计器设计的,它不过是一个不断从左向右移动(忙碌指示)的进度条。

However, when I run the code above, the terminal still hangs after the Pyqt window is closed. 但是,当我运行上面的代码时,关闭Pyqt窗口后,终端仍然挂起。 Ctrl+C also couldn't kill the process. Ctrl + C也无法终止该进程。 In short, how can I properly close/terminate a Pyqt window without clicking a button or using a timer? 简而言之,如何在不单击按钮或使用计时器的情况下正确关闭/终止Pyqt窗口?

It's not working because you're calling GUI methods on the dialog ( close() ) outside of the event loop. 它不起作用,因为您是在事件循环外部的对话框( close() )上调用GUI方法。 The event loop doesn't start until you call app.exec_() . 直到您调用app.exec_() ,事件循环才开始。

If you really want to close the dialog immediately after it opens without using a QTimer , you can override the showEvent() method and call termination() from there, which gets called when the dialog is first displayed. 如果确实要在对话框打开后立即关闭而不使用QTimer ,则可以覆盖showEvent()方法并从那里调用termination() ,该方法在首次显示对话框时被调用。

class ProgressMeter(progressMeter_simple.Ui_Dialog, QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        progressMeter_simple.Ui_Dialog.__init__(self)
        self.setupUi(self)
        self.progressBar.setRange(0, 0)

    def showEvent(self, event):
        super(ProgressMeter, self).showEvent(event)
        self.termination()

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

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