Search code examples
pythontui

Clear only part of the screen in Python


So basically I'm trying to create a text based user interface for a python application. This is what I got so far:

#!/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')

As you can see, I have to print the top and the bottom part every time a new query happens. Now, this works just fine for a small application like this, but if I add more functions to it, I'll have to repeat the same line of code (top and bottom) every time.

I was wondering if it there is any way to keep the top and bottom static and only allow the program to clear the areas in between...?


Solution

  • I am not familiar with blessed but given the problem of:

    I'll have to repeat the same line of code (top and bottom) every time.

    If you find yourself repeating the same lines of code, use a function.

    Put this above "def main (self):"

    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(" ======================================================================================= "))
    

    and replace each occurence of the three print lines with

    drawTop()