How do I pass a existing variable into a pyqt5 ui file that is called?

I’m using PyQt5 for my interface. I want to import the link variable from my main program into the GUI program so that it can display the link fetched from my main program.

This is how I want it to work:

Link is fetched –> Link is put into a variable –> Link is passed into the GUI file –> GUI displays the image link in the GUI.

Watered down version of my code:

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:UsersameteDocumentschromedriver.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 code

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

I need to pass “URlink” variable into the user interface file

Answer

The problem is that you are not modularized your project, the logic is to create functions or classes that implement the necessary logic and that are independent, after that join them so that the functions and objects interact. On the other hand, do not modify the code generated by pyuic but create another class that inherits from a suitable QWidget and fill them.

In this case, you must execute the scraping and download tasks in another thread since they consume a lot of time, and send the information to the main thread.

Considering the above, the solution is:

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

Note: restore Ui_MainWindow class.