pyqt how to connect stop button

2019-08-16 10:13发布

问题:

I tried to design a pyqt gui in which when I press start it initiates the function fun1 and that starts selenium script in funbrowser. And what I intended that when I press "stop", the function funbrowser stops running regardless of where is the control in funbrowser. and also pyqt window should be closed and program has to be stopped.But it is still printing "HI" I am using spyder windows 7 python 3.6 So what should I connect to button stop so that funbrowser stops.

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def funbrowser(self,browser,details):
        browser.get("https://fileml.com/l/0w5O")
        time.sleep(1)
        browser.switch_to.frame("offer-iframe")
        elemlist=browser.find_elements_by_xpath("//ul/li/a")
        list_of_names=[] #All those present
        for i in range(len(elemlist)):
            list_of_names.append(str(elemlist[i].text))
        print("\n")
        print(list_of_names)
        lon=len(list_of_names)*[0]
        available=[] #All those linked
        for i in range(len(elemlist)):
            if "Register for Free" in elemlist[i].text:
                lon[i]="register"
                available.append("register")
            if "Health Samples" in elemlist[i].text:
                lon[i]="health"
                available.append("health")
        print("Available",available)
        print("lon",lon)
        selected=random.choice(available)
        print("selected is ",selected)

        clickon=lon.index(selected)
        print(clickon)
        elemlist[clickon].click()
        alert = browser.switch_to.alert
        alert.accept()    

    def fun1(self):
        browser=webdriver.Firefox()
        self.funbrowser(browser,details)

    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("stop",self)
        self.btn2.clicked.connect()
        self.btn3=QtWidgets.QPushButton("quit",self)
        self.btn3.clicked.connect(self.close)
        self.layout.addWidget(self.btn1,0,0)
        self.layout.addWidget(self.btn2,0,1)
        self.layout.addWidget(self.btn3,0,2)
        self.setLayout(self.layout)
        self.show()

What function should I have to connec to self.btn2("stop")