Label Text Update Does Not Clear Previous Text

57 views
Skip to first unread message

Gumby

unread,
Oct 17, 2019, 12:43:17 PM10/17/19
to Kivy users support
I am communicating with a sensor using a serial protocol. This is my first test app using Kivy (so, please forgive some of the bad style). Two things are happening where the UI is not updating to my expectations.

1.) When I do "self.label_pH1.text = some new value" it does not delete the old text and overlays the new text over the old text.
2.) In my callback function when I call self.button.text = 'Stop Scanning' it does not update the button until after returning from the blocking serial port read call ( self.pH1_text = self.com.read_pH() ).

I attempted a workaround using the clock to schedule updateWidgets, but that didn't work... I'm really stuck here. What am I missing?

ReaderProtoScreen.jpg


import kivy
kivy.require('1.11.1')

# Kivy UI
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.clock import Clock

# Serial Port
import serial
from time import sleep

import binascii

class ReaderEngine:
    
    def __init__(self):
        self.ser = serial.Serial('COM3'9600timeout=3)  # open serial port
        self.packet_Out = bytearray()
        self.packet_In = bytearray()

    def __del__(self):
        self.ser.close()

    def resetSerBuffers(self):
        self.ser.reset_input_buffer()
        self.ser.reset_output_buffer()

    def blinkLED(self):

        self.packet_Out.clear()

        # turn blue LED on
        self.packet_Out.append(0xAA)
        self.packet_Out.append(0x50)
        self.packet_Out.append(0x00)

        self.ser.write(self.packet_Out) # write hexadecimal command

        sleep(5# Time in seconds

        self.packet_Out.clear()

        # turn blue LED off
        self.packet_Out.append(0xAA)
        self.packet_Out.append(0x58)
        self.packet_Out.append(0x00)

        self.ser.write(self.packet_Out) # write hexadecimal command

    def read_pH(self):
        self.packet_Out.clear()
        self.resetSerBuffers()

        # MeaCalc_pH1
        self.packet_Out.append(0xAA)
        self.packet_Out.append(0x22)
        self.packet_Out.append(0x00)
        self.ser.write(self.packet_Out) # write hexadecimal command
        print('write ReadPH')

        # Get echoed command
        self.packet_In = self.ser.read(size=3)
        print('Got Echo ReadPH')

        # Get Reply with pH value
        self.packet_In = self.ser.read(size=32)
        print('Got Reply ReadPH')

        pH_text_bytes = self.packet_In[25:30].decode('ascii')
 
        return pH_text_bytes

    def read_DO(self):
        return


class ReaderScreen(GridLayout):
    button_text = 'Start Scanning'
    button = Button(text=button_text)
    pH1_text = '-.---'
    label_pH1 = Label(text=pH1_text, font_size=25)
    DO1_text = '-.---'
    label_DO1 = Label(text=DO1_text, font_size=25)
    pH2_text = '-.---'
    label_pH2 = Label(text=pH2_text, font_size=25)
    DO2_text = '-.---'
    label_DO2 = Label(text=DO2_text, font_size=25)
    com = ReaderEngine()

    def __init__(self, **kwargs):
        super(ReaderScreen, self).__init__(**kwargs)

        self.cols = 3
        
        self.button.bind(on_press=self.callback)
        self.add_widget(self.button)

        self.add_widget(Label(text='pH'font_size=25))
        self.add_widget(Label(text='DO'font_size=25))
        self.add_widget(Label(text='1'font_size=25))
        self.add_widget(self.label_pH1)
        self.add_widget(self.label_DO1)
        self.add_widget(Label(text='2'font_size=25))
        self.add_widget(self.label_pH2)
        self.add_widget(self.label_DO2)

        #Clock.schedule_interval(self.updateWidgets, 1)

    def callback(selfinstance):
        if self.button.text == 'Start Scanning':
            self.button.text = 'Stop Scanning'
            #self.com.blinkLED()
            self.pH1_text = self.com.read_pH()
            self.label_pH1.text = self.pH1_text
        else:
            self.button.text = 'Start Scanning'        

    def updateWidgets(selfdt):
        self.label_pH1.text = self.pH1_text
        self.button.text = self.button_text
        print('Updating Widgets')
    
class ReaderProtoApp(App):
    def build(self):
        return ReaderScreen()

if __name__ == '__main__':
    ReaderProtoApp().run()

Robert Flatt

unread,
Oct 17, 2019, 6:11:59 PM10/17/19
to Kivy users support
Don't have the hardware so I can fully replicate. But if I comment out ReaderEngine and in callback() set  self.pH1_text = "6.7"  every thing works (no overwrite, button text toggles). So style aside this is mostly there. One small change the app architecture should do it.....

You identified the issue "blocking". This is the cause of the Button.text issue and presumably a general pause. The solution is to make read_pH() an event triggered using the clock so that this function does not block execution. The app gets the update when the pH value is ready via "another_callback()".
So try using schedule_interval() to schedule com.read_pH() At completion of read_pH(), another_callback() returns updates the label text. There will be the extra dt argument, but updateWidgets() show you understand this.

That should address issue 2).  Issue 1) I assume is related but I have no idea why.

Gumby

unread,
Oct 18, 2019, 12:21:22 PM10/18/19
to Kivy users support
I changed the callback to remove the blocking serial port call. The button now updates the text promptly, but the label is still doing the weird overwrite thing.

kivyscrn.png



    def callback(selfinstance):
        if self.button.text == 'Start Scanning':
            self.button.text = 'Stop Scanning'
            #self.com.blinkLED()
            #self.pH1_text = self.com.read_pH()
            self.pH1_text = '7.777'
            self.label_pH1.text = self.pH1_text
        else:
            self.button.text = 'Start Scanning'
            self.pH1_text = '8.888'
            self.label_pH1.text = self.pH1_text  

Elliot Garbus

unread,
Oct 18, 2019, 12:38:26 PM10/18/19
to kivy-...@googlegroups.com
This seems like a graphics driver problem.  Can you update the OpenGL driver?

Sent from my iPhone

On Oct 18, 2019, at 9:23 AM, Gumby <e.scott....@gmail.com> wrote:


I changed the callback to remove the blocking serial port call. The button now updates the text promptly, but the label is still doing the weird overwrite thing.

<kivyscrn.png>



    def callback(selfinstance):
        if self.button.text == 'Start Scanning':
            self.button.text = 'Stop Scanning'
            #self.com.blinkLED()
            #self.pH1_text = self.com.read_pH()
            self.pH1_text = '7.777'
            self.label_pH1.text = self.pH1_text
        else:
            self.button.text = 'Start Scanning'
            self.pH1_text = '8.888'
            self.label_pH1.text = self.pH1_text  

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/54d7cb15-1c3c-4356-a4c5-279760653eba%40googlegroups.com.
<kivyscrn.png>

Elliot Garbus

unread,
Oct 18, 2019, 12:47:50 PM10/18/19
to kivy-...@googlegroups.com
Another thing to try, declare the variables you are using to update the labels as kivy StringProperties. 

pH2_text = StringProperty(‘-.—-‘)

Another hack to try, clear the string prior to updating it. 

Sent from my iPhone

On Oct 18, 2019, at 9:39 AM, Elliot Garbus <elli...@cox.net> wrote:

This seems like a graphics driver problem.  Can you update the OpenGL driver?

Robert Flatt

unread,
Oct 18, 2019, 1:26:10 PM10/18/19
to Kivy users support
Question:
As a test, if you do the same as I did, hardcode a return value say   self.pH1_text = "6.7"    do you still see the overwrite? (I didn't)

On Friday, October 18, 2019 at 6:47:50 AM UTC-10, Elliot Garbus wrote:
Another thing to try, declare the variables you are using to update the labels as kivy StringProperties. 

pH2_text = StringProperty(‘-.—-‘)

Another hack to try, clear the string prior to updating it. 

Sent from my iPhone

On Oct 18, 2019, at 9:39 AM, Elliot Garbus <elli...@cox.net> wrote:

This seems like a graphics driver problem.  Can you update the OpenGL driver?

Sent from my iPhone

On Oct 18, 2019, at 9:23 AM, Gumby <e.scott...@gmail.com> wrote:


I changed the callback to remove the blocking serial port call. The button now updates the text promptly, but the label is still doing the weird overwrite thing.

<kivyscrn.png>



    def callback(selfinstance):
        if self.button.text == 'Start Scanning':
            self.button.text = 'Stop Scanning'
            #self.com.blinkLED()
            #self.pH1_text = self.com.read_pH()
            self.pH1_text = '7.777'
            self.label_pH1.text = self.pH1_text
        else:
            self.button.text = 'Start Scanning'
            self.pH1_text = '8.888'
            self.label_pH1.text = self.pH1_text  

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Robert Flatt

unread,
Oct 18, 2019, 3:21:07 PM10/18/19
to Kivy users support
Shoulda seen this before. I suspect it is the style issue. Static initialization is legal in Python, but may result in unexpected behavior - its a known issue (hence style). Kivy is really good at finding this unexpected behavior.

So move the 11 static initializations in ReaderScreen into its  __init__()  . Prefixing the lhs of the assigns with   self.
Message has been deleted

Gumby

unread,
Oct 21, 2019, 2:15:01 PM10/21/19
to Kivy users support
OK, I started over using your suggestion with something more basic and still see the same behavior. I using StringProperty as Elliot suggested, but can't get it to run. I'm not sure how to use StringProperty.

import kivy
kivy.require('1.11.1'# replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty

class MainWindow(GridLayout):
    
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)

        self.button = Button(text='Hit Me')
        self.label = Label(text='Hello world')

        self.cols = 2
        
        self.button.bind(on_press=self.callback)
        self.add_widget(self.button)
        self.add_widget(self.label)

    def callback(selfinstance):
        if self.button.text == 'Hit Me':
            self.button.text = 'Ouch that hurt!'
            self.label.text = 'Goodbye cruel world'
        else:
            self.button.text = 'Hit Me'
            self.label.text = 'Hello World' 


class MyApp(App):

    def build(self):
        return MainWindow()


if __name__ == '__main__':
    MyApp().run()

kivyhellogoodbyejpg.jpg

Elliot Garbus

unread,
Oct 21, 2019, 3:43:33 PM10/21/19
to Kivy users support
I've updated your code.  I used KV.  KV opens up a lot of power in kivy.  I added 2 labels so I could show how to change the label in python and in KV.

A few things to look at:in call back, first is the use of the String Property.  While it looks like a class variable it is actually an instance variable.

class MyButton(Button):
button_text = StringProperty('Hit Me')

In kivy I set the text attribute to self.button_text, this is a StringPropery as declared in MyButton.  The String Property will automatically update the text value when it changes.

MyButton:
text: self.button_text

In the python code, I changed the text in the label directly by going through the ids dictionary.  Read about it here: https://kivy.org/doc/stable/api-kivy.lang.html?highlight=ids#
def callback(self):
app = App.get_running_app()
if self.button_text == 'Hit Me':
self.button_text = 'Ouch that hurt!'
app.root.ids.label_1.text = 'Goodbye cruel world'

Of course I could have used another StringPropert to change the label as well, but wanted to show this additional capability.

I created a second Label (label_2), and changed it's text value directly in KV:
on_press: 
self.callback()
label_2.text = 'What?' if self.text == 'Hit Me' else 'How?'

It is very easy to access the attributes of widgets in kv, using the id.


Here is the updated code, this is the same as what I have attached:

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.properties import StringProperty
from kivy.lang import Builder


kivy.require('1.11.1') # replace with your current kivy version !

kv = """
GridLayout:
cols: 2
MyButton:
text: self.button_text
on_press:
self.callback()
label_2.text = 'What?' if self.text == 'Hit Me' else 'How?'
BoxLayout:
orientation: 'vertical'
Label:
id: label_1
text: 'Hello world'
Label:
id: label_2
text: 'another way'
"""


class MyButton(Button):
button_text = StringProperty('Hit Me')

def callback(self):
app = App.get_running_app()
if self.button_text == 'Hit Me':
self.button_text = 'Ouch that hurt!'
app.root.ids.label_1.text = 'Goodbye cruel world'

else:
self.button_text = 'Hit Me'
app.root.ids.label_1.text = 'Hello World'


class MyApp(App):

def build(self):
return Builder.load_string(kv)
kivyhelp.py

Gumby

unread,
Oct 21, 2019, 4:04:49 PM10/21/19
to Kivy users support
Thanks for being so helpful. But, it is still doing the same thing. BTW, my environment is Anaconda on Windows 10.

kivyhelp.png

Elliot Garbus

unread,
Oct 21, 2019, 4:08:48 PM10/21/19
to kivy-...@googlegroups.com

Make sure you update your graphics driver.  I think that is all it could be.

 

 

 

From: Gumby
Sent: Monday, October 21, 2019 1:07 PM
To: Kivy users support
Subject: Re: [kivy-users] Re: Label Text Update Does Not Clear Previous Text

 

Thanks for being so helpful. But, it is still doing the same thing. BTW, my environment is Anaconda on Windows 10.

 

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/77f482f3-5763-454a-b925-cb19b28cd10b%40googlegroups.com.

 

kivyhelp.png

Gumby

unread,
Oct 21, 2019, 5:34:29 PM10/21/19
to Kivy users support
Drivers are updated. Still having the same problem. I'm starting to get pretty discouraged. I was planning on writing a new application for my company's flagship product based on Kivy, but if I can't get this working soon I'm going to have to look for another framework.

Robert Flatt

unread,
Oct 21, 2019, 5:34:34 PM10/21/19
to Kivy users support
Nice simple example. :)
But I can't replicate the demonstrated behavior :(  Here it works nicely.
I use Windows 10 , CMD prompt, Python 3.6.1  . Kivy 1.11.1

I don't know anything about Anaconda.

Elliot Garbus

unread,
Oct 21, 2019, 6:13:21 PM10/21/19
to kivy-...@googlegroups.com

That is a strange issue.

Perhaps there is a dependency that is the wrong version.  Check that you have the correct versions of all of the kivy-deps.* files.

 

Here is my pip list:

Package             Version

------------------- ----------

certifi             2019.3.9

chardet             3.0.4

docutils            0.14

idna                2.8

Kivy                1.11.0

kivy-deps.glew      0.1.12

kivy-deps.gstreamer 0.1.17

kivy-deps.sdl2      0.1.22

Kivy-Garden         0.1.4

mido                1.2.9

Pillow              6.0.0

pip                 19.2.1

pipenv              2018.11.26

Pygments            2.4.2

pypiwin32           223

python-rtmidi       1.3.0

pywin32             224

requests            2.22.0

setuptools          41.0.1

urllib3             1.25.3

virtualenv          16.6.0

virtualenv-clone    0.5.3

wheel               0.33.4

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/b90ce2f0-01c7-4c42-8ab4-3f6fe392432c%40googlegroups.com.

 

Avour

unread,
Oct 22, 2019, 7:21:37 AM10/22/19
to kivy-...@googlegroups.com

The issue seems really weird, definitely not something I've seen before, but I really advice you not to give up, you'll will really find development in Kivy really exciting and fun as I have.
The issue it self might just be a small thing we are missing.
Since you're using an anaconda env    (which is likely not the problem) but for the benefit of doubt you should reinstallation kivy again (normally), most time this could be the fix to most of this issues

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.

 

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5dae2d77.1c69fb81.be774.e580SMTPIN_ADDED_MISSING%40gmr-mx.google.com.

Gumby

unread,
Oct 22, 2019, 9:15:28 AM10/22/19
to Kivy users support
How do I list my dependency versions like you have here? Sorry for the noob questions. I've been writing software for 26 years, but I'm fairly new to python. Mostly C++/VB/C#.

Gumby

unread,
Oct 22, 2019, 9:20:21 AM10/22/19
to Kivy users support
Do I just do a "conda install" like it is at the beginning of this page, or should do a manual install as if I'm upgrading from a previous version? I'm still learning how these package managers work (It's a lot different from writing C# in VS .NET).

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Oct 22, 2019, 11:22:05 AM10/22/19
to kivy-...@googlegroups.com
On the command line type:
pip list


Sent from my iPhone

On Oct 22, 2019, at 6:15 AM, Gumby <e.scott....@gmail.com> wrote:


To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/13737e95-311a-4464-9806-1ed79981f8ca%40googlegroups.com.

Gumby

unread,
Oct 22, 2019, 12:43:55 PM10/22/19
to Kivy users support
I did "pip list" and the and the kivy dependencies were not there. I thought for sure we had this thing beat. Evidently the conda installer isn't complete ( https://kivy.org/doc/stable/installation/installation-windows.html ). So I followed the instructions a little further down the page for the manual install. I'll attach the before and after pip list. But, I'm still getting the same behavior on the Label


On Tuesday, October 22, 2019 at 11:22:05 AM UTC-4, Elliot Garbus wrote:
On the command line type:
pip list


Sent from my iPhone
piplist.txt
piplist2.txt

Robert Flatt

unread,
Oct 22, 2019, 10:22:57 PM10/22/19
to Kivy users support
To me piplist2.txt looks fine from the Kivy point of view, though there is a lot of other stuff in there - presumably from Anaconda.

I suspect you need to see this work, the hard part on this side is we can't replicate the issue. And I don't know of previous instances of this issue.

How about trying another PC?
If it doesn't have Python on it all the better, it will be a clean install (3.7 is a good choice); don't install anything not in the Kivy install instructions. Run the test.
A least that should put a lower bound on the issue.

Andrei Sima

unread,
Oct 23, 2019, 4:43:17 AM10/23/19
to Kivy users support
If you are new to python i would suggest you following

1. Use virtualenv and optional VirtualEnvWrapper.
in this way you can manage your virtual envs and control them way better than hunting for conda, pycharm environments.

2. Stay away for now from Conda, PyCharm, and use a normal editor (Sublime, Atom, <that windows one>, Vim, etc.).
i code in python for 5 years and i did not bothered with the above. I really enjoy my virtual envs and the way i can manage them via pip

3. Launch your code from terminal. Or something similar from windows. Drop windows if possible for developing python, even MacOS i find it better for this task than windows.

I guess that you pip list in the main environment. Think about environments as virtual machines for python.
A virtual env has his own python interpreter and his own deps. When you run python main.py in myENV the python interpreter in that env will be picked up and it will look for deps in myENV ONLY, if that fails it will not resolve the deps to the global python env.
In this way you can separate your projects and be sure that different versions of deps will not conflict. For some reasons you need Pill==x.xx (versioning) for one project and Pill==y.yy for another. Having them in different envs per proect will do the trick.

And yes your app behavior is strange, first time i see it. I guess that some dependency is not playing nice with you.

!!!! MIGHT B STUPID !!!!!
Do you by any chance have a .kv file in your project directory? If the name of the app is MyAwesomeApp the kv that will be picked automaticaly will be myAwesome.kv.
or is .py only ?

Can you please post the STDOUT from terminal when app starts?






To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/008a960f-c6dc-47d0-86de-bae032757d5b%40googlegroups.com.

qua non

unread,
Oct 23, 2019, 8:09:35 AM10/23/19
to kivy-...@googlegroups.com
This seems like a oddity related to the graphics driver. 

Let's confirm this with a simple Label update app, can you please try and run the code below::
 
from kivy.app import App
from kivy.lang import Builder

class TestApp(App):

    def build(self):
        return Builder.load_string('''
<ClearLabel@Label>
    canvas.before:
        Color:
            rgba: 0, 0, 0, 1 #black
        Rectangle:
            size: self.size
            pos: self.pos 

BoxLayout
    cols: 2
    rows: 2
    on_parent:
        from kivy.clock import Clock
        Clock.schedule_interval( lambda dt: but.trigger_action(), 1)
    Button:
        id: but
        text: "Push Me..."
        on_release:
            normtext = normal_label.text
            normal_label.text = self.text
            self.text = clear_label.text
            clear_label.text = normtext
    Label:
        id: normal_label
        text: "Till"
    ClearLabel:
        id: clear_label
        text: "99999999999"
''')


if __name__ == "__main__":
    TestApp().run()

If you are getting the issue on the middle label and not on the one on the right,  then please create a issue with logs and all graphics driver details. 

Gumby

unread,
Oct 24, 2019, 12:49:55 PM10/24/19
to Kivy users support
Thanks for that slick little app! This finally helped me see what what going on. 

I have a brand new Dell G7 laptop with an NVIDIA GeForce GTX 1660 Ti graphics card. Based on previous posts I had tried to update the NVIDIA driver through the Device Manager and it kept saying I had the latest driver. I downloaded the NVIDIA GeForce Experience application and it actually found that there was a newer driver, so I installed it. Still, things didn't work.

Two days ago (I was out of town on business yesterday) I noticed something odd in the text output as my apps launched. I saw that it was using the Intel UHD Graphics 630 (see the attached screen shot). I was like, "What's up with that! I thought I had an NVIDIA..." So I went into the Device manager again, right mouse clicked on the Intel UHD Graphics 630, and low and behold it found new drivers and now everything is working!

Thank you so much to everyone who helped. I just started this job about 2 months ago and was really pumping up Kivy as a way to build a new user interface that will look awesome. You all saved me. I hope I can give back to the community the same kind of help someday.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
pykivyout.jpg
devicemgr.jpg

Andrei Sima

unread,
Oct 24, 2019, 1:46:27 PM10/24/19
to Kivy users support
BigUp.
'You came for the language you stay for the community.' not my quote.
Enjoy.



To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/2b376fb7-db24-4191-b579-a3eec52c7bd4%40googlegroups.com.

Elliot Garbus

unread,
Oct 24, 2019, 3:51:08 PM10/24/19
to kivy-...@googlegroups.com

Glad to hear you got it working!  Thanks for the follow up.



To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/2b376fb7-db24-4191-b579-a3eec52c7bd4%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages