V
V
Vladislav2019-12-20 03:41:54
Python
Vladislav, 2019-12-20 03:41:54

How to describe the button color changing function by condition in PyQT?

There is a front made on PyQT widgets.
This code describes how to launch the browser and the functions of emulating clicks inside the page, called by button clicks. To simplify, I cut JS scripts and the number of buttons here

from PyQt5 import QtWidgets
        from interface import Ui_MainWindow
        from selenium import webdriver
        from selenium.webdriver.common.keys import Keys
        from selenium.webdriver.common.by import By
        from selenium.webdriver.common.action_chains import ActionChains
        from selenium.webdriver.support import expected_conditions
        from selenium.webdriver.support.wait import WebDriverWait
        
        driver = webdriver.Chrome(executable_path='C:\chromedriver.exe')
        url = "https://www.google.com/"
        driver.get(url) 
        
        class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
            def __init__(self):  #связываем функции с UI кнопками
                super().__init__()
                self.setupUi(self)
                self.pushButton.clicked.connect(self.NoAdress)
                self.pushButton_2.clicked.connect(self.SpecTransp)
                self.pushButton_3.clicked.connect(self.NoSvetofor)
        
            def NoAdress(self):          #функция для кнопки 1
                driver.execute_script('''some js code 1''')
                         
            def SpecTransp(self): #функция для кнопки 2
                driver.execute_script('''some js code 2''')
        
            def NoSvetofor(self): #функция для кнопки 3
                driver.execute_script('''some js code 3''')
            
        if __name__ == '__main__':
            app = QtWidgets.QApplication([])
            window = ExampleApp()
            window.show()
            app.exec_()

Sticking this code into one of the button functions changes its color
self.pushButton.setStyleSheet('QPushButton {background-color: red; color: white;}')

How to describe a function that will change the color of one of the buttons depending on a given condition? Those. let's say if x == 1: then button_1 is highlighted in red, if x = 2: then button 2 is highlighted in red, x == 3: button_3, etc.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question