在 Python 中仅清除部分屏幕
Clear only part of the screen in 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')
如您所见,每次发生新查询时,我都必须打印顶部和底部。现在,这对于像这样的小型应用程序来说工作得很好,但如果我向它添加更多功能,我将不得不每次都重复相同的代码行(顶部和底部)。
我想知道是否有任何方法可以使顶部和底部保持静止,只允许程序清除中间的区域...?
不要调用os.system('clear')
,只需在要清除的屏幕行中写入space个字符即可。或者,只需写入新内容,用 spaces 填充到行尾以清除之前的任何内容。
我对 blessed 不熟悉,但考虑到以下问题:
I'll have to repeat the same line of code (top and bottom) every time.
如果您发现自己在重复相同的代码行,请使用函数。
把这个放在上面"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(" ======================================================================================= "))
并用
替换三个打印行的每个出现
drawTop()
所以基本上我正在尝试为 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')
如您所见,每次发生新查询时,我都必须打印顶部和底部。现在,这对于像这样的小型应用程序来说工作得很好,但如果我向它添加更多功能,我将不得不每次都重复相同的代码行(顶部和底部)。
我想知道是否有任何方法可以使顶部和底部保持静止,只允许程序清除中间的区域...?
不要调用os.system('clear')
,只需在要清除的屏幕行中写入space个字符即可。或者,只需写入新内容,用 spaces 填充到行尾以清除之前的任何内容。
我对 blessed 不熟悉,但考虑到以下问题:
I'll have to repeat the same line of code (top and bottom) every time.
如果您发现自己在重复相同的代码行,请使用函数。
把这个放在上面"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(" ======================================================================================= "))
并用
替换三个打印行的每个出现drawTop()