Reorganizing
This commit is contained in:
81
old/ex1.py
Normal file
81
old/ex1.py
Normal file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import curses
|
||||
from curses import panel
|
||||
|
||||
|
||||
class Menu(object):
|
||||
def __init__(self, items, stdscreen):
|
||||
self.window = stdscreen.subwin(0, 0)
|
||||
self.window.keypad(1)
|
||||
self.panel = panel.new_panel(self.window)
|
||||
self.panel.hide()
|
||||
panel.update_panels()
|
||||
|
||||
self.position = 0
|
||||
self.items = items
|
||||
self.items.append(("exit", "exit"))
|
||||
|
||||
def navigate(self, n):
|
||||
self.position += n
|
||||
if self.position < 0:
|
||||
self.position = 0
|
||||
elif self.position >= len(self.items):
|
||||
self.position = len(self.items) - 1
|
||||
|
||||
def display(self):
|
||||
self.panel.top()
|
||||
self.panel.show()
|
||||
self.window.clear()
|
||||
|
||||
while True:
|
||||
self.window.refresh()
|
||||
curses.doupdate()
|
||||
for index, item in enumerate(self.items):
|
||||
if index == self.position:
|
||||
mode = curses.A_REVERSE
|
||||
else:
|
||||
mode = curses.A_NORMAL
|
||||
|
||||
msg = "%d. %s" % (index, item[0])
|
||||
self.window.addstr(1 + index, 1, msg, mode)
|
||||
|
||||
key = self.window.getch()
|
||||
|
||||
if key in [curses.KEY_ENTER, ord("\n")]:
|
||||
if self.position == len(self.items) - 1:
|
||||
break
|
||||
else:
|
||||
self.items[self.position][1]()
|
||||
|
||||
elif key == curses.KEY_UP:
|
||||
self.navigate(-1)
|
||||
|
||||
elif key == curses.KEY_DOWN:
|
||||
self.navigate(1)
|
||||
|
||||
self.window.clear()
|
||||
self.panel.hide()
|
||||
panel.update_panels()
|
||||
curses.doupdate()
|
||||
|
||||
|
||||
class MyApp(object):
|
||||
def __init__(self, stdscreen):
|
||||
self.screen = stdscreen
|
||||
curses.curs_set(0)
|
||||
|
||||
submenu_items = [("beep", curses.beep), ("flash", curses.flash)]
|
||||
submenu = Menu(submenu_items, self.screen)
|
||||
|
||||
main_menu_items = [
|
||||
("beep", curses.beep),
|
||||
("flash", curses.flash),
|
||||
("submenu", submenu.display),
|
||||
]
|
||||
main_menu = Menu(main_menu_items, self.screen)
|
||||
main_menu.display()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
curses.wrapper(MyApp)
|
125
old/ex2.py
Normal file
125
old/ex2.py
Normal file
@@ -0,0 +1,125 @@
|
||||
#!/bin/env python3
|
||||
|
||||
import urwid as u
|
||||
|
||||
class ListItem(u.WidgetWrap):
|
||||
|
||||
def __init__ (self, country):
|
||||
|
||||
self.content = country
|
||||
|
||||
name = country["name"]
|
||||
|
||||
t = u.AttrWrap(u.Text(name), "country", "country_selected")
|
||||
|
||||
u.WidgetWrap.__init__(self, t)
|
||||
|
||||
def selectable (self):
|
||||
return True
|
||||
|
||||
def keypress(self, size, key):
|
||||
return key
|
||||
|
||||
class ListView(u.WidgetWrap):
|
||||
|
||||
def __init__(self):
|
||||
|
||||
u.register_signal(self.__class__, ['show_details'])
|
||||
|
||||
self.walker = u.SimpleFocusListWalker([])
|
||||
|
||||
lb = u.ListBox(self.walker)
|
||||
|
||||
u.WidgetWrap.__init__(self, lb)
|
||||
|
||||
def modified(self):
|
||||
|
||||
focus_w, _ = self.walker.get_focus()
|
||||
|
||||
u.emit_signal(self, 'show_details', focus_w.content)
|
||||
|
||||
def set_data(self, countries):
|
||||
|
||||
countries_widgets = [ListItem(c) for c in countries]
|
||||
|
||||
u.disconnect_signal(self.walker, 'modified', self.modified)
|
||||
|
||||
while len(self.walker) > 0:
|
||||
self.walker.pop()
|
||||
|
||||
self.walker.extend(countries_widgets)
|
||||
|
||||
u.connect_signal(self.walker, "modified", self.modified)
|
||||
|
||||
self.walker.set_focus(0)
|
||||
|
||||
class DetailView(u.WidgetWrap):
|
||||
|
||||
def __init__ (self):
|
||||
t = u.Text("")
|
||||
u.WidgetWrap.__init__(self, t)
|
||||
|
||||
def set_country(self, c):
|
||||
s = f'Name: {c["name"]}\nPop: {c["pop"]}\nGDP: {c["gdp"]}'
|
||||
self._w.set_text(s)
|
||||
|
||||
class App(object):
|
||||
|
||||
def unhandled_input(self, key):
|
||||
if key in ('q',):
|
||||
raise u.ExitMainLoop()
|
||||
|
||||
def show_details(self, country):
|
||||
self.detail_view.set_country(country)
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.palette = {
|
||||
("bg", "black", "white"),
|
||||
("country", "black", "white"),
|
||||
("country_selected", "black", "yellow"),
|
||||
("footer", "white, bold", "dark red")
|
||||
}
|
||||
|
||||
self.list_view = ListView()
|
||||
self.detail_view = DetailView()
|
||||
|
||||
u.connect_signal(self.list_view, 'show_details', self.show_details)
|
||||
|
||||
footer = u.AttrWrap(u.Text(" Q to exit"), "footer")
|
||||
|
||||
col_rows = u.raw_display.Screen().get_cols_rows()
|
||||
h = col_rows[0] - 2
|
||||
|
||||
f1 = u.Filler(self.list_view, valign='top', height=h)
|
||||
f2 = u.Filler(self.detail_view, valign='top')
|
||||
|
||||
c_list = u.LineBox(f1, title="Countries")
|
||||
c_details = u.LineBox(f2, title="Country Details")
|
||||
|
||||
columns = u.Columns([('weight', 30, c_list), ('weight', 70, c_details)])
|
||||
|
||||
frame = u.AttrMap(u.Frame(body=columns, footer=footer), 'bg')
|
||||
|
||||
self.loop = u.MainLoop(frame, self.palette, unhandled_input=self.unhandled_input)
|
||||
|
||||
def update_data(self):
|
||||
|
||||
l = [] # https://databank.worldbank.org/embed/Population-and-GDP-by-Country/id/29c4df41
|
||||
l.append({"name":"USA", "pop":"325,084,756", "gdp":"$ 19.485 trillion"})
|
||||
l.append({"name":"China", "pop":"1,421,021,791", "gdp":"$ 12.238 trillion"})
|
||||
l.append({"name":"Japan", "pop":"127,502,725", "gdp":"$ 4.872 trillion"})
|
||||
l.append({"name":"Germany", "pop":"82,658,409", "gdp":"$ 3.693 trillion"})
|
||||
l.append({"name":"India", "pop":"1,338,676,785", "gdp":"$ 2.651 trillion"})
|
||||
|
||||
self.list_view.set_data(l)
|
||||
|
||||
def start(self):
|
||||
|
||||
self.update_data()
|
||||
self.loop.run()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
app = App()
|
||||
app.start()
|
151
old/ex3.py
Normal file
151
old/ex3.py
Normal file
@@ -0,0 +1,151 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import curses
|
||||
from curses import panel
|
||||
|
||||
|
||||
|
||||
class Menu(object):
|
||||
def __init__(self, items, stdscreen):
|
||||
self.window = stdscreen
|
||||
self.window.keypad(1)
|
||||
self.panel = panel.new_panel(self.window)
|
||||
self.panel.hide()
|
||||
panel.update_panels()
|
||||
self.position = 0
|
||||
self.items = items
|
||||
self.items.append(("exit", "exit"))
|
||||
|
||||
def navigate(self, n):
|
||||
self.position += n
|
||||
if self.position < 0:
|
||||
self.position = 0
|
||||
elif self.position >= len(self.items):
|
||||
self.position = len(self.items) - 1
|
||||
|
||||
def display(self):
|
||||
self.panel.top()
|
||||
self.panel.show()
|
||||
self.window.clear()
|
||||
self.window.box()
|
||||
|
||||
while True:
|
||||
self.window.refresh()
|
||||
curses.doupdate()
|
||||
for index, item in enumerate(self.items):
|
||||
if index == self.position:
|
||||
mode = curses.A_REVERSE
|
||||
else:
|
||||
mode = curses.A_NORMAL
|
||||
|
||||
msg = "%s" % (item[0])
|
||||
self.window.addstr(1 + index, 1, msg, mode)
|
||||
|
||||
key = self.window.getch()
|
||||
|
||||
if key in [curses.KEY_ENTER, ord("\n")]:
|
||||
if self.position == len(self.items) - 1:
|
||||
break
|
||||
else:
|
||||
self.items[self.position][1]()
|
||||
|
||||
elif key == curses.KEY_UP:
|
||||
self.navigate(-1)
|
||||
|
||||
elif key == curses.KEY_DOWN:
|
||||
self.navigate(1)
|
||||
|
||||
self.window.clear()
|
||||
self.panel.hide()
|
||||
panel.update_panels()
|
||||
curses.doupdate()
|
||||
|
||||
|
||||
def resist(menu):
|
||||
resistors_items = [("10R", curses.beep), ("10K", curses.beep), ("330R", curses.flash), ("57K", curses.flash)]
|
||||
resistorsMenu = Menu(resistors_items, menu)
|
||||
resistorsMenu.display()
|
||||
|
||||
|
||||
class MyApp(object):
|
||||
|
||||
def __init__(self, screen):
|
||||
screen = curses.initscr()
|
||||
curses.curs_set(0)
|
||||
curses.start_color()
|
||||
curses.init_color(0,0,0,0)
|
||||
screen.box()
|
||||
|
||||
cols22 = (curses.COLS - 2) * 0.22 # Take 2 characters out from the boarders and then calculate 22%
|
||||
cols22 = int(cols22) # Ditch the fractional part
|
||||
|
||||
cols56 = (curses.COLS - 2) * 0.56 # Take 2 characters out from the boarders and then calculate 56%
|
||||
cols56 = int(cols56) + 1 # Ditch the fractional part and add 1
|
||||
|
||||
lines = (curses.LINES - 2) * 0.95 # Take 2 characters out from the boarders and then calculate 95%
|
||||
lines = int(lines) # Ditch the fractional part
|
||||
|
||||
|
||||
|
||||
# title = "Sparks-Skraps" + " COLS -> " + str(curses.COLS) + " lines -> " + str(cols56)
|
||||
title = "Sparks-Skraps"
|
||||
titlePos = (curses.COLS / 2) - (len(title) / 2)
|
||||
|
||||
screen.addstr(1, int(titlePos), title, curses.A_BOLD)
|
||||
screen.refresh()
|
||||
|
||||
|
||||
components = curses.newwin(lines,cols22,3,1)
|
||||
components.box()
|
||||
components.addstr(1, 1, "precious components")
|
||||
components.refresh()
|
||||
|
||||
parts = curses.newwin(lines, cols22, 3, 1+cols22)
|
||||
parts.box()
|
||||
parts.addstr(1, 1, "My precious parts")
|
||||
parts.refresh()
|
||||
|
||||
info = curses.newwin(lines, cols56, 3, 1+cols22*2)
|
||||
info.box()
|
||||
info.addstr(1, 1, "My precious info")
|
||||
info.refresh()
|
||||
|
||||
|
||||
# resistors_items = [("10R", curses.beep), ("10K", curses.beep), ("330R", curses.flash), ("57K", curses.flash)]
|
||||
# resistorsMenu = Menu(resistors_items, parts)
|
||||
# resistorsMenu.display()
|
||||
|
||||
components_items = [("Resistors", curses.beep), ("Capacitors", curses.beep), ("Coils", curses.flash), ("IC\'s", curses.flash)]
|
||||
componentsMenu = Menu(components_items, components)
|
||||
componentsMenu.display()
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
curses.wrapper(MyApp)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
'''
|
||||
|
||||
components = curses.newwin(lines,cols22,3,1)
|
||||
# components.box()
|
||||
# components.addstr(1, 1, "precious components")
|
||||
components.refresh()
|
||||
|
||||
parts = curses.newwin(lines, cols22, 3, 1+cols22)
|
||||
parts.box()
|
||||
parts.addstr(1, 1, "My precious parts")
|
||||
parts.refresh()
|
||||
|
||||
info = curses.newwin(lines, cols56, 3, 1+cols22*2)
|
||||
info.box()
|
||||
info.addstr(1, 1, "My precious info")
|
||||
info.refresh()
|
||||
'''
|
||||
|
Reference in New Issue
Block a user