2013-01-21 18 views
7

से बाहर निकलने के बाद टर्मिनल स्क्रॉलबैक इतिहास में श्राप प्रोग्राम आउटपुट बना रहता है मैं शाप के लिए काफी नया हूं, इसलिए मैं अजगर में कुछ अलग-अलग चीजों को आजमाने की कोशिश कर रहा हूं।प्रोग्राम

मैंने खिड़की की शुरुआत की है और विंडो ऑब्जेक्ट के लिए स्क्रोलोक सेट किया है। मैं तार जोड़ सकता हूं, और स्क्रॉलिंग काम करता है ताकि addstr() में विंडो के अंत में कोई त्रुटि न हो।

प्रोग्राम समाप्त होने के बाद मेरे टर्मिनल प्रोग्राम (टीएमयूक्स या केडीई कंसोल, इस मामले में) प्रोग्राम आउटपुट में वापस स्क्रॉल करने की क्षमता है जो मैं चाहता हूं।

मेरे कोड में, यदि मैं एंडविन() कॉल को छोड़ देता हूं तो मैं कम से कम आउटपुट देख सकता हूं, लेकिन टर्मिनल को रीसेट को परिचालन पर वापस जाने के लिए कॉल करने की आवश्यकता है।

साथ ही, जब भी प्रोग्राम चल रहा है, तब भी कर्सर विंडो स्क्रॉल हो जाने के बाद, मैं प्रारंभिक आउटपुट देखने के लिए कंसोल में वापस स्क्रॉल नहीं कर सकता।

#!/usr/bin/env python2 
import curses 
import time 
win = curses.initscr() 
win.scrollok(True) 
(h,w)=win.getmaxyx() 
h = h + 10 
while h > 0: 
    win.addstr("[h=%d] This is a sample string. After 1 second, it will be lost\n" % h) 
    h = h - 1 
    win.refresh() 
    time.sleep(0.05) 
time.sleep(1.0) 
curses.endwin() 

उत्तर

7

इस कार्य के लिए, मैं सुझाव है कि आप एक पैड (http://docs.python.org/2/library/curses.html#curses.newpad) का उपयोग करें:

सिवाय इसके कि यह स्क्रीन आकार द्वारा ही सीमित नहीं है एक पैड, एक खिड़की की तरह है, और नहीं है जरूरी स्क्रीन के एक विशेष भाग से जुड़ा हुआ है। [...] खिड़की का केवल एक हिस्सा स्क्रीन पर एक बार होगा। [...]

आदेश कंसोल पर पैड की सामग्री को छोड़ने के लिए में जाने के बाद शाप का उपयोग कर समाप्त कर दिया है, मैं सामग्री पीठ में पैड से, अंत शाप पढ़ सकते हैं और मानक के लिए सामग्री लिखते थे उत्पादन।

निम्न कोड जो आप वर्णन करते हैं उसे प्राप्त करता है।

#!/usr/bin/env python2 

import curses 
import time 

# Create curses screen 
scr = curses.initscr() 
scr.keypad(True) 
scr.refresh() 
curses.noecho() 

# Get screen width/height 
height,width = scr.getmaxyx() 

# Create a curses pad (pad size is height + 10) 
mypad_height = height + 10 
mypad = curses.newpad(mypad_height, width); 
mypad.scrollok(True) 
mypad_pos = 0 
mypad_refresh = lambda: mypad.refresh(mypad_pos, 0, 0, 0, height-1, width) 
mypad_refresh() 

# Fill the window with text (note that 5 lines are lost forever) 
for i in range(0, height + 15): 
    mypad.addstr("{0} This is a sample string...\n".format(i)) 
    if i > height: mypad_pos = min(i - height, mypad_height - height) 
    mypad_refresh() 
    time.sleep(0.05) 

# Wait for user to scroll or quit 
running = True 
while running: 
    ch = scr.getch() 
    if ch == curses.KEY_DOWN and mypad_pos < mypad_height - height: 
     mypad_pos += 1 
     mypad_refresh() 
    elif ch == curses.KEY_UP and mypad_pos > 0: 
     mypad_pos -= 1 
     mypad_refresh() 
    elif ch < 256 and chr(ch) == 'q': 
     running = False 

# Store the current contents of pad 
mypad_contents = [] 
for i in range(0, mypad_height): 
    mypad_contents.append(mypad.instr(i, 0)) 

# End curses 
curses.endwin() 

# Write the old contents of pad to console 
print '\n'.join(mypad_contents)