Question

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()
  1. How do the attributes work in this example ?
  2. What does it mean by foreground and background?
  3. What does it mean by map1 "wraps" txt ? Does it mean you create a widget named map1 and place the widget over the screen position at which txt is placed ? Does the phrase "wrapping a widget A with another widget B" mean the same as when we say "map widget A onto widget B" ?
  4. Is map1 the original_widget for the Filler decoration widget ?
  5. What code part match the vertical height of streak match exactly with vertical height of txt ? Why didn't the vertical height of streak become bigger or smaller than vertical height of txt ? If there would be no map2 then would it mean that the rest of the screen apart from txt1 would be red or just black ?
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top