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
