Last active
June 23, 2020 19:44
-
-
Save monsieurp/24e7ee612c712f280b832990aaf50ba8 to your computer and use it in GitHub Desktop.
dialog(1) like example with urwid using a single main loop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import urwid | |
from urwid import MainLoop | |
import time | |
import sys | |
import os | |
PALETTE = [ | |
('s1_titlebar', 'white,bold', 'yellow'), | |
('s2_titlebar', 'white,bold', 'light green'), | |
('fruit', 'white', 'dark blue'), | |
('filler', 'white', 'dark blue'), | |
('focus', 'white', 'dark blue'), | |
('s1_selectable', 'black', 'yellow'), | |
('s2_selectable', 'black', 'light green') | |
] | |
class Screen1Widget(urwid.WidgetWrap): | |
fruit_text = """Do you like Bananas?""" | |
def __init__(self, app): | |
self.app = app | |
self.header = urwid.AttrMap(urwid.Text('Bananas Inc.'), 's1_titlebar') | |
body = urwid.AttrMap(urwid.Text(self.fruit_text), 'fruit') | |
yes = urwid.Button('Yes', self.app.returncode) | |
yes.rc = 0 | |
yes = urwid.AttrMap(yes, 'focus', 's1_selectable') | |
no = urwid.Button('No', self.app.returncode) | |
no.rc = 1 | |
no = urwid.AttrMap(no, 'focus', 's1_selectable') | |
buttons = urwid.GridFlow([yes, no], 10, 3, 1, 'center') | |
body = urwid.Pile([body, urwid.Divider(), buttons], focus_item=2) | |
body = urwid.Filler(body, valign='top', top=1, bottom=1) | |
body = urwid.AttrMap(body, 'filler') | |
sfill = urwid.AttrMap(urwid.SolidFill(' '), 'filler') | |
body = urwid.LineBox(body) | |
body = urwid.AttrMap(body, 'filler') | |
self._w = urwid.Overlay(body, sfill, | |
align='center', valign='middle', | |
width=('relative', 40), height=('relative', 20) | |
) | |
class Screen2Widget(urwid.WidgetWrap): | |
fruit_text = """Do you like Apples?""" | |
def __init__(self, app): | |
self.app = app | |
self.header = urwid.AttrMap(urwid.Text('Apple Inc.'), 's2_titlebar') | |
body = urwid.AttrMap(urwid.Text(self.fruit_text), 'fruit') | |
yes = urwid.Button('Yes', self.app.returncode) | |
yes.rc = 0 | |
yes = urwid.AttrMap(yes, 'focus', 's2_selectable') | |
no = urwid.Button('No', self.app.returncode) | |
no.rc = 1 | |
no = urwid.AttrMap(no, 'focus', 's2_selectable') | |
buttons = urwid.GridFlow([yes, no], 10, 3, 1, 'center') | |
body = urwid.Pile([body, urwid.Divider(), buttons], focus_item=2) | |
body = urwid.Filler(body, valign='top', top=1, bottom=1) | |
body = urwid.AttrMap(body, 'filler') | |
sfill = urwid.AttrMap(urwid.SolidFill(' '), 'filler') | |
body = urwid.LineBox(body) | |
body = urwid.AttrMap(body, 'filler') | |
self._w = urwid.Overlay(body, sfill, | |
align='center', valign='middle', | |
width=('relative', 40), height=('relative', 20) | |
) | |
class App: | |
def __init__(self): | |
self.cur_screen_number = 0 | |
self.cur_screen = Screen1Widget(self) | |
self.layout = urwid.Frame( | |
header=self.cur_screen.header, | |
body=self.cur_screen._w | |
) | |
def returncode(self, bt): | |
if bt.rc == 0 and self.cur_screen_number == 0: | |
self.cur_screen = Screen2Widget(self) | |
self.layout.header = self.cur_screen.header | |
self.layout.body = self.cur_screen._w | |
self.cur_screen_number += 1 | |
elif bt.rc == 0 and self.cur_screen_number == 1: | |
raise urwid.ExitMainLoop() | |
if __name__ == '__main__': | |
app = App() | |
loop = urwid.MainLoop(app.layout, PALETTE) | |
loop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment