python输入q停止循环_如何在python中终止qthread

本文介绍如何在Python的QThread中通过用户输入'q'来终止循环。示例代码展示了一个使用PySide库创建的对话框,包含开始和停止按钮,当点击停止按钮时,调用QThread的terminate方法结束线程中的循环。
摘要由CSDN通过智能技术生成

关键是使主循环在“run”方法中,因为“terminate”函数是在“run”中停止循环,而不是线程本身

这是一个有效的例子,但不幸的是它只在windows上运行import sys

import time

from PySide.QtGui import *

from PySide.QtCore import *

class frmMain(QDialog):

def __init__(self):

QDialog.__init__(self)

self.btStart = QPushButton('Start')

self.btStop = QPushButton('Stop')

self.counter = QSpinBox()

self.layout = QVBoxLayout()

self.layout.addWidget(self.btStart)

self.layout.addWidget(self.btStop)

self.layout.addWidget(self.counter)

self.setLayout(self.layout)

self.btStart.clicked.connect(self.start_thread)

self.btStop.clicked.connect(self.stop_thread)

def stop_thread(self):

self.th.stop()

def loopfunction(self, x):

self.counter.setValue(x)

def start_thread(self):

self.th =

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值