简体   繁体   English

用于基于curses的UI的滑块

[英]A slider for curses based UI

As a learning project, I'd like to set-out to make an ncurses-based UI for a program I had in mind, written in python. 作为一个学习项目,我想开始为我想到的程序制作一个基于ncurses的UI,用python编写。

After looking at urwid documentation, I cannot see anyway to create a simple slider (I need it to make a volume slider) that can be adjusted with the mouse. 在查看了urwid文档之后,我无法看到创建一个可以使用鼠标调整的简单滑块(我需要它来制作音量滑块)。

Am I missing something in urwid, or is there a more convenient curses module to make such a slider? 我在urwid中遗漏了什么,或者是否有更方便的curses模块来制作这样的滑块?

Curses is has very low level API - going back to the 1980's C'programing. Curses具有非常低级别的API - 可以追溯到1980年代的C'programing。

The Python wrappers have some higher level support for keyboard input and some other niceties, but they are few and apart and not nicely documented. Python包装器对键盘输入和其他一些细节有一些更高级别的支持,但它们很少并且分开并且没有很好地记录。

The Python niceties do not include Mouse support (ok, you get your mouse state back in a tuple instead of having to create a C structure for that, so it is somewhat better). Python细节不包括鼠标支持(好吧,你将鼠标状态恢复为元组而不必为此创建C结构,所以它稍微好一点)。

The idea is that one has to enable a curses Window enable "keypad" so that Python gives you full key codes enable a "mousemask" so that mouse events are sent to your app Detect the special "mouse_key" keyboard code in the getch function so that you can call "getmouse" to get the coordinates and button state. 我们的想法是,必须启用curses窗口启用“小键盘”,以便Python为您提供完整的密钥代码,启用“鼠标掩码”,以便将鼠标事件发送到您的应用程序检测getch函数中的特殊“mouse_key”键盘代码,以便您可以调用“getmouse”来获取坐标和按钮状态。

So there are no pre-made nice callbacks, you have to set-up the mainloop of your application to detect mouse events your self. 因此,没有预先制作好的回调,您必须设置应用程序的主循环以检测您自己的鼠标事件。

This sample code performs the above steps for reading the mouse events and printing the mouse state to the screen - it should be enough to get one started in building some usefull mouse handling with curses: 此示例代码执行上述步骤以读取鼠标事件并将鼠标状态打印到屏幕上 - 应该足以让人们开始使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM