当我通过PyQT按下按钮时,我想要一个脚本运行。 这是我创建的按钮的一个例子
<Property name="ConnectionName">cn=wso2admin,ou=Users,dc=prc,dc=com</Property>
这是我想要运行的脚本然后按下按钮。这是一个自动测试,可以打开浏览器并执行某些操作。
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
class AutoTestWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn1 = QPushButton("Test № 1 ", self)
btn1.move(30, 50)
btn2 = QPushButton("Test № 2", self)
btn2.move(150, 50)
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
self.statusBar()
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Запуск тестов')
self.show()
def buttonClicked(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' test started')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = AutoTestWindow()
sys.exit(app.exec_())
我应该做些什么来实现它?我按下按钮时试图调用一个函数,但它没有工作
答案 0 :(得分:1)
试一试:
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QStatusBar
class AutoTestWindow(QMainWindow):
def __init__(self):
super().__init__()
self.driver = None # +++
self.query = "Тензор" # +++
self.initUI()
def initUI(self):
btn1 = QPushButton("Test № 1 ", self)
btn1.move(30, 50)
btn2 = QPushButton("Test № 2", self)
btn2.move(150, 50)
#btn1.clicked.connect(self.buttonClicked) # ---
#btn2.clicked.connect(self.buttonClicked) # ---
btn1.clicked.connect(self.init_driver) # +++
btn2.clicked.connect(lambda checked : self.findelem(self.driver, self.query)) # +++
self.statusBar().showMessage('Запуск тестов', 5000)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Запуск тестов')
self.show()
# ---
#def buttonClicked(self):
# sender = self.sender()
# self.statusBar().showMessage(sender.text() + ' test started')
# +++
def init_driver(self):
self.statusBar().showMessage('Test № 1 test started')
#driver = webdriver.Chrome("/Users/alexeynikitin/Desktop/chromedriver")
self.driver = webdriver.Chrome()
self.driver.wait = WebDriverWait(self.driver, 10)
#return self.driver
# +++
def findelem(self, driver, query):
print("\ndriver=`{}`, \nquery=`{}`\n".format(driver, query))
self.statusBar().showMessage('Test № 2 test started')
if driver is not None:
driver.get("https://www.yandex.ru/")
try:
box = driver.wait.until(ec.presence_of_element_located(
(By.ID, "text")))
button = driver.wait.until(ec.visibility_of_element_located(
(By.CLASS_NAME, "search2__button")))
box.send_keys(query)
suggestion_box = driver.wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "body > div.i-bem.popup.suggest2.suggest2_theme_flat.suggest2_size_m.suggest2_adaptive_yes.suggest2_type_advanced.suggest2_ahead_yes.popup_adaptive_yes.popup_animate_no.popup_autoclosable_yes.popup_theme_ffffff.suggest2-detect_js_inited.suggest2_js_inited.popup_js_inited.popup_to_bottom.popup_visibility_visible")))
try:
button.click()
except ElementNotVisibleException:
button = driver.wait.until(ec.visibility_of_element_located(
(By.CLASS_NAME, "search2__button")))
except TimeoutException:
print("ничего не нашел на https://www.yandex.ru/")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = AutoTestWindow()
sys.exit(app.exec_())
答案 1 :(得分:0)
如果我正确理解了您的问题,那么在初始化课程时(或在您的initUI
方法中)将clickbutton事件连接到正确的函数就足够了:
def __init__(self):
super().__init__()
self.initUI()
btn1.clicked.connect(init_driver)
btn2.clicked.connect(lambda checked :findelem(value1, value2))
我更正了第二个绑定为@eyllanesc建议
答案 2 :(得分:0)
调用python脚本的一种简单方法是使用os
假设您的脚本与应用程序位于同一文件夹中,则需要添加import os
,然后在函数os.system('yourscriptname.py')
中调用buttonClicked
。