python怎么窗口显示文字_如何在Python/Curses子窗口中滚动文本?

#!/usr/bin/env python2.7

import curses

# content - array of lines (list)

mylines = ["Line {0} ".format(id)*3 for id in range(1,11)]

import pprint

pprint.pprint(mylines)

def main(stdscr):

hlines = begin_y = begin_x = 5 ; wcols = 10

# calculate total content size

padhlines = len(mylines)

padwcols = 0

for line in mylines:

if len(line) > padwcols: padwcols = len(line)

padhlines += 2 ; padwcols += 2 # allow border

stdscr.addstr("padhlines "+str(padhlines)+" padwcols "+str(padwcols)+"; ")

# both newpad and subpad are :

mypadn = curses.newpad(padhlines, padwcols)

mypads = stdscr.subpad(padhlines, padwcols, begin_y, begin_x+padwcols+4)

stdscr.addstr(str(type(mypadn))+" "+str(type(mypads)) + "\n")

mypadn.scrollok(1)

mypadn.idlok(1)

mypads.scrollok(1)

mypads.idlok(1)

mypadn.border(0) # first ...

mypads.border(0) # ... border

for line in mylines:

mypadn.addstr(padhlines-1,1, line)

mypadn.scroll(1)

mypads.addstr(padhlines-1,1, line)

mypads.scroll(1)

mypadn.border(0) # second ...

mypads.border(0) # ... border

# refresh parent first, to render the texts on top

#~ stdscr.refresh()

# refresh the pads next

mypadn.refresh(0,0, begin_y,begin_x, begin_y+hlines, begin_x+padwcols)

mypads.refresh()

mypads.touchwin()

mypadn.touchwin()

stdscr.touchwin() # no real effect here

#stdscr.refresh() # not here! overwrites newpad!

mypadn.getch()

# even THIS command erases newpad!

# (unless stdscr.refresh() previously):

stdscr.getch()

curses.wrapper(main)

当您运行这个程序时,开始时您会得到类似于(newpad左subpad(右)┌────────────────────────┐ ┌────────────────────────┐

│Line 1 Line 1 Line 1 ───│ │Line 1 Line 1 Line 1 ───│

│Line 2 Line 2 Line 2 │ │Line 2 Line 2 Line 2 │

│Line 3 Line 3 Line 3 │ │Line 3 Line 3 Line 3 │

│Line 4 Line 4 Line 4 │ │Line 4 Line 4 Line 4 │

│Line 5 Line 5 Line 5 │ │Line 5 Line 5 Line 5 │

│Line 6 Line 6 Line 6 │

│Line 7 Line 7 Line 7 │

│Line 8 Line 8 Line 8 │

│Line 9 Line 9 Line 9 │

│Line 10 Line 10 Line 10 │

└────────────────────────┘

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值