0

基本的に、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')

ご覧のとおり、新しいクエリが発生するたびに上部と下部を印刷する必要があります。これは、このような小さなアプリケーションでは問題なく動作しますが、関数をさらに追加すると、毎回同じコード行 (上部と下部) を繰り返さなければならなくなります。

上部と下部を静的に保ち、プログラムがその間の領域のみをクリアできるようにする方法があるかどうか疑問に思っていました...?

4

2 に答える 2

2

を呼び出さないでくださいos.system('clear')。クリアしたい画面の行にスペース文字を書き込んでください。または、新しいコンテンツを書き、スペースを行末まで埋めて、以前にあったものをすべてクリアします。

于 2015-12-27T08:39:50.163 に答える