2015-12-27 81 views
0

所以基本上我試圖爲python應用程序創建基於文本的用戶界面。這是我走到這一步:只清除Python中的部分屏幕

#!/usr/bin/env python 
# encoding: utf-8 

from blessed import Terminal 
import sqlite3 
import sys 
import os 

reload(sys) 
sys.setdefaultencoding("UTF-8") 

db = sqlite3.connect('/Users/JoaoPedro/Desktop/PyTest/Alunos') 
c = db.cursor() 

term = Terminal() 

os.system('clear') 

def main (self): 

print (term.yellow(" CICS 000009/1   Centro Educacional Charles Darwin     z\OS 3.2 ")) 
print (term.yellow(" TERMINAL: 2297    Sistema de Controle de Notas     VITÓRIA/ES ")) 
print (term.yellow(" ======================================================================================= ")) 
print (term.move_y(28)) + (term.yellow(" ======================================================================================= ")) 
matricula = raw_input (term.move(4, 7) + "Matrícula: ") 

os.system('clear') 

print (term.yellow(" CICS 000009/1   Centro Educacional Charles Darwin     z\OS 3.2 ")) 
print (term.yellow(" TERMINAL: 2297    Sistema de Controle de Notas     VITÓRIA/ES ")) 
print (term.yellow(" ======================================================================================= ")) 
print 
print (term.cyan("  Matrícula Nome        Série Turma  Nota ")) 

if matricula in ["/", ""]: 

    c.execute('select * from A ORDER BY nome') 
    rows = c.fetchall() 

else: 

    c.execute('select * from A WHERE matricula = ?', (matricula,)) 
    rows = c.fetchall() 

for row in rows: 
    print (term.white((term.move_x(9)) + row[0] + (term.move_x(20)) + row[1] + (term.move_x(56)) + row[2] + (term.move_x(64)) + row[3] + (term.move_x(73)) + row[4])) 
print (term.move_y(28)) + (term.yellow(" ======================================================================================= ")) 
command = raw_input (term.move(27, 2) + "Comando ===> ") 
if command == "X": 
    os.system('clear') 
    sys.exit() 
else: 
    os.system('clear') 
    main('self') 


main('self') 

正如你所看到的,我有打印的頂部,每一個新的查詢發生時的底部。現在,這對於像這樣的小應用程序來說工作得很好,但是如果我添加更多功能,每次都必須重複同一行代碼(頂部和底部)。

我想知道是否有任何方法來保持頂部和底部的靜態,只允許程序清除之間的區域......?

回答

1

我不熟悉的祝福,但鑑於問題:

我要重複的代碼(頂部和底部)每次都在同一行。

如果您發現自己重複相同的代碼行,請使用函數。

將這個上面的 「高清主(個體經營):」

def drawTop(): 
    print (term.yellow(" CICS 000009/1   Centro Educacional Charles Darwin     z\OS 3.2 ")) 
    print (term.yellow(" TERMINAL: 2297    Sistema de Controle de Notas     VITÓRIA/ES ")) 
    print (term.yellow(" ======================================================================================= ")) 

​​
2

不要調用os.system('clear'),只需在要清除的屏幕行中寫入空格字符。或者,只需編寫新內容,並在空白處填入行尾以清除之前存在的任何內容。

+0

哇更換三個打印線的每次出現!這其實很不錯,我從來沒有想過這個笑話 –