using urwid create a inline button within a block of text. Similar to <a> within the <p> tag in HTML

49 Views Asked by At

Im looking to write a program that ultimately looks similar to what you can get with HTML. In a single paragraph you can have some words or phrases acting as buttons which lead you to another page.

"text text text button text text text button text text text "

Essentially im looking to create what is a inline button without using columns because the the button could be the 5th word but depending on the length of the words it could end up being placed on the next row. Is there a way I can stack widgets left->right, top->bottom, or at least produce inline buttons.

I tried using selectable_icons instead of text widgets to get select-able/highlight-able text but I cant stack horizontally without using columns. And the window is expected to resize as well.

The closest I got to what I want is this.

class ClickyText(urwid.Text):                                                                                                                                                                   
ignore_focus = False                                                                                                                                                                        
_selectable = True                                                                                                                                                                          
def __init__(self, text, cursor_position=0):                                                                                                                                                
    self.__super.__init__(text)                                                                                                                                                             
    self._cursor_position = cursor_position                                                                                                                                                 
                                                                                                                                                                                            
def render(self, size, focus=False):                                                                                                                                                        
    c = self.__super.render(size, focus)                                                                                                                                                    
    if focus:                                                                                                                                                                               
        c = CompositeCanvas(c)                                                                                                                                                              
        c.cursor = self.get_cursor_coords(size)                                                                                                                                             
    return c                                                                                                                                                                                
                                                                                                                                                                                            
def get_cursor_coords(self, size):                                                                                                                                                          
    (maxcol,) = size                                                                                                                                                                        
    trans = self.get_line_translation(maxcol)                                                                                                                                               
    x, y = calc_coords(self.text, trans, self._cursor_position)                                                                                                                             
    if maxcol <= x:                                                                                                                                                                         
        return None                                                                                                                                                                         
    return x, y                                                                                                                                                                             
                                                                                                                                                                                            
def keypress(self, size, key):                                                                                                                                                              
    return key                                                                                                                                                                              

~

0

There are 0 best solutions below