-
I got a small program that consists of multiple screens that act as "pages" for the logic. As the examples had the message as a submessage of a widget but there is no particular widget being affected, I created the Message as a 'free standing' class outside of any App or Screen class. So i post that message and added a decorator that should in Theory trigger by the message. But I doesn't. As I don't know why I came here to ask instead of bashing my head against something I dont grasp properly. I assume there might be multiple sources of error at fault:
# the Message
class ReDrawEvent(Message):
def __init__(self) -> None:
super().__init__() # there is probably no need for an init right?
# the trigger inside a screen class
self.post_message(ReDrawEvent())
#the actuall process inside another screen that also exists
@on(ReDrawEvent)
def message_redraw(self):
self._init_data()
edit it does indeed work to use a global variable .. or rather, to set one in the app class that can be accessed from everywhere via self.app.[var] as I got multiple screens that are affected, this might be the best for this case. Still, this feels really clunky |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @BurnoutDV, Depending on your use case using a ContentSwitcher might be a better solution. In general screens are useful if you are dealing with different data in each of the screens, but from the information you have given it sounds like you are trying to handle the same data. Events bubble to their parents and each One way to do this would be to handle the event in # In your App subclass
@on(ReDrawEvent)
def message_redraw(self) -> None:
# Check if the screen has been activated or not.
# If active update the screen as necessary. An alternative way would be to use Signals and subscribe to the signal in the # In your App class
def __init__(self, ...) -> None:
super().__init__(...)
self.update_signal = Signal[None](self, "update_signal")
@on(ReDrawEvent)
def message_redraw(self) -> None:
self.update_signal.publish(None)
# In your alternate screen class
def on_mount(self) -> None:
self.app.update_signal.subscribe(self, self._re_draw)
def _re_draw(self. _: None) -> None:
# Do what you need to do here... |
Beta Was this translation helpful? Give feedback.
Hi @BurnoutDV,
Depending on your use case using a ContentSwitcher might be a better solution. In general screens are useful if you are dealing with different data in each of the screens, but from the information you have given it sounds like you are trying to handle the same data.
Events bubble to their parents and each
Screen
will be in in its own separate 'column' and the screens will not know about events in the other screens. See Bubbling. If you want to handle these events you will have to do so at theApp
level.One way to do this would be to handle the event in
App
class and control your screens from there: