python程序不会停止

时间:2017-09-10 04:08:02

标签: python pyqt

我试图设计一个pyqt gui,当我按下start时,启动函数fun1并在每3秒后开始打印“HI”。当我按退出时,我打算关闭gui应该关闭并且程序必须停止。但它仍然打印“HI”我正在使用spyder windows 7 python 3.6

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def fun1(self):
        import time
        for i in range(10000):
            print("HI")
            time.sleep(3)
    def fun2(self):
        import sys
        self.hide()
        quit()
        print("..")

    def launch_thread(self):
        import threading
        x=threading.Thread(target=self.fun1)
        x.start()

    def initUI(self):

        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('web.png'))        
        self.layout=QtWidgets.QGridLayout()
        self.btn1=QtWidgets.QPushButton("start",self)
        self.btn1.clicked.connect(self.launch_thread)
        self.btn2=QtWidgets.QPushButton("quit",self)
        self.btn2.clicked.connect(self.fun2)
        self.layout.addWidget(self.btn1,0,0)
        self.layout.addWidget(self.btn2,0,1)
        self.setLayout(self.layout)
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

试试这个:

public static final