Urwid: make cursor invisible

2024/10/3 14:19:00

I'm using urwid, which is a Python "framework" for designing terminal user interfaces in ncurses. There's one thing though that I'm not able to do in urwid that was easy in curses - make the cursor invisible. As it is now, the cursor is visible when selecting buttons, and it just looks plain ugly. Is there a way to disable it?

Answer

I agree that the flashing cursor on an urwid.Button looks a bit lame, so I've come up with a solution to hide it. In urwid, the Button class is just a subclass of WidgetWrap containing a SelectableIcon and two Text widgets (the enclosing "<" and ">"). It's the SelectableIcon class that sets the cursor position to the first character of the label, by default. By subclassing SelectableIcon, modifying the cursor position and then wrapping it into an urwid.WidgetWrap subclass you can create your own custom button that can do all the tricks a built-in Button, or even more.

Here' what it looks like in my project.

enter image description here

import urwidclass ButtonLabel(urwid.SelectableIcon):def __init__(self, text):"""Here's the trick: we move the cursor out to the right of the label/text, so it doesn't show"""curs_pos = len(text) + 1 urwid.SelectableIcon.__init__(self, text, cursor_position=curs_pos)

Next, you can wrap a ButtonLabel object along with any other objects into a WidgetWrap subclass that will be your custom button class.

class FixedButton(urwid.WidgetWrap):_selectable = Truesignals = ["click"]def __init__(self, label):self.label = ButtonLabel(label)# you could combine the ButtonLabel object with other widgets heredisplay_widget = self.label urwid.WidgetWrap.__init__(self, urwid.AttrMap(display_widget, None, focus_map="button_reversed"))def keypress(self, size, key):"""catch all the keys you want to handle hereand emit the click signal along with any data """passdef set_label(self, new_label):# we can set the label at run time, if necessaryself.label.set_text(str(new_label))def mouse_event(self, size, event, button, col, row, focus):"""handle any mouse events hereand emit the click signal along with any data """pass

In this code, there is actually not much combination of widgets in the FixedButton WidgetWrap subclass, but you could add a "[" and "]" to the edges of the button, wrap it into a LineBox, etc. If all this is superfluous, you can just move the event handling functions into the ButtonLabel class, and make it emit a signal when it gets clicked.

To make the button reversed when the user moves on it, wrap it into AttrMap and set the focus_map to some palette entry ("button_reversed", in my case).

https://en.xdnf.cn/q/70712.html

Related Q&A

How do I use scipy.weave.inline together with external C libraries?

I am trying to understand weave.inline to wrap C code in my Python programs. The code below simply takes the Numpy array and multiplicates all of its elements by 2.inl.py import numpy import scipy.weav…

sqlalchemy multiple foreign keys to same table

I have a postgres database that looks something like this:Table "public.entities"Column | Type | Modifiers ---------------+---…

Django - Return a file from Root folder via a URL

I purchased a SSL cert online and now ind the mid of verifying my host. How it works is:It gives me a file I have to make that file accessible through a specific URL on my host. If the content of the f…

Flask deployement on lighttpd and raspberry pi

Im trying to deploy a hello flask app to a raspberry pi using lighttpd fastCGI.I followed the instructions on the http://flask.pocoo.org/docs/0.10/deploying/fastcgi/ to the best of my abilityHere is my…

Django admin asks for login after every click

Im working on a Django app hosted on Heroku. Im able to login to the admin with my username, password. But on every single click (or on each click after a few seconds) it redirects me to the login page…

Change numerical Data to Categorical Data - Pandas [duplicate]

This question already has answers here:How to create new values in a pandas dataframe column based on values from another column(2 answers)Closed 6 years ago.I have a pandas dataframe which has a numer…

Why is dataclasses.astuple returning a deepcopy of class attributes?

In the code below the astuple function is carrying out a deep copy of a class attribute of the dataclass. Why is it not producing the same result as the function my_tuple? import copy import dataclass…

customize dateutil.parser century inference logic

I am working on old text files with 2-digit years where the default century logic in dateutil.parser doesnt seem to work well. For example, the attack on Pearl Harbor was not on dparser.parse("12…

How can I check a Python unicode string to see that it *actually* is proper Unicode?

So I have this page:http://hub.iis.sinica.edu.tw/cytoHubba/Apparently its all kinds of messed up, as it gets decoded properly but when I try to save it in postgres I get:DatabaseError: invalid byte seq…

Test assertions for tuples with floats

I have a function that returns a tuple that, among others, contains a float value. Usually I use assertAlmostEquals to compare those, but this does not work with tuples. Also, the tuple contains other …