Nea*_*eal 8 python tui ncurses
这是我发布堆栈溢出的第一篇文章.多年来我一直潜伏在这个网站上获取信息,它总是有用的,所以我想我会发布我的第一个问题.
我一直在寻找一些类似的例子,但似乎找不到任何东西.
最后,我正在尝试编写一个简单的文本ui,用于在文本提取程序中查找误报和漏报.误报模块是一个简单的是/否选择,显示彩色文本和使用getch()很容易.然而,假阴性部分变得困难.
所以这就是我想要做的:
有什么想法甚至开始吗?我正在尝试简单的事情,比如在屏幕上保留文字并移动光标,但无济于事.
我知道curses.textpad.TextBox()模块,但它执行编辑,如插入和删除,我不想这样做.也许有一种方法可以禁用它.
我还有其他问题,但我现在还会详细说明.
谢谢!!
尼尔
编辑:更具体地说,我不是在寻找编写整个程序的帮助,只是帮助将光标移动到显示的文本上,突出显示它,然后选择它并在变量中保存它.
Nea*_*eal 13
我想更新这个问题,以防其他人在网上搜索这个问题并偶然发现这个问题.
好的,所以答案实际上非常简单,需要阅读python curses文档中列出的所有函数.
我做的是制作3状态机:状态1:正常模式(仅显示文本),状态2:高亮模式,允许光标在窗口周围移动,以及状态3:荧光笔模式,仅从左侧给出有限的移动直到文本并在文本移动时突出显示文本.
要完成此任务,只需要一些基本的curses函数调用.
我制作了单独的窗口,但我只假设一个窗口进行解释.
在窗口中显示文字,坚持:
window.addstr()
window.refresh()
Run Code Online (Sandbox Code Playgroud)
用于移动光标:
#get current cursor position
curr_y, curr_x = window.getyx()
# depending on direction, update the cursor with
next_y, next_x = get_next_direction()
# place cursor in new position
window.move(next_y, next_x)
window.refresh()
Run Code Online (Sandbox Code Playgroud)
一旦光标超过突出显示的起始点,按'v'进入荧光笔状态,并限制光标的移动,更改所选文本的属性:
# get current cursor position
curr_y, curr_x = window.getyx()
# save position for later use
start_y = curr_y; start_x = curr_x
# loop until 'v' is pressed again
while highlight_state:
# change the attribute of the current character, for 1 character only, to reverse
window.chgat(curr_y,curr_x, 1, curses.A_REVERSE)
curr_y, curr_x = get_next_direction()
# save end state, this is buggy obviously, but you get the point
end_y = curr_y; end_x = curr_X
Run Code Online (Sandbox Code Playgroud)
现在从头到尾提取该信息
# get integer representation of char at positiong
outstr = ''
#from start to end
char_as_int = windows.inch(y,x)
char = char_as_int & 0000FF
attr = char_as_int & FFFF00 #not useful here, but maybe later
outstr += char
Run Code Online (Sandbox Code Playgroud)
而已!我还尝试了另一种方法来保存突出显示的材料,它基本上将x,y坐标转换为正在显示的字符串的索引,但是允许以字符串表示形式(换行符,制表符等)发出,加上更难做到.
如果有人对更有效/更清洁的方法有意见,请回复!
| 归档时间: |
|
| 查看次数: |
6457 次 |
| 最近记录: |