How to separate GUI and application code?
-
wrote on 30 Jan 2023, 20:43 last edited by
Hi,
I'm trying to use PyQt5 as part of a larger application. I would describe this as an application that has an optional GUI attached. The GUI itself is not the focus of the project. As a result, I don't want the entire codebase littered with PyQt imports. The GUI and the main application run in two different threads. That's the background information. Here's my specific question:
My application has the notion of a battery_percentage that is updated periodically in the application code. I want the GUI to display the battery percentage in a QLabel. Ultimately, in the GUI, I think this resolves to
my_label.setText(f"{new_battery_percentage}%")
, but there are two possibly related questions that I'm not clear on.- How can I register a handler in the UI code to perform the setting of the text as mentioned above? I was reading a bit about signals/slots, but that seems like a way for one widget to communicate with another widget. What I am trying to do is have a non-widget publish something that is consumed by a widget.
- The application code runs in a different thread than the GUI, so how can I publish the battery updated "event" in a way that is safe and doesn't block the application thread?
-
Hi,
I'm trying to use PyQt5 as part of a larger application. I would describe this as an application that has an optional GUI attached. The GUI itself is not the focus of the project. As a result, I don't want the entire codebase littered with PyQt imports. The GUI and the main application run in two different threads. That's the background information. Here's my specific question:
My application has the notion of a battery_percentage that is updated periodically in the application code. I want the GUI to display the battery percentage in a QLabel. Ultimately, in the GUI, I think this resolves to
my_label.setText(f"{new_battery_percentage}%")
, but there are two possibly related questions that I'm not clear on.- How can I register a handler in the UI code to perform the setting of the text as mentioned above? I was reading a bit about signals/slots, but that seems like a way for one widget to communicate with another widget. What I am trying to do is have a non-widget publish something that is consumed by a widget.
- The application code runs in a different thread than the GUI, so how can I publish the battery updated "event" in a way that is safe and doesn't block the application thread?
wrote on 30 Jan 2023, 21:12 last edited by@dpfrey
The answer to both is you can and should use signals/slots. They also work across your threads correctly. Signals/slots are for Qt objects, not just widgets. Write a single Python module which includes PyQt and emits signals, then rest of codebase can call these as necessary without them having to include/know about PyQt/Qt. -
@dpfrey
The answer to both is you can and should use signals/slots. They also work across your threads correctly. Signals/slots are for Qt objects, not just widgets. Write a single Python module which includes PyQt and emits signals, then rest of codebase can call these as necessary without them having to include/know about PyQt/Qt.
1/3