prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.28k stars 715 forks source link

Hide cursor for application layout using only buttons #827

Open ulno opened 5 years ago

ulno commented 5 years ago

I am building a small TUI application with python prompt toolkit, it is not using a prompt. As there is nothing to input, I would like to hide the cursor. There are just a couple of buttons in a full-screen text application.

I tried to use application.output.hide_cursor() before application.run(), however that seems to be ignored. As I have no input, I can also not wrap the whole thing into a Window-object and set its always_hide_cursor property to True nor access the underlying Screen objects which seem to have an effect if the cursor get switched on in rendering or not.

Is there any way I can accomplish to disable the cursor in my case?

jonathanslenders commented 5 years ago

Hi @ulno,

Calling hide_cursor() doesn't have any effect, because show_cursor() is probably called again at the end of the next render/paint event.

The visibility of the cursor is determined by the visibility of the currently focused user control. Every UIControl has a method create_content() which returns a UIContent object. The cursor becomes visible if the show_cursor attribute of this UIContent is true for the current control. Right now, most built-in widgets probably don't allow you to hide the cursor, without overriding private methods.

But if you are sure you always want to hide the cursor, you could replace the Output.show_cursor() method:

output.show_cursor = lambda:None

Hope this helps, Jonathan

ulno commented 5 years ago

That does work! Thanks @jonathanslenders.

Should that be at least documented somewhere?

Unfortunately, I was developing this terminal-app for an educational project running on the Raspberry Pi. It turns out that python prompt toolkit is totally unusable on the Raspberry Pi - 2s to react to keyboard strokes moving from button to button (it might be something with ncurses and effects ipython, xonsh, and even asciimatic too), but I will open another issue for that. This is further explained in #830.