免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4065 | 回复: 6
打印 上一主题 下一主题

curses 模块如何删除指定的行 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-17 16:19 |只看该作者 |倒序浏览
本帖最后由 ccne 于 2012-02-17 16:25 编辑

curses 模块如何删除指定的行,我用多线覆盖去显示的,但因为有的长度不一样 所以会有显示重叠问题。不知道curses有没有办法删除或清空指定的某一行。谢谢!


论坛徽章:
0
2 [报告]
发表于 2012-02-17 16:59 |只看该作者
2.7下还真有个curses,又孤陋寡闻了

论坛徽章:
0
3 [报告]
发表于 2012-02-17 17:22 |只看该作者
呃!木有人搞过吗?

论坛徽章:
0
4 [报告]
发表于 2012-02-17 20:47 |只看该作者
本帖最后由 askandstudy 于 2012-02-17 20:48 编辑

我用投机取巧的方法没有发现有重叠问题,我在要写到屏幕上的字符串后面加了几个空格。
info='thread [%02d] is downloading... [%02d%%]   ' % (thno,curprogress)

调用stdscr.addstr()时会用新字符串去覆盖的啊

不知道你用的是哪个curses,我在fc16上试的。

python库参考手册有curses模块对象及方法的介绍说明。

论坛徽章:
0
5 [报告]
发表于 2012-02-20 10:32 |只看该作者
空格是可以,不过窗口必须要最大最可以 小一点就乱了。我现在是把clear()的 只是有新任务加进来的时候会闪一下!~

论坛徽章:
0
6 [报告]
发表于 2012-02-20 11:17 |只看该作者
本帖最后由 askandstudy 于 2012-02-20 12:24 编辑

程序在运行中不能把窗口调得太小,不然会报段错误的,然后就乱了。也试了用删除行的方法,但是效果不怎么好,后面一行下面开始有时会乱掉,调一下窗口就恢复了,可能是我没处理好。
  1. #!/usr/bin/python
  2. #coding:utf-8

  3. import curses, sys
  4. import time,random
  5. import threading

  6. def workthread(stdscr,thno):
  7.     speed=random.randint(100,200)/100.0
  8.     testnumber=2
  9.     for i in range(testnumber):
  10.         curprogress=0
  11.         while (curprogress+speed*5)<100:
  12.             curprogress += speed*5
  13.             info='thread [%02d] is downloading... [%02d%%]     ' % (thno,curprogress)
  14.             updateprogress(stdscr,thno,info)
  15.             time.sleep(0.5)
  16.         curprogress=100
  17.         info='thread [%02d] is downloading... [%02d%%]     ' % (thno,curprogress)
  18.         updateprogress(stdscr,thno,info)

  19. def updateprogress(stdscr,line,info):
  20.     stdscr.move(line,0)
  21.     stdscr.addstr(line,0,info)
  22.     stdscr.refresh()
  23.     return 0

  24. def main(stdscr):
  25.     stdscr.clear()
  26.     stdscr.refresh()

  27.     #start thread
  28.     threads={}
  29.     threadnumbers=5
  30.     for i in range(threadnumbers):
  31.         th=threading.Thread(target=workthread,name='thread%d' % i,args=(stdscr,i))
  32.         th.start()
  33.         thid=str(th.ident)
  34.         threads[thid]=th

  35.     while 1:
  36.         thidnume=[str(x.ident) for x in threading.enumerate()]
  37.         for key,value in threads.items():
  38.             if key not in thidnume:
  39.                 threads.pop(key)
  40.         if len(threads)<=0:
  41.             break
  42.         time.sleep(1)

  43.     return 0

  44. curses.wrapper(main)
复制代码
感觉用后面加空格来覆盖前面的输出的方法也还行啊。
要做得好一些似乎还是需要花些功夫的,线程结束就删除改行等功能似乎还是需要用到删除行的处理。

论坛徽章:
0
7 [报告]
发表于 2012-02-21 16:03 |只看该作者
不清楚除了curses 还有木有别的方法 来实现这样的功能!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP