Issue
I am writing a tool that calls some external function using PyQt. Basically, some files shall be loaded in a GUI and the files will then be sent to the external function with some subprocess.run-command. This function takes quite long to run through completely and the tool will be inactive during this time. My goal is thus to change the color/icon/... of a QLabel/QPushButton from "green" to "red" for the time it takes to run through the function. After the function call is complete, I want to change the color back to "green". It should thus imitate a status lamp.
lamp = QtWidgets.QPushButton(self.centralwidget)
lamp.setStyleSheet("background-color: red")
# external function call
lamp.setStyleSheet("background-color: green")
However, the lamp label will not turn red unless the external function call includes something like a QFileDialog or something else that halts the program execution.
I tried to use a Palette instead of the style sheet as well. I also tried to solve the issue using PyQtSignals - nothing worked.
I also tried to force the label to update its color by using
lamp.style().unpolish(lamp);
lamp.style().polish(lamp);
lamp.update();
I would be more than welcome for a solution to this! Thanks in advance!
Solution
I'm not sure if this is what you want.
But I think you should use QThread to execute subprocess.
Give you the QtThread example, you are modifying it yourself.
Or you Google to search for QThread.
class YourQt:
code...
def example(self):
self.lamp = QtWidgets.QPushButton(self.centralwidget)
self.lamp.setStyleSheet("background-color: red")
self.lamp_thread = LampThread()
self.lamp_thread.lamp_signal .connect(self.example_finish)
self.lamp_thread.start()
def example_finish(self, signal):
self.lamp.setStyleSheet("background-color: green")
class LampThread(QtCore.QThread):
lamp_signal = QtCore.pyqtSignal(bool)
def __init__(self):
super(LampThread, self).__init__()
def run(self):
# subprocess.run-command
self.lamp_signal.emit(True)
Answered By - dudulu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.