python - 如何通过pyqt5按钮启动多个函数实例?

标签 python selenium-webdriver pyqt pyqt5 selenium-chromedriver

嘿,我写了一个小自动购买机器人。 GUI是用PYQT5制作的。

我有一个按钮,可以调用带有几个参数的函数。 我遇到的问题是,

  1. 主窗口卡住
  2. 对于问题 nr1,我无法多次单击按钮来多次触发该功能。(我想多次启动它)

我真的需要一些帮助。

我的按钮:

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

和我的按钮操作功能:

    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("")

用于测试: 2 个文件:

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_())



搜索.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 的东西,我可以在单击按钮时创建尽可能多的 chromewindows

知道如何解决这个问题吗?

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

最佳答案

您必须在另一个线程中运行 StartBotFunction:

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()
        <b>threading.Thread(target=search.StartBotFunction, args=(word,)).start()</b>


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

关于python - 如何通过pyqt5按钮启动多个函数实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58945879/

相关文章:

Python在内存中运行EXE

java - Selenium 2 Webdriver 与 JUnit 或 TestNG for PageObject 设计模式

java - Webdriver : Unable to enter value inside nested iframe, 框架集和框架

python-3.x - 如何将 VLC 实例插入 QFrame

python - 为什么我的线程中的 python 套接字并不总是正确关闭(即,如果我连续多次运行该程序)

python - 如何在 Pony ORM 中使用 Oracle 模式名称?

python - 使用 Selenium 缓慢向下滚动页面

python - 可以在白色文本上添加黑色边框吗?

python - QGridLayout,展开widget就行

Python MySql Connector 准备语句和字典类