I am trying to write a program with different views.
I tried to make a class which handles different views with urwid, also to separate the view code from the rest. But after a lot of different tries, i don't know where to start anymore.
Which urwid objects do I need for a clean erasing and redraw of the screen? And how do they need to be encapsulated so i can switch views after user input?
From the Urwid documentation:
Now for some code:
In short, I've used a global key handling function to listen for a certain sequence pressed by the user and on receiving that sequence, my key handling function builds a new view object with the MainView class and replaces
loop.widget
with that object. Of course, in an actual application, you're going to want to create a signal handler on a particular widget in your view class rather than use the global unhandled_input function for all user input. You can read about the connect_signal function here.Note the part about garbage collection in Signal Functions documentation: if you're intending to write something with many views, they will remain in memory even after you've replaced them due to the fact that the signal_handler is a closure, which retains a reference to that widget implicitly, so you need to pass the
weak_args
named argument into theurwid.connect_signal
function to tell Urwid to let it go once its not actively being used in the event loop.