Skip to content

Home News Screenshots Wiki Themes Bugtracker Members Logos Search
  You are not logged in Link icon Log in Link icon Join
You are here: Home » PicoGUI Wiki » cli_python documentation » LowLevelHelloWorld » wikipage_view

Log in
 
FrontPage » LowLevelLibrary »

LowLevelHelloWorld

Let me repeat it again, I can't stress this enough: YOU DON'T WANT TO WRITE APPS USING THE LOW LEVEL LIBRARY. This example code is here just to document usage of the functions, and in case you want to study the protocol.

That said, this code is already more robust than the equivalent C code, as responses.next() will raise exceptions for any errors reported by the server. As the example doesn't handle them, any exception will interrupt the whole program and exit with a suitable (if ugly) error message.

This example is included with the library, in sample/hell_low_level_library.py (or online - guaranteed up-to-date).

Here is the code:

 # low-level library version of Hello, World

 from PicoGUI import network, requests, responses, events

 def test(where='localhost'):
  connection = network.sock(where)
  connection.send(requests.mkstring('Greetings'))
  string_id = responses.next(connection)
  connection.send(requests.register(string_id))
  top_id = responses.next(connection)
  # PG_WIDGET_LABEL is 1
  connection.send(requests.createwidget(1))
  label_id = responses.next(connection)
  connection.send(requests.mkstring('Hello, World'))
  string_id = responses.next(connection)
  # PG_WP_TEXT is 7
  connection.send(requests.set(label_id, 7, string_id))
  responses.next(connection)
  # PG_WP_SIDE is 2, PG_S_ALL is 2048 (1<<11)
  connection.send(requests.set(label_id, 2, 2048))
  responses.next(connection)
  # PG_FSTYLE_BOLD is 256 (1<<8)
  connection.send(requests.mkfont('', 256, 24))
  font_id = responses.next(connection)
  # PG_WP_FONT is 8
  connection.send(requests.set(label_id, 8, font_id))
  responses.next(connection)
  # PG_DERIVE_INSIDE is 2
  connection.send(requests.attachwidget(top_id, label_id, 2))
  responses.next(connection)
  connection.send(requests.update())
  responses.next(connection)
  return connection

 def event_loop(connection):
  while 1:
    connection.send(requests.wait())
    ev = responses.next(connection)
    if isinstance(ev, events.Event) and ev.name == 'close':
      return

 if __name__ == '__main__':
  from sys import argv
  if len(argv) > 2: port = argv[2]
  else: port = 0
  if len(argv) > 1: where = argv[1]
  else: where = 'localhost'
  c = test()
  event_loop(c)

Status

UpToDateStatus