Slider for curses user interfaces

As a training project, I would like to set up to create an ncurses-based user interface for a program that I had in mind written in python.

After looking at the urwid documentation, I still canโ€™t create a simple slider (I need it to create a volume slider), which can be configured with the mouse.

Am I missing something in urwid, or is there a more convenient curse module to make such a slider?

+6
source share
1 answer

Curses has a very low level API - back to 1980's C'programing.

Python shells have higher-level support for keyboard input and some other subtleties, but they are few in number and separate and not very well documented.

The intricacies of Python do not include mouse support (well, you return your mouse state to the tuple, instead of creating a C structure for it, so it's a little better).

The idea is to turn on the curses window, turn on the "keyboard" so that Python gives you full key codes, turn on "mousemask" so that mouse events are sent to your application. Detection of the special mouse_key keyboard code in the getch function so that you can call getmouse to get the coordinates and state of the button.

Thus, there are no ready-made good callbacks, you must configure the mainloop of your application to detect mouse events yourself.

This code example follows the steps above to read mouse events and print the state of the mouse on the screen โ€” this should be enough to get started creating useful mouse processing using curses:

# -*- coding: utf-8 -*- import curses screen = curses.initscr() curses.noecho() curses.mousemask(curses.ALL_MOUSE_EVENTS) screen.keypad(1) char = "" try: while True: char = screen.getch() screen.addstr( str(char) + " ") if char == curses.KEY_MOUSE: screen.addstr (" |" + str(curses.getmouse()) + "| ") finally: screen.keypad(0) curses.endwin() curses.echo() 
+1
source

Source: https://habr.com/ru/post/902267/


All Articles