Hello,
I am trying to build a GridBox widget, which is similiar to the GridFlow
widget, but can be scrolled.
In doing so, I encountered a problem using urwid's ListBox when setting a focus
explicitely.
The following code generates a display with 3x5 checkboxes. The focus is
explicitely set on the second entry in the third row.
When running this code in a terminal which is large enough to hold the focus
without scrolling the list, the focus is set to the first entry in the third
row instead of the second.
When the terminal is small enough not to hold the focus without scrolling, the
focus is set properly.
I could trace the problem back to the render method of the ListBox, which
explicitely alters the focus of the contained Columns object.
(See the second code snippet and the corresponding output).
Is this a bug in the ListBox or am I doing something wrong when setting the
focus?
Any help would be appreciated!
Best regards.
Snippet 1:
######################################################################################
#!
/usr/bin/python
import urwid
import random
palette = [('header', 'white', 'dark red', "", "#afd", "#f0f"),
('footer', 'black', 'light blue', "", "#6f0", "#d00"),
('reveal focus', 'dark green', 'yellow', "", "#fd0", "#808"),
('body', 'yellow', 'dark green', 'standout', "#808", "#fd0"),
('edit', 'black', 'light gray', "", "#f0f", "#afd")]
buttons = [urwid.AttrMap(urwid.CheckBox(k), None, 'reveal focus')
for k in ["%d %s" % (i, j)
for i, j in
enumerate(sorted(["".join([chr(random.randint(97, 122))
for x in range(
random.randint(4, 16))])
for j in range(15)]))]]
body = urwid.ListBox(urwid.SimpleListWalker([urwid.Columns(buttons[3*i:3*i+3])
for i in range(len(buttons)/3)]))
body.set_focus(2)
body.get_focus()[0].set_focus(1)
loop = urwid.MainLoop(body, palette)
loop.screen.set_terminal_properties(colors=256)
loop.run()
Snippet 2:
########################################################################
#!
/usr/bin/python
import urwid
import random
import sys
from math import log10
palette = [('header', 'white', 'dark red', "", "#afd", "#f0f"),
('footer', 'black', 'light blue', "", "#6f0", "#d00"),
('reveal focus', 'dark green', 'yellow', "", "#fd0", "#808"),
('body', 'yellow', 'dark green', 'standout', "#808", "#fd0"),
('edit', 'black', 'light gray', "", "#f0f", "#afd")]
def log(txt):
import time
f = open("/tmp/gridbox.log", "a")
f.write("%s: %s\n" % (time.strftime("%H:%M:%S"), txt))
f.close()
class MyListBox(urwid.ListBox):
def __init__(self, *args, **kwargs):
urwid.ListBox.__init__(self, *args, **kwargs)
def render(self, *args, **kwargs):
log("BEFORE RENDER:")
for i in self.body:
l = [j.original_widget for j in i.widget_list]
log("row %d: [%s], focus is on %d" % (id(i),
", ".join([str(id(j)) for j
in l]),
i.get_focus_column()))
retval = super(MyListBox, self).render(*args, **kwargs)
log("AFTER RENDER:")
for i in self.body:
l = [j.base_widget for j in i.widget_list]
log("row %d: [%s], focus is on %d" % (id(i),
", ".join([str(id(j)) for j
in l]),
i.get_focus_column()))
return retval
buttons = [urwid.AttrMap(urwid.CheckBox(k), None, 'reveal focus')
for k in ["%d %s" % (i, j)
for i, j in
enumerate(sorted(["".join([chr(random.randint(97, 122))
for x in
range(random.randint(4, 16))])
for j in range(15)]))]]
body = MyListBox(urwid.SimpleListWalker([urwid.Columns(buttons[3*i:3*i+3])
for i in range(len(buttons)/3)]))
body.set_focus(2)
body.get_focus()[0].set_focus(1)
loop = urwid.MainLoop(body, palette)
loop.screen.set_terminal_properties(colors=256)
loop.run()
Output of snippet 2:
11:51:44: BEFORE RENDER:
11:51:44: row 35364944: [35360976, 35361296, 35361552], focus is on 0
11:51:44: row 35365008: [35361808, 35362064, 35362320], focus is on 0
11:51:44: row 35365072: [35362576, 35362832, 35363088], focus is on 1
11:51:44: row 35365136: [35363344, 35363600, 35363856], focus is on 0
11:51:44: row 35365200: [35364112, 35364368, 35364624], focus is on 0
11:51:44: AFTER RENDER:
11:51:44: row 35364944: [35360976, 35361296, 35361552], focus is on 0
11:51:44: row 35365008: [35361808, 35362064, 35362320], focus is on 0
11:51:44: row 35365072: [35362576, 35362832, 35363088], focus is on 0
11:51:44: row 35365136: [35363344, 35363600, 35363856], focus is on 0
11:51:44: row 35365200: [35364112, 35364368, 35364624], focus is on 0_______________________________________________
Urwid mailing list
[email protected]
http://lists.excess.org/mailman/listinfo/urwid