2013-03-28 26 views
2

我算在Python線這樣的文字錯誤的結果:計數線條給人滾動文本框

count = 0 
for ch in text: 
    if(ch == "\n"): 
     count += 1 
return count 

同樣的文本進入一個TextBox控件。 我想滾動到最後一行,但行數並沒有幫助,因爲textBox將長行包裹成2行或更多行。
我可以通過滾動到-1去最後一行,但後來我無法再向上滾動。我需要知道(實際)最大線數,所以我可以滾動到我想要的任何位置。

lastLine = self.count_lines(text) 
self.getControl(100).setText(text) 
self.getControl(100).scroll(lastLine) 
+4

要計算在給定的Python字符串的換行符'LEN(text.splitlines()'會更容易些。甚至'text.count( '\ n')'。 –

+0

@MartijnPieters但它會給我計算一個文本框中的實際行數,包括包裹的行數? – ilomambo

+0

@MartijnPieters - 你的兩種方法會給出不同的結果 – eumiro

回答

1

你說長長的一行分成兩行或更多,對嗎?

,你可以計算行這樣

count=0 
for a in string.split(): 
    count+= 1+a//MaxLen 

其中字符串是你正在處理的文本,MAXLEN是文本框可以在一條線上

但這恰恰顯示的最多字符數沒有解決問題,如果你不知道如何得到MaxLen,我其實不...

+1

感謝Alberto。看來這個問題是知道的,而且目前還不能解決。 XBMC團隊沒有完全預見到TextBox控件的使用,因爲XBMC首先面向媒體。 – ilomambo

+0

嘗試+1。謝謝。 – Geoff

0

有點晚了,但只是在別人可以使用這個,這是我的解決方案。此示例將動態調整curses文本板窗口小部件的高度。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

# Purpose: Test Scrolling 
# 
# File test.py 
# Author: Dan Huckson 
# Date: 20150922 
# 

import curses 

def main(stdscr): 
    vpwidth = 1 
    vpheight = 30 
    string = "A\n BB\n CCC\n DDDD\n EEEEE\n  FFFFFF\n  GGGGGGG\n  HHHHHHHH\n  JJJJJJJJJ\n   KKKKKKKKKK\n   LLLLLLLLLLL\n   MMMMMMMMMMMM\n   NNNNNNNNNNNNN\n    OOOOOOOOOOOOOO END" 

    height = w = 0 
    stdscr.refresh() 
    for c in string: 
     if c == '\n' and not w: height += 1 
     else: 
      if c != '\n': 
       w += 1 
       if w < vpwidth: continue 
      elif w == vpwidth: height += 1 
      w = 0  
      height += 1 
    if w: height += 1 

    pad = curses.newpad(height+1, vpwidth) 
    pad.addstr(string) 

    top = 0 
    left = 3 
    offset = key = 0 
    while True: 
     if key == curses.KEY_UP and offset: offset -= 1 
     elif key == curses.KEY_DOWN and (offset + vpheight) < height: 
      offset += 1 

     pad.refresh(offset, 0, top, left, vpheight+top, vpwidth+left) 

     for i in range(vpheight): 
      stdscr.addstr(i,0, (' %s:' % i)[-3:]) 

     stdscr.addstr(vpheight,0, ' ----~----+----~----+----~----+----~----+----~----+----~----+----~----+----~----') 
     stdscr.addstr(vpheight+1,0, '   10  20  30  40  50  60  70  ') 
     stdscr.addstr(vpheight+2,0, '                    ') 
     stdscr.addstr(vpheight+4,0, '%s %s %s %s %s %s %s %s ' % (offset, 0, 0, 0, vpheight, vpwidth, height, len(string))) 

     key = stdscr.getch() 


curses.wrapper(main)