如何将现有变量传递到调用的 pyqt5 ui 文件中?
How do I pass a existing variable into a pyqt5 ui file that is called?
我正在为我的界面使用 PyQt5。我想从我的主程序导入link变量到GUI程序,这样它就可以显示link 从我的主程序中获取。
这就是我希望它的工作方式:
Link 被获取 --> Link 被放入一个变量 --> Link 被传递到 GUI 文件中 - -> GUI 在 GUI.
中显示图像 link
我的代码的简化版本:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.close()
driver = webdriver.Chrome(executable_path=r"C:\Users\amete\Documents\chromedriver.exe")
driver.get("https://myanimelist.net/search/all?q=one%20piece&cat=all")
search = driver.find_element_by_xpath('//input[@name="q"]')
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@name="q"]')))
# Clears the field
search.send_keys(Keys.CONTROL, 'a')
search.send_keys(Keys.DELETE)
# The field is now cleared and the program can type whatever it wants
search.send_keys(Anime)
search.send_keys(Keys.RETURN)
# Accept the cookies
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="qc-cmp2-ui"]/div[2]/div/button[3]'))).click()
# Added this wait
wait.until(EC.element_to_be_clickable((By.XPATH,'//h2[@id="anime"]//ancestor::div[@class="content-left"]//article[1]/div[contains(@class, "list")][1]/div[contains(@class, "information")]/a[1]')))
wait.until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[2]/div[2]/div[3]/div[2]/div[2]/div[1]/div/article[1]/div[1]/div[2]/a[1]'))).click()
#Grabs the link of the picture
piclink = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[3]/div[2]/table/tbody/tr/td[1]/div/div[1]/a/img').get_attribute('src')
print (piclink)
.UI 代码
import sys
import requests
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QImage, QPixmap
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(537, 402)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 0, 221, 291))
Image = QImage()
Image.loadFromData(requests.get(piclink).content)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap(Image))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(240, 190, 131, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(390, 190, 131, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(240, 10, 281, 61))
font = QtGui.QFont()
font.setPointSize(20)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(290, 90, 161, 41))
font = QtGui.QFont()
font.setPointSize(22)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 537, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Yes"))
self.pushButton_2.setText(_translate("MainWindow", "No"))
self.label_2.setText(_translate("MainWindow", "Is this the right anime?"))
self.label_3.setText(_translate("MainWindow",str(Titles)))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我需要将“URlink”变量传递到用户界面文件
问题是你没有模块化你的项目,逻辑是创建函数或 classes 来实现必要的逻辑并且是独立的,然后加入它们以便函数和对象交互.另一方面,不要修改 pyuic 生成的代码,而是创建另一个 class 继承自合适的 QWidget 并填充它们。
在这种情况下,抓取和下载任务会消耗大量时间,必须在另一个线程中执行,并将信息发送到主线程。
综合以上,解决方案是:
import threading
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
from gui import Ui_MainWindow
def find_link(anime):
# ...
piclink = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[3]/div[2]/table/tbody/tr/td[1]/div/div[1]/a/img').get_attribute('src')
return piclink
def download_image(link):
image = QImage()
image.loadFromData(requests.get(link).content)
return image
class AnimeManager(QObject):
finished = pyqtSignal(QImage)
def start(self, anime):
threading.Thread(target=self._do_work, args=(anime,), daemon=True).start()
def _do_work(self, anime):
link = find_link(anime)
image = download_image(link)
self.finished.emit(image)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.anime_manager = AnimeManager()
self.anime_manager.finished.connect(self.handle_finished)
self.anime_manager.start("Pokemon")
def handle_finished(self, image):
pixmap = QPixmap.fromImage(image)
self.ui.label.setPixmap(pixmap)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
注:恢复Ui_MainWindowclass.
我正在为我的界面使用 PyQt5。我想从我的主程序导入link变量到GUI程序,这样它就可以显示link 从我的主程序中获取。
这就是我希望它的工作方式:
Link 被获取 --> Link 被放入一个变量 --> Link 被传递到 GUI 文件中 - -> GUI 在 GUI.
中显示图像 link我的代码的简化版本:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.close()
driver = webdriver.Chrome(executable_path=r"C:\Users\amete\Documents\chromedriver.exe")
driver.get("https://myanimelist.net/search/all?q=one%20piece&cat=all")
search = driver.find_element_by_xpath('//input[@name="q"]')
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@name="q"]')))
# Clears the field
search.send_keys(Keys.CONTROL, 'a')
search.send_keys(Keys.DELETE)
# The field is now cleared and the program can type whatever it wants
search.send_keys(Anime)
search.send_keys(Keys.RETURN)
# Accept the cookies
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="qc-cmp2-ui"]/div[2]/div/button[3]'))).click()
# Added this wait
wait.until(EC.element_to_be_clickable((By.XPATH,'//h2[@id="anime"]//ancestor::div[@class="content-left"]//article[1]/div[contains(@class, "list")][1]/div[contains(@class, "information")]/a[1]')))
wait.until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[2]/div[2]/div[3]/div[2]/div[2]/div[1]/div/article[1]/div[1]/div[2]/a[1]'))).click()
#Grabs the link of the picture
piclink = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[3]/div[2]/table/tbody/tr/td[1]/div/div[1]/a/img').get_attribute('src')
print (piclink)
.UI 代码
import sys
import requests
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QImage, QPixmap
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(537, 402)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 0, 221, 291))
Image = QImage()
Image.loadFromData(requests.get(piclink).content)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap(Image))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(240, 190, 131, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(390, 190, 131, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(240, 10, 281, 61))
font = QtGui.QFont()
font.setPointSize(20)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(290, 90, 161, 41))
font = QtGui.QFont()
font.setPointSize(22)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 537, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Yes"))
self.pushButton_2.setText(_translate("MainWindow", "No"))
self.label_2.setText(_translate("MainWindow", "Is this the right anime?"))
self.label_3.setText(_translate("MainWindow",str(Titles)))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我需要将“URlink”变量传递到用户界面文件
问题是你没有模块化你的项目,逻辑是创建函数或 classes 来实现必要的逻辑并且是独立的,然后加入它们以便函数和对象交互.另一方面,不要修改 pyuic 生成的代码,而是创建另一个 class 继承自合适的 QWidget 并填充它们。
在这种情况下,抓取和下载任务会消耗大量时间,必须在另一个线程中执行,并将信息发送到主线程。
综合以上,解决方案是:
import threading
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
from gui import Ui_MainWindow
def find_link(anime):
# ...
piclink = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[3]/div[2]/table/tbody/tr/td[1]/div/div[1]/a/img').get_attribute('src')
return piclink
def download_image(link):
image = QImage()
image.loadFromData(requests.get(link).content)
return image
class AnimeManager(QObject):
finished = pyqtSignal(QImage)
def start(self, anime):
threading.Thread(target=self._do_work, args=(anime,), daemon=True).start()
def _do_work(self, anime):
link = find_link(anime)
image = download_image(link)
self.finished.emit(image)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.anime_manager = AnimeManager()
self.anime_manager.finished.connect(self.handle_finished)
self.anime_manager.start("Pokemon")
def handle_finished(self, image):
pixmap = QPixmap.fromImage(image)
self.ui.label.setPixmap(pixmap)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
注:恢复Ui_MainWindowclass.