User Interface Suggestions? (newbie)
Akira Li
4kir4.1i at gmail.com
Wed Oct 5 18:12:50 EDT 2016
Beverly Howard <bev at bevhoward.com> writes:
>...snip...
> A primary question would be, "What are options for building a display
> that would update displayed values without scrolling?"
To rewrite only the last character, you could use '\b':
import os
import itertools
import time
for c in map(str.encode, itertools.cycle('\-/|')):
os.write(1, b'\b'+c)
time.sleep(.3)
To rewrite only the last line, you could use '\r':
for i in range(1, 101):
time.sleep(.1)
print('\r{:4.0%}'.format(i/100), flush=True, end='')
To control the full screen in the terminal e.g., to change the position,
you could use *blessings* module [1]:
from blessings import Terminal # $ pip install blessings
line = 'Line in the middle of the terminal.'
term = Terminal()
with term.hidden_cursor(), term.fullscreen():
x = (term.width - len(line)) // 2
y = (term.height - 1) // 2
with term.location(x, y):
print(term.bold_white_on_black(line))
with term.location(0, term.height - 1):
input('press <Enter> to exit..')
For interactive command-line applications from a simple prompt to full
screen terminal applications, you could use *prompt_toolkit* module [2].
> My first goal is to build a program that would interface with a
> Raspberry Pi to control a heating process.
>
For flexibility, you could split your program into a server and a client
that controls it (text, GUI, web).
To control the Raspberry Pi from your mobile device, you could try Kivy
[3] or just create something quick in Pythonista 3 on iOS [4]. To create
a GUI on desktop, you could try Tkinter, Gtk, Qt frameworks [5,6].
For the web part on the server, you could try *flask*, *bottle* modules
[7], to return json data to a client using http protocol:
from bottle import route # $ pip install bottle
@route('/data')
def data():
return dict(data=[1,2,3])
On the (text/GUI) client in Python:
import requests # $ pip install requests
data = requests.get('https://your.server.example.com/data').json()
Or in the browser using jQuery [8]:
$.getJSON('https://your.server.example.com/data', function(data) {
//use data here
});
To make the interaction more responsive, you could use WebSocket protocol e.g., via
Flask-SocketIO on the server [9].
[1]: https://pypi.python.org/pypi/blessings/
[2]: https://python-prompt-toolkit.readthedocs.io/
[3]: https://kivy.org
[4]: http://omz-software.com/pythonista/
[5]: http://www.tkdocs.com/tutorial/onepage.html
[6]: http://doc.qt.io/qt-4.8/gallery-gtk.html
[7]: http://bottlepy.org/docs/stable/
[8]: http://api.jquery.com/jquery.getjson/
[9]: https://flask-socketio.readthedocs.io
More information about the Python-list
mailing list