嘿,我写了一个小的AutoBuy Bot。GUI是在PYQT5中制作的。
我有一个按钮,它调用一个带有几个参数的函数。我遇到的问题是,
- 主窗口冻结
- 对于问题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个文件:
主.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的东西,我可以创建与我点击按钮一样多的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()
threading.Thread(target=search.StartBotFunction, args=(word,)).start()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())