Page 1 of 1

guizero - refresh text display

Posted: Mon Jun 12, 2017 2:45 pm
by bensimmo
After having a play with guizero, it looked like the perfect thing we are looking for to display simple information updates.

But we (well I) cannot figure out how to refresh the display easily.
Everything seems to need to have no loops and be initiated by push buttons or sliders.

Am I missing something?

The use of app.display seems to kill off a program.

For example.

-Import app and text from guizero
-Add a textbox.

-Display a value in textbox
-LOOP
-- Add one to the value
-- update text box with .set


IF app.display is in the loop, it will halt at that and doesn't loop.
If it's before the loop it stops there and doesn't go past it and into the loop
If it after the loop, nothing gets updated as it has not yet created the GUI.*

So how do I do this with guizero
Create a display with text boxes.

Collect data values from a sensor
Show value in the text boxes
And repeat


(It's part of a bigger logging program based around the SenseHat Datalogging code, but this simple loop is the basis, get data, display data, save data...).

*Given it never leaves the while true: , app.display would never be run.

Re: guizero - refresh text display

Posted: Mon Jun 12, 2017 4:16 pm
by scotty101
Correct. You should not make use of infinite/long loops within guizero (or tkinter*) programs

The guizero examples show how to make use of a 'timer callback' to call a function again and again (as if in a loop). This example also includes a "read_sensor" function so it should help you out a lot.
https://github.com/lawsie/guizero/blob/ ... example.py
Code copied here for clarity of this answer

Code: Select all

from guizero import *
import random


def read_sensor():
    return random.randrange(3200, 5310, 10) / 100


def update_label():
    text.set(read_sensor())
    # recursive call
    text.after(1000, update_label)


if __name__ == '__main__':
    app = App(title='Sensor Display!',
              height=100,
              width=200,
              layout='grid')

    title = Text(app, 'Sensor value:', grid=[0, 0])
    text = Text(app, "xx", grid=[1, 0])

    text.after(1000, update_label)
    app.display()
You create a function (in the example called update_label) and you use the after method to schedule this function to be called periodically.
app.display should only be called once in a program and any command after that won't be run until the app is quit by the user.

*guizero is a wrapper on top of tkinter

Re: guizero - refresh text display

Posted: Mon Jun 12, 2017 7:40 pm
by bensimmo
Many thanks, I've been using the main documentation https://lawsie.github.io/guizero/about/ here and it's certainly not clear. No mention of that at all.
What you given shows me what I need to attempt doing.

Microsoft make GUI programming so much easier.

Re: guizero - refresh text display

Posted: Mon Jun 12, 2017 10:01 pm
by scotty101
Guizero is new. Give it a chance.

It makes it much easier for beginners.

Re: guizero - refresh text display

Posted: Tue Jun 13, 2017 5:35 am
by B.Goode
Version

guizero is currently version 0.3.3

This is a pre-release version, so there may be bugs and features may change.
Maybe polite constructive feedback to the developer concerning the perceived deficiencies in the documentation would be appropriate?

https://github.com/lawsie/guizero/issues

Re: guizero - refresh text display

Posted: Tue Jun 13, 2017 6:31 am
by bensimmo
I wasn't having a go, just mentioning the documentation didn't mention it.
(and yes I do feedback on git if I think it's needed, but the documentation is not linked)

I had seen guizero crop up before (I've mentioned it before), though given it had hit MagPi & the website https://www.raspberrypi.org/learning/ge ... worksheet/ I figured it must have hit the can now be used level. (No mention in MagPi or the website it is all pre release ;-))
Which it can :-)
But for what I wanted nothing but the small demo on github gave hint of how to actually use it. It's​ just a change of thinking from the Python way they have been teaching on the site.
Job for today, try and get it working and then see if the kids can understand it.

Hopefully it can compliment gpiozero well :-)

Re: guizero - refresh text display

Posted: Tue Jun 13, 2017 8:20 am
by scotty101
A link to the documentation is provided on the GitHub readme page.

https://github.com/lawsie/guizero/blob/master/README.md

Re: guizero - refresh text display

Posted: Tue Jun 13, 2017 8:29 am
by bensimmo
scotty101 wrote:A link to the documentation is provided on the GitHub readme page.

https://github.com/lawsie/guizero/blob/master/README.md
but not vice versa or any specific way to comment on the documentation.
compare that to gpiozero
https://gpiozero.readthedocs.io


It'll come along though as people start using it.

It's a brilliant idea

Re: guizero - refresh text display

Posted: Mon Jun 19, 2017 7:57 am
by lawsie
The entire documentation resides in the exact same github repo at https://github.com/lawsie/guizero/tree/master/docs. You're very welcome to create a pull request to docs-src (https://github.com/lawsie/guizero/tree/master/docs-src ) if you think something needs updating, and I will merge it and regenerate the docs as necessary.

Re: guizero - refresh text display

Posted: Mon Jun 19, 2017 11:24 am
by bensimmo
lawsie wrote:The entire documentation resides in the exact same github repo at https://github.com/lawsie/guizero/tree/master/docs. You're very welcome to create a pull request to docs-src (https://github.com/lawsie/guizero/tree/master/docs-src ) if you think something needs updating, and I will merge it and regenerate the docs as necessary.
That's another thing I've got to get around to learning about :-)
I can ask questions on github so far (as you may know :oops: )

Re: guizero - refresh text display

Posted: Mon Jun 19, 2017 8:21 pm
by B.Goode
bensimmo wrote:
lawsie wrote:The entire documentation resides in the exact same github repo at https://github.com/lawsie/guizero/tree/master/docs. You're very welcome to create a pull request to docs-src (https://github.com/lawsie/guizero/tree/master/docs-src ) if you think something needs updating, and I will merge it and regenerate the docs as necessary.
That's another thing I've got to get around to learning about :-)
I can ask questions on github so far (as you may know :oops: )

Those nice Raspberry Pi Foundation people have even provided a Learning Resource (tutorial) that explains just how to do that...
https://www.raspberrypi.org/learning/ge ... -with-git/

Re: guizero - refresh text display

Posted: Tue Jun 20, 2017 7:12 am
by bensimmo
B.Goode wrote:
bensimmo wrote:
lawsie wrote:The entire documentation resides in the exact same github repo at https://github.com/lawsie/guizero/tree/master/docs. You're very welcome to create a pull request to docs-src (https://github.com/lawsie/guizero/tree/master/docs-src ) if you think something needs updating, and I will merge it and regenerate the docs as necessary.
That's another thing I've got to get around to learning about :-)
I can ask questions on github so far (as you may know :oops: )

Those nice Raspberry Pi Foundation people have even provided a Learning Resource (tutorial) that explains just how to do that...
https://www.raspberrypi.org/learning/ge ... -with-git/
It's terrible isn't it, they make it easy for us :-)

Re: guizero - refresh text display

Posted: Wed Sep 20, 2017 2:08 pm
by emailman
Thanks for posting the example. This is exactly what I was trying to do.

I would like to understand this statement better:
text.after(1000, update_label)

I understand that after 1000 milliseconds, the function update_label is called, but why is it executed on the widget "text". Can ".after" be applied to any widget? Where is ".after" documented?

This is the first semester I've been using giuzero in an Intro to Programming course in Python. It so much easier than teaching tkinter to new programmers. Combining it with gpiozero on a Raspberry Pi opens up so many possibilities. Much thanks for both of these wonderful libraries.

Re: guizero - refresh text display

Posted: Thu Sep 21, 2017 6:20 am
by bensimmo
I opened an issue on this (seemed the right thing to do) way back when.
https://github.com/lawsie/guizero/issues/30

At the moment you also need to read the tkinter docs.

Re: guizero - refresh text display

Posted: Wed Sep 27, 2017 7:21 am
by lawsie
Yes, sorry, I haven't had time to do any updates on guizero for a little while now.