-2

How can I access a variable that is defined after the function in Python?

this is my code:

from PyQt5.QtWidgets import (QApplication, QLabel, QMainWindow, QSlider)
from PyQt5.QtCore import Qt
import sys


app = QApplication(sys.argv)

window = QMainWindow()


def change_value():

    window.setStyleSheet('QLabel{ \n'
                       'font-size:20px;\n'
                       'background:transparent;\n'
                       '}\n'
                       'QMainWindow{ \n'
                       f'background:rgb({red_slider.value()}, {green_slider.value()}, {blue_slider.value()});\n'
                       '}')

    red_value.setText(f'{rsv}')
    green_value.setText(f'{gsv}')
    blue_value.setText(f'{bsv}')
    


#-------------------------------------------------------------------#

red_label = QLabel('red', window)                                   # 
red_label.move(140, 247)                                            #
#                                                                   #
red_slider = QSlider(Qt.Horizontal, window)                         #
red_slider.setGeometry(190, 250, 200, 30)                           # 
red_slider.setMinimum(0)                                            # red
red_slider.setMaximum(255)                                          #
red_slider.valueChanged.connect(change_value)                       #
red_slider.setValue(255)                                            #
#                                                                   #
red_value = QLabel('0', window)                                     #
red_value.move(400, 247)
#-------------------------------------------------------------------#


#----------------------------------------------------------------------#

green_label = QLabel('green', window)                                  # 
green_label.move(135, 297)                                             #
#                                                                      #
green_slider = QSlider(Qt.Horizontal, window)                          #
green_slider.setGeometry(190, 300, 200, 30)                            # 
green_slider.setMinimum(0)                                             # green
green_slider.setMaximum(255)                                           # 
green_slider.valueChanged.connect(change_value)                        #
green_slider.setValue(255)                                             #
#                                                                      #
green_value = QLabel('0', window)                                      #
green_value.move(400, 297)
#----------------------------------------------------------------------#


#--------------------------------------------------------------------#

blue_label = QLabel('blue', window)                                  # 
blue_label.move(140, 347)                                            #
#                                                                    #
blue_slider = QSlider(Qt.Horizontal, window)                         #
blue_slider.setGeometry(190, 350, 200, 30)                           # 
blue_slider.setMinimum(0)                                            # blue
blue_slider.setMaximum(255)                                          #
blue_slider.valueChanged.connect(change_value)                       #
blue_slider.setValue(255)                                            #
#                                                                    #
blue_value = QLabel('0', window)                                     #
blue_value.move(400, 347)
#--------------------------------------------------------------------#



window.setStyleSheet('QLabel{ \n'
                   'font-size:20px;\n'
                   'background:transparent;\n'
                   '}\n'
                   'QMainWindow{ \n'
                   f'background:rgb({red_slider.value()}, {green_slider.value()}, {blue_slider.value()});\n'
                   '}')


window.show()

app.exec_()

and I got the error here:

 f'background:rgb({red_slider.value()}, {green_slider.value()}, {blue_slider.value()});\n'

in the function, (red_slider) works good but (green_slider) and (blue_slider) gives me this error:

Traceback (most recent call last):
  File "E:\????? ????\????? ????\??????\??????? PyQt5\????? 1.py", line 18, in change_value
    f'background:rgb({red_slider.value()}, {green_slider.value()}, {blue_slider.value()});\n'
NameError: name 'green_slider' is not defined

if someone knows what is wrong please tell me

and thanks in advance

1 Answer 1

2

You need to bind signals and slots after creating all objects (red_slider, green_slider, blue_slider)

import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QMainWindow, QSlider)
from PyQt5.QtCore import Qt


app = QApplication(sys.argv)
window = QMainWindow()

def change_value(value):
    window.setStyleSheet('QLabel{ \n'
                       'font-size:20px;\n'
                       'background:transparent;\n'
                       '}\n'
                       'QMainWindow{ \n'
                       f'background:rgb({red_slider.value()}, {green_slider.value()}, {blue_slider.value()});\n'
                       '}')

#    red_value.setText(f'{rsv}')
#    green_value.setText(f'{gsv}')
#    blue_value.setText(f'{bsv}')
    red_value.setText(f'{red_slider.value()}')
    green_value.setText(f'{green_slider.value()}')
    blue_value.setText(f'{blue_slider.value()}')


#-------------------------------------------------------------------#

red_label = QLabel('red', window)                                   # 
red_label.move(140, 247)                                            #
#                                                                   #
red_slider = QSlider(Qt.Horizontal, window)                         #
red_slider.setGeometry(190, 250, 200, 30)                           # 
red_slider.setMinimum(0)                                            # red
red_slider.setMaximum(255)                                          #
#red_slider.valueChanged.connect(change_value)                       #
red_slider.setValue(255)                                            #
#                                                                   #
red_value = QLabel('0', window)                                     #
red_value.move(400, 247)
#-------------------------------------------------------------------#


#----------------------------------------------------------------------#

green_label = QLabel('green', window)                                  # 
green_label.move(135, 297)                                             #
#                                                                      #
green_slider = QSlider(Qt.Horizontal, window)                          #
green_slider.setGeometry(190, 300, 200, 30)                            # 
green_slider.setMinimum(0)                                             # green
green_slider.setMaximum(255)                                           # 
#green_slider.valueChanged.connect(change_value)                        #
green_slider.setValue(255)                                             #
#                                                                      #
green_value = QLabel('0', window)                                      #
green_value.move(400, 297)
#----------------------------------------------------------------------#


#--------------------------------------------------------------------#

blue_label = QLabel('blue', window)                                  # 
blue_label.move(140, 347)                                            #
#                                                                    #
blue_slider = QSlider(Qt.Horizontal, window)                         #
blue_slider.setGeometry(190, 350, 200, 30)                           # 
blue_slider.setMinimum(0)                                            # blue
blue_slider.setMaximum(255)                                          #
#blue_slider.valueChanged.connect(change_value)                       #
blue_slider.setValue(255)                                            #
#                                                                    #
blue_value = QLabel('0', window)                                     #
blue_value.move(400, 347)
#--------------------------------------------------------------------#


red_slider.valueChanged.connect(change_value)                        #      <<<-----
green_slider.valueChanged.connect(change_value)                      #      <<<-----
blue_slider.valueChanged.connect(change_value)                       #      <<<-----



window.setStyleSheet('QLabel{ \n'
                   'font-size:20px;\n'
                   'background:transparent;\n'
                   '}\n'
                   'QMainWindow{ \n'
                   f'background:rgb({red_slider.value()}, {green_slider.value()}, {blue_slider.value()});\n'
                   '}')


window.show()
window.resize(500, 500)

app.exec_()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much it works with me, but Can you explain to me what is the different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.