我刚刚偶然发现了curses.ascii.islower().它检查传递的字符是否为小写.与str.islower()相比,使用此函数有什么好处?是的,它需要将ASCII字符转换为字符串对象,这会增加开销.但除此之外还有什么优势?
我发现的一个缺点是需要额外的库,可能有也可能没有.
我在ncurses安装Python3. 当我执行正常操作时sudo apt-get install ncurses-dev,它似乎已安装,Python2但当我尝试运行我的脚本时Python3,它说。
ImportError: No module named curses
Run Code Online (Sandbox Code Playgroud)
你会怎样去ncurses工作Python3?
我正在查看一本书中的一些源代码,并注意到某些代码似乎不在当前的 Python2.7 API 中。curses根据此代码,该模块应该有一个名为 的常量变量LINES和另一个名为COLS. 我打开一个Python交互式终端,看到没有COLS或LINES变量或方法。
我的问题是:这段代码是如何工作的?
def draw_loglines(self):
self.screen.clear()
status_col = 4
bytes_col = 6
remote_host_col = 20
status_start = 0
bytes_start = 4
remote_host_start = 10
line_start = 26
logline_cols = curses.COLS - status_col - bytes_col - remote_host_col - 1
for i in range(curses.LINES):
c = self.curr_topline
try:
curr_line = self.loglines[c]
except IndexError:
break
self.screen.addstr(i, status_start, str(curr_line[2]))
self.screen.addstr(i, bytes_start, str(curr_line[3]))
self.screen.addstr(i, remote_host_start, str(curr_line[1]))
#self.screen.addstr(i, line_start, str(curr_line[4])[logline_cols])
self.screen.addstr(i, line_start, …Run Code Online (Sandbox Code Playgroud) 如何滚动诅咒?我尝试了以下方法,但失败了:
import curses
def main(stdscr):
stdscr.clear()
# Display 10 numbered lines:
for line in range(10):
stdscr.addstr(line, 0, str(line))
stdscr.getch() # Wait for a key press
# Scrolling:
stdscr.setscrreg(0, 9) # Set scrolling region
for _ in range(5):
stdscr.scroll() # Fails!
stdscr.getch()
curses.wrapper(main)
Run Code Online (Sandbox Code Playgroud)
该错误没有提供太多信息:
stdscr.scroll()
_curses.error: scroll() returned ERR
Run Code Online (Sandbox Code Playgroud)
我在 OS X 和 xterm(也是 OS X)中尝试了终端应用程序,但两种情况下的错误都是一样的。
我尝试编译和安装带有 curses 支持的 Python 的尝试失败了,我已经在编译标志和诸如此类的东西上尝试了各种迭代,但似乎无法让这个东西正常工作。
这是在 Solaris 11,Python 版本 3.4.3 上:
首先,这是问题所在:
Python 3.4.3 (default, Mar 3 2015, 14:43:41)
[GCC 4.5.2] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import curses
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/pkg/python/3.4.3/lib/python3.4/curses/__init__.py", line 13, in <module>
from _curses import *
ImportError: No module named '_curses'
Run Code Online (Sandbox Code Playgroud)
各种谷歌告诉我,这是因为在编译 Python 时,没有找到 ncurses 库。
所以我将curses从ftp://ftp.gnu.org/gnu/ncurses/(5.9)安装到/usr/local/pkg/ncurses/5.9并尝试重新编译Python,传入ncurses的位置:
export LDFLAGS=-L/usr/local/pkg/ncurses/5.9/lib
export CPPFLAGS="-I/usr/local/pkg/ncurses/5.9/include -I/usr/local/pkg/ncurses/5.9/include/ncurses"
./configure --enable-shared --prefix=/usr/local/pkg/python/3.4.3
make
make …Run Code Online (Sandbox Code Playgroud) 我正在用 Python 编写一个 curses 程序。我是诅咒的初学者,但我已经使用终端控制序列进行彩色输出。
现在有一些代码片段要打印在窗口内,我希望它们被语法突出显示,最好使用像 pygments 这样的库来完成,它输出带有控制序列的突出显示代码。
最初我将 pygments 输出直接提供给window.addstr(),但结果是控制序列被转义并且整个突出显示的字符串打印在屏幕上(就像这样:https : //too-young.me/web/repos/curses-突出显示.png)。我怎样才能直接用诅咒来显示它,就像cat?
当我按住一个键时,将键入第一个符号,然后稍有延迟,然后快速键入其他符号。像这样:
在终端中也是如此。即使那里的延迟较小,在Linux控制台(tty)中也会发生同样的情况。
我正在使用的Python中的控制台应用程序进行工作curses,它可以处理箭头键的按动操作,并且也存在这种延迟。
我想摆脱这种延迟,这样当我按住一个键时,它将均匀地发送信号,并且在第一个(或任意一个)符号之后没有任何特定的延迟。
我该怎么做?我应该使用阿森纳的东西curses吗?还是修改某些系统范围的设置?
EDIT1:我认为我找到了一种方法。我可以转到键盘设置并设置自动重复的延迟。但是它会全局更改它,并且仅在我的图形界面上更改。它在Linux控制台中没有任何改变。因此,我也在寻找一种在控制台中执行此操作的方法,因此它只会影响我的应用程序,而不会影响整个系统。
EDIT2:在X http://linuxforcynics.com/how-to/set-keyboard-repeat-delay-and-rate中找到了一种在全球范围内进行命令行处理的方法
以及用于Linux控制台的方法:https : //unix.stackexchange.com/questions/58651/adjusting-keyboard-sensitivity-in-a-command-line-terminal
但仍在寻找仅限应用的方式。
我想弄清楚在这个函数顶部的类型注释中应该放什么。
我有以下简单的例子:
import curses
def main(stdscr):
stdscr.clear()
stdscr.addstr(2, 0, "What is the type of stdscr?")
stdscr.addstr(5, 0, "It is: {}".format(type(stdscr)))
stdscr.refresh()
stdscr.getkey()
curses.wrapper(main)
Run Code Online (Sandbox Code Playgroud)
这返回<type '_curses.curses window'>. 这似乎不适用于类型提示,因为它有一个空格。 预期结果将编辑:此处的文档不正确。WindowObject列在文档中。我在curses 模块本身中找不到WindowObject 的路径。
如何使用准确的类型注释编写 main ?
我正在使用Python curses,并尝试使用curses.init_color()初始化新颜色。即使在初始化新的RGB值并将其分配给一对之后,更改也不会生效。
我的终端支持颜色更改,因为curses.can_change_color()返回True。我还检查了使用curses.color_content()更改的颜色索引-它返回了我在init_color()中指定的RGB值。我也一开始就调用过curses.start_color()。
我想念什么吗?我该怎么做才能使init_color()发生更改?代码如下:
curses.start_color()
curses.init_color(17, 200,200,200)
curses.init_pair(1, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(4, 17, curses.COLOR_BLACK)
print curses.can_change_color() #returns True
print curses.color_content(17) #(200,200,200),
stdscr.addstr("test",curses.color_pair(4)) #yet this text is blue
Run Code Online (Sandbox Code Playgroud)
我想念什么吗?
当我将某些表情符号(特别是标志)输出到 Curses 的子窗口中时,它会使输出变形,即使是在该子窗口之外的输出也是如此。
跑步:
import curses
def draw_screen(stdscr):
event = 0
stdscr.clear()
stdscr.refresh()
while (event != ord('q')):
emojis = ["", "", ""]
# emojis = ["", "", ""]
for i, emoji in enumerate(emojis):
box1 = stdscr.subwin(11, 11, 0, i*12)
box1.box()
box1.addstr(0, 4, emoji)
event = stdscr.getch()
if __name__ == "__main__":
curses.wrapper(draw_screen)
Run Code Online (Sandbox Code Playgroud)
产生:
如果你只为心切换表情符号,它工作正常:
我知道国旗表情符号是一系列区域指示符,但我认为这应该可行,但我不确定如何修复它。
我已经在 Mac OS 10.13 和 10.14 上的 iTerm 和终端中对此进行了测试。
(我还注意到其他一些多点表情符号(?)在原始 Python 中打印得很好,但在诅咒中被分成两个单独的表情符号来组成它们。我不确定这是否相关。)
python-curses ×10
python ×8
ncurses ×4
curses ×3
python-3.x ×2
input ×1
python-2.7 ×1
python-3.5 ×1
python-3.6 ×1
scroll ×1
solaris ×1
type-hinting ×1
ubuntu-14.04 ×1
unicode ×1