python退出线程_pyqt:如何正确退出线程

我编写了一个pyqt gui并使用线程来运行需要很长时间才能执行的代码,但是我希望能够选择安全地停止执行。我不想用get_线程终止()方法。我想用一个特殊的函数来停止代码(也许del())。我的问题是,我在自己的类中编写代码,只想在不改变语法的情况下中止类。在

编辑:有人提到必须向类传递一个标志,这个标志必须经常检查。我怎样把这面旗子送到班上?因为当按下停止按钮时,标志必须改变值。在

编辑2:到目前为止,我的解决方案是,对一个名为running-global的全局变量进行去公差。我变了self.get_线程.terminate()到running_global=False,如果变量被设置为False,我会不断地检查长时间运行的\u prog。我觉得这个解决方案很难看,所以如果有人有更好的主意,我会很高兴的。在

这是我启动线程的对话框代码:class SomeDialog(QtGui.QDialog,

userinterface_status.Ui_status_window):

finished = QtCore.pyqtSignal(bool)

def __init__(self):

"""

:param raster: Coordinates which are going to be scanned.

"""

super(self.__class__, self).__init__() # old version, used in python 2.

self.setupUi(self) # It sets up layout and widgets that are defined

self.get_thread = SomeThread()

# Conencting the buttons

self.start_button.clicked.connect(self.start)

self.stop_button.clicked.connect(self.stop)

self.close_button.clicked.connect(self.return_main)

# Connecting other signals

self.connect(self.get_thread, QtCore.SIGNAL("stop()"), self.stop)

self.connect(self.get_thread, QtCore.SIGNAL("update_status_bar()"), self.update_status_bar)

def return_main(self):

"""

Function is excecuted, when close button is clicked.

"""

print("return main")

self.get_thread.terminate()

self.close()

def start(self):

"""

Starts the thread, which means that the run method of the thread is started.

"""

self.start_button.setEnabled(False)

self.get_thread.start()

def stop(self):

print("Stop programm.")

self.start_button.setEnabled(True)

self.get_thread.quit()

def end(self):

QtGui.QMessageBox.information(self, "Done!", "Programm finished")

def closeEvent(self, event):

"""

This method is called, when the window is closed and will send a signal to the main window to activaete the

window again.

:param event:

"""

self.finished.emit(True)

# close window

event.accept()

下面的类是线程的代码:

^{pr2}$

因此,如果你按下停止按钮,程序应该立即关闭,以一种节省的方式。有没有办法用保存的方式中止类?例如,我是否可以将一个变量传递给long_running_prog类,当您按下停止按钮时,该类将变为True?如果有这种可能,有人能告诉我怎么做吗?在

提前谢谢你的帮助

我希望你能理解我的问题。

问候语

希兹

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值