0

Am I doing something wrong with:

self.main_self.ui_manage_sounds_window.frame_5.setStyleSheet("QFrame{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgb(255, 255, 0), stop:1 rgb("+str(stop_red)+", "+str(stop_green)+", 0))}")

The command run every 125 msec, but after a few seconds the program stops.

Sometimes in the console this message is displaied:

Could not parse stylesheet of object QFrame(0x21ede8778e0, name = "frame_5")
Could not parse stylesheet of object QFrame(0x21ede8778e0, name = "frame_5")

volume_strength.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(626, 28)
        self.gridLayout = QtWidgets.QGridLayout(Dialog)
        self.gridLayout.setObjectName("gridLayout")
        self.volume_strength = QtWidgets.QFrame(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.volume_strength.sizePolicy().hasHeightForWidth())
        self.volume_strength.setSizePolicy(sizePolicy)
        self.volume_strength.setMinimumSize(QtCore.QSize(0, 5))
        self.volume_strength.setMaximumSize(QtCore.QSize(16777215, 5))
        self.volume_strength.setStyleSheet("QFrame{\n"
"    border:1px solid #dadada;\n"
"}")
        self.volume_strength.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.volume_strength.setFrameShadow(QtWidgets.QFrame.Raised)
        self.volume_strength.setObjectName("volume_strength")
        self.frame = QtWidgets.QFrame(self.volume_strength)
        self.frame.setGeometry(QtCore.QRect(0, 0, 0, 16))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
        self.frame.setSizePolicy(sizePolicy)
        self.frame.setMinimumSize(QtCore.QSize(0, 0))
        self.frame.setStyleSheet("QFrame{\n"
"    background-color:blue;\n"
"}")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.gridLayout.addWidget(self.volume_strength, 0, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

volume_strength_code.py

from PyQt5 import QtCore, QtGui, QtWidgets
from volume_strength import *

import sys
import random
import time

import threading

class VolumeStregnth:
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        self.ui = Ui_Dialog()
        self.ui.setupUi(Dialog)
        Dialog.show()
        
        
        self.animate_thread = AnimateThread(self.ui)
        self.animate_thread.start()
        
        app.exec_()
        
class AnimateThread(threading.Thread):
    def __init__(self,ui):
        threading.Thread.__init__(self)
        self.ui = ui
        self.frame_width = self.ui.volume_strength.geometry().width()
        
    def run(self):
        while(True):
            stop_red = 255
            stop_green = int(random.uniform(0, 1)*255)
            f_width = int(random.uniform(0, 1)*self.frame_width)
            #print(f_width)
            self.ui.frame.setGeometry(QtCore.QRect(0, 0, f_width, 16))
            self.ui.frame.setStyleSheet("QFrame{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgb(255, 255, 0), stop:1 rgb("+str(stop_red)+", "+str(stop_green)+", 0))}")
            time.sleep(0.125)




volume_strength = VolumeStregnth()

Same error.

Now this:

enter image description here

Chris P
  • 2,059
  • 4
  • 34
  • 68
  • As usual, please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – musicamante Apr 03 '21 at 14:48
  • @musicamante did it as you left – Chris P Apr 03 '21 at 15:21
  • Access to UI elements (which obviously includes setting their stylesheet) is forbidden from external threads. For what you need, there's no need for threads at all, just use a QTimer that constantly updates the stylesheet. See the "[equivalent to time.sleep?](https://stackoverflow.com/questions/41545300/equivalent-to-time-sleep)" marked as duplicate for this. (For future reference, this is exactly why we ask for a MRE, so please avoid any other pointless complaint) – musicamante Apr 03 '21 at 15:27
  • Ok i will try, and i will tell you the results. – Chris P Apr 03 '21 at 15:27
  • The snippet is used insead a pyaudio callback which is not inside a thread. – Chris P Apr 03 '21 at 15:29
  • @musicamante i tested 1 minute with QTimer and it seems to work. But i don't know how can i manage it for my purpose (pyaudio callback)? – Chris P Apr 03 '21 at 15:34
  • Thanks musicamante, that was (synchronization subject), now i removed stream_callback, and i put a QTimer, and self.stream.write(data). I have tested two. Possible a true solution. – Chris P Apr 03 '21 at 15:47

0 Answers0