简体   繁体   中英

How to start multiple instances of function via pyqt5 button?

Hey i wrote an little AutoBuy Bot. The GUI is made in PYQT5.

I have an button that calls an function with a few parameters. The problem im running into is, that

  1. the Main Window freezes
  2. with problem nr1 i cant click the button multiple times to trigger the function multiple times.(I want to start it multiple times)

I really need some help with that.

my button:

        self.buttonSoleBox = QPushButton('Start Bot', self)
        self.buttonBox.move(20, 120)
        self.buttonBox.clicked.connect(self.on_click)

and My Button action function:

    def on_click(self):

        email = self.textbox.text()
        password = self.textbox1.text()
        aid = self.textbox2.text()
        payment = self.textbox4.text()
        paypalemail = self.textbox5.text()
        paypalpassword = self.textbox6.text()
        StartBotFunction(email, password, aid, payment, paypalemail, paypalpassword)

        self.textbox.setText("")
        self.textbox1.setText("")
        self.textbox2.setText("")
        self.textbox4.setText("")
        self.textbox5.setText("")
        self.textbox6.setText("")

for testing: 2files:

main.py

from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QLineEdit, QLabel, QComboBox
from PyQt5 import QtGui
from selenium import webdriver
import sys

class App(QMainWindow):

    def __init__(self):
        super(App,self).__init__()
        self.title = 'whatever'
        self.left = 200
        self.top = 200
        self.width = 800
        self.height = 500
        self.setWindowIcon(QtGui.QIcon('icon.png'))
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.initUI()

    def initUI(self):

        self.button1 = QPushButton('Start Bot', self)
        self.button1.move(20, 120)
        self.button1.clicked.connect(self.on_click)

        self.show()


    def on_click(self):
        word = "sneaker"
        StartBotFunction(word)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())



search.py

from selenium import webdriver
import time

def StartBotFunction(word):
    word
    driver = webdriver.Chrome()
    driver.get('https://www.zalando.de/herren/?q=' + word )

    first = driver.find_element_by_xpath('//*[@id="z-nvg-cognac-root"]/div[1]/z-grid/z-grid-item[2]/div/div[5]/z-grid/z-grid-item[1]/div/a')
    first.click()


    while (driver.page_source).__contains__('Bewertung'):
        time.sleep(5)
        driver.refresh()

while building that example for you, i found out that i run into my main problem when i do that if i remove the while thing, i can create as many chromewindows as i click the button

Any idea how to fix that?

while (driver.page_source).__contains__('Bewertung'):
time.sleep(5)
driver.refresh()```

You have to run StartBotFunction in another thread:

import sys
import threading

from PyQt5 import QtCore, QtWidgets

import search


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.lineedit = QtWidgets.QLineEdit("sneaker", placeholderText="word")
        button = QtWidgets.QPushButton("Press me")
        button.clicked.connect(self.on_click)

        flay = QtWidgets.QFormLayout(self)
        flay.addRow("Insert word:", self.lineedit)
        flay.addRow(button)

    @QtCore.pyqtSlot()
    def on_click(self):
        word = self.lineedit.text()
        


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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