From Urwid Documentation :
The topmost widget displayed by MainLoop should be passed as the first parameter to the constructor. If you want to change the topmost widget while you work, you can assign a new widget to MainLoop objects. MainLoop.widget attribute. This is useful for applications that have a number of different modes or types.
Now for some code:
import urwid
In short, I used the global key processing function to listen to a specific sequence pressed by the user, and upon receiving this sequence my key processing function creates a new view object with the MainView class and replaces loop.widget
with this object. Of course, in a real application, you will want to create a signal handler for a specific widget in your view class, and not use the global unhandled_input function for all user inputs. You can read about connect_signal function here .
Pay attention to the part about garbage collection in the signal documentation: if you are going to write something with many views, they will remain in memory even after you replace them due to the fact that signal_handler is a closure that implicitly refers to this the widget, so you need to pass the weak_args
named argument to the weak_args
function to tell Urwid to leave if it is not actively used in the event loop.
source share