I am a newcomer to using the Urwid library (built in Python) and I am trying to understand some existing urwid examples at urwid examples. One of them being this code :
import urwid
palette = [
('banner', 'black', 'light gray', 'standout,underline'),
('streak', 'black', 'dark red', 'standout'),
('bg', 'black', 'dark blue'),]
txt = urwid.Text(('banner', u" Hello World "), align='center')
map1 = urwid.AttrMap(txt, 'streak')
fill = urwid.Filler(map1)
map2 = urwid.AttrMap(fill, 'bg')
def exit_on_q(input):
if input in ('q', 'Q'):
raise urwid.ExitMainLoop()
loop = urwid.MainLoop(map2, palette, unhandled_input=exit_on_q)
loop.run()
Attributes are part of the programming style, or paradigm, of object oriented programming (OOP). Object oriented programs are built up from classes, and instances of those classes. Classes are like blueprints, instances are like things made from those blueprints.
For example, you might have a class called Person, and then code like this:
alice = Person(eye_colour="blue",hair_colour="ginger")
bob = Person(eye_colour="brown",hair_colour="black")
The variables eye_colour and hair_colour would then be attributes of alice and bob. You could then do this:
print(alice.eye_colour)
bob.hair_colour = "pink"
print(bob.hair_colour)
This would output the following: blue pink
In the case of urwid, things like instances of Text objects have attributes, in that case things like the text to display and how to align them. For example here - txt = urwid.Text(('banner', u" Hello World "), align='center')
- an instance of the Text class is created and assigned to the variable txt
with a display attribute ('banner', u" Hello World")
and another attribute (align
) with the value 'center'. This means the program will, when displaying the object, display the unicode string " Hello World " in the style of a banner, aligned to the centre.
In answer to some of your questions:
map1 wrapping txt means map1 has txt as an attribute, so the program displays txt in a style based on other attributes of map1, in this case 'streak'. The code that matches the heights is somewhere in the module urwid, which is added to your program by the line import urwid
. If there was no map2 then I think, as it says here then the background colour would be your default terminal colour.