我试图运行这个:
from blessings import Terminal
t = Terminal()
print (t.bold('Hi there!'))
print (t.bold_red_on_bright_green('It hurts my eyes!'))
with t.location(0, t.height - 1):
print ('This is at the bottom.')
Run Code Online (Sandbox Code Playgroud)
这是第一个例子:https://pypi.python.org/pypi/blessings.
但是,我收到此错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\?????\AppData\Local\Programs\Python\Python35- 32\lib\site-packages\blessings\__init__.py", line 5, in <module>
import curses
File "C:\Users\?????\AppData\Local\Programs\Python\Python35-32\lib\curses\__init__.py", line 13, in <module>
from _curses import *
ImportError: No module named '_curses'
Run Code Online (Sandbox Code Playgroud)
我的系统是win10 x64.
在Python curses模块中,我观察到按下esc键和getch()返回之间有大约1秒的延迟.其他键似乎没有发生此延迟.为什么会发生这种情况,我该怎么办呢?
测试用例:
import curses
import time
def get_delay(window, key):
while True:
start = time.time()
ch = window.getch()
end = time.time()
if ch == key:
return end-start
def main(stdscr):
stdscr.clear()
stdscr.nodelay(1)
stdscr.addstr("Press ESC")
esc_delay = get_delay(stdscr, 27)
stdscr.addstr("\nPress SPACE")
space_delay = get_delay(stdscr, ord(' '))
return esc_delay, space_delay
if __name__ == '__main__':
esc_delay, space_delay = curses.wrapper(main)
print("Escape delay: {} ms".format(esc_delay*1000))
print("Space delay: {} ms".format(space_delay*1000))
Run Code Online (Sandbox Code Playgroud)
结果:
Escape delay: 1001.09195709 ms
Space delay: 0.00596046447754 ms
Run Code Online (Sandbox Code Playgroud) 我有以下代码,允许您向上和向下滚动文本垫.每次滚动(即处理用户输入)时,打击垫都会按预期更新.但是,在按下第一个键之前没有显示任何内容,尽管我正在调用pad.refresh(),就像我在每个用户输入后一样.
我的代码看起来像这样:
def main(self,stdscr):
x,y = 20,150 # size of the window
u,a = 10,20 # where to place window - up,across
pad = curses.newpad(20,150) # nlines, ncols
pad_pos = 0
exit = False
pad.addstr(0,0,str(self.all_results))
while not exit:
pad.addstr(0,0,str(self.format_results()))
++ stdscr.refresh()
pad.refresh(pad_pos,10, u,a, x,y)
-- cmd = stdscr.getch()
++ cmd = pad.getch()
stdscr.nodelay(1)
+ pad.getch() - caused the screen not to update
+ stdscr.refresh() - no change
if cmd != -1:
+ pad.getch() - - caused the screen not to …Run Code Online (Sandbox Code Playgroud) 我开始学习Python中的curses.我在Mac OS X上使用Python 3.5.当我尝试在右下角写入时,程序崩溃并出现以下错误:
$ python ex_curses.py
[...]
File "ex_curses.py", line 19, in do_curses
screen.addch(mlines, mcols, 'c')
_curses.error: add_wch() returned ERR
Run Code Online (Sandbox Code Playgroud)
示例程序是:
import curses
def do_curses(screen):
curses.noecho()
curses.curs_set(0)
screen.keypad(1)
(line, col) = 12, 0
screen.addstr(line, col, "Hello world!")
line += 1
screen.addstr(line, col, "Hello world!", curses.A_REVERSE)
screen.addch(0, 0, "c")
(mlines, mcols) = screen.getmaxyx()
mlines -= 1
mcols -= 1
screen.addch(mlines, mcols, 'c')
while True:
event = screen.getch()
if event == ord("q"):
break
curses.endwin()
if __name__ == "__main__":
curses.wrapper(do_curses)
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,我错过了一些明显的东西,但我不知道是什么.
我正在尝试创建一个小的Python/curses应用程序.
但据我可以看到有没有办法知道是否 CTRL+ J或Enter已被按下.现在这可能是因为它们都具有相同的ascii代码(10):
http://en.wikipedia.org/wiki/Control_character#In_ASCII
但VIM如何区分这两者呢?
我有一个bash脚本打印一个漂亮的大彩色表,使用转义代码生成前景和背景tput.我的curses应用程序需要调用此bash脚本并将输出放在屏幕上.
当我尝试这样做时,curses会以堆栈跟踪结束爆炸:
File "./dostuff.py", line 38, in print_art
screen.addstr(y, x_start, line)
TypeError: must be str, not bytes
Run Code Online (Sandbox Code Playgroud)
其中"线"是这样的:
'\x1b[44m\x1b[30mcard major minor revision runs updated\x1b(B\x1b[m\x1b(B\x1b[m\n'
有没有办法让诅咒解释这些颜色代码?我可以使用颜色代码对字符串进行任何处理以使curses显示它吗?或者我必须基本上从bash脚本中删除颜色,然后重新实现python中的着色?
编辑:
获取bash输出的命令类似于:
print_art(subprocess.Popen(["./automount", "backup", "list"], stdout=subprocess.PIPE).communicate()[0])
Run Code Online (Sandbox Code Playgroud)
通过调用decode()字节字符串,我可以使用curses来打印字符串,尽管有文字转义序列.除非我听到其他人的意见,否则我只是手动解析这些文字转义序列并转换为使用curses颜色方法.
我在Raspberry Pi 3上运行了一个python 2.7脚本.
class UIThread(threading.Thread):
def __init__(self, threadID, name, counter, U):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
self.U = U
def run(self):
self.U.run()
def main():
time.sleep(3)
try:
try:
###launch a UI running as background thread#####
U = UIlib.UI()
thread1 = UIThread(1, "UI", 1, U)
thread1.daemon = True
thread1.start()
except:
###if there is no monitor, lanch a fake UI class#######
U = UIlib.nomonitorUI()
thread1 = UIThread(1, "NMUI", 1, U)
thread1.daemon = True
thread1.start()
print "No Monitor …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用cursestextpad.Textbox()函数进行文本输入。到目前为止,一切工作正常,但是,某些键无法识别,包括部分符号(\xc2\xa7)和所有德语变音符号(\xc3\xa4/\xc3\xb6/\xc3\xbc)。我想这与文本编码有某种关系,但我不知道如何解决这个问题。我的德语键盘布局与input().
这是一些最小的例子:
\n\n import curses\n import curses.textpad as textpad\n\n try:\n stdtscr = curses.initscr()\n curses.cbreak()\n stdtscr.keypad(1)\n curses.noecho()\n\n textpad.Textbox(stdtscr).edit()\n\n finally:\n curses.nocbreak()\n stdtscr.keypad(0)\n curses.echo()\n curses.endwin()\nRun Code Online (Sandbox Code Playgroud)\n 我在使用 Curses 模块检测 Backspace 键时遇到了困难。每当我按退格键时,返回的字符/字符串是“^?”,但是我无法通过以下方式检测到它:
如果 str(key) == '^?':
下面的代码设置为运行
import curses
def main(win):
win.nodelay(True)
key = ''
record = ''
win.clear()
win.addstr("Key:")
win.addstr('\n\nRecord:')
win.addstr(record)
while True:
try:
key = win.getkey()
if str(key) == '^?':
# Attempt at detecting the Backspace key
record = record[:-1]
elif str(key) == '\n':
# Attempt at detecting the Enter Key
record = ''
else:
record += str(key)
win.clear()
win.addstr("Key:")
win.addstr(str(key))
win.addstr('\n\nRecord:')
win.addstr(record)
if key == os.linesep:
break
except Exception as e:
# No input …Run Code Online (Sandbox Code Playgroud) 我正在Python中运行一个多处理系统,我计划使用curses将终端窗口分为4个象限,并在其中一个象限中显示每个进程的输出。
所以,最终的输出应该类似于:
--------------------------------
| | |
| PROCESS01 | PROCESS02 |
| | |
---------------------------------
| | |
| PROCESS03 | PROCESS04 |
| | |
---------------------------------
Run Code Online (Sandbox Code Playgroud)
到目前为止,我尝试将窗口分成 4 个部分,如下所示:
--------------------------------
| | |
| PROCESS01 | PROCESS02 |
| | |
---------------------------------
| | |
| PROCESS03 | PROCESS04 |
| | |
---------------------------------
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
File "screen_show.py", line 78, in <module> main()
File "screen_show.py", line 46, in main
pad12.refresh(0,cols_mid, 0,cols_mid, rows_mid,cols_tot-1)
_curses.error: prefresh() returned ERR
Run Code Online (Sandbox Code Playgroud) python ×10
python-curses ×10
curses ×5
ncurses ×3
python-3.x ×2
bash ×1
encoding ×1
keyboard ×1
module ×1
python-2.7 ×1
uart ×1
vim ×1
windows ×1