Dynamic Updating of Label text

8,252 views
Skip to first unread message

BBQTrader .

unread,
May 7, 2014, 5:05:15 PM5/7/14
to kivy-...@googlegroups.com
I'm new to Kivy, and new to GUIs, but not new to programming.

I've got a project where I'll be receiving some socket data, and I want to update Label and/or Button text with some dynamic information.

In order to simulate that, I have a Thread that will kick off a text change about 5 seconds later, so I can have a look.

I've look everywhere for examples that dynamically change Label text, but everything is originating from an event, such as a button click; my code will need to update on the arrival of new data, and be pushed to the screen.

I thought I might have to 'repaint' or 'refresh' (my words, don't know the right thing here) the GUI to get the new text up, but I can't figure out how to do that, or even know if that's what I need to do!

=========py code=======
import time
from threading import Thread
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty


class Box_a(BoxLayout):
    pass
    
class Label_a(Label):
    display_text = StringProperty("Banjos Rule!")
    
    def chgText(self):
        time.sleep(5)
        print("changing text")
        print(self.display_text)
        self.display_text = "Fiddle ain't bad!"
        print("changed text")
        print(self.display_text)

class Simple2(App):
        
    def build(self):
        return  Box_a()

def main():
    
    
    th = Thread(target=Label_a().chgText)
    th.start()
    
    b = Simple2()
    b.run()
    
    return 0

if __name__ == '__main__':

    main()


============kv code============
<Box_a>:
    Label_a:
        text: "My label is " + self.display_text

BBQTrader

unread,
May 7, 2014, 8:41:20 PM5/7/14
to kivy-...@googlegroups.com
Found this link from tshirtman.  I see where he's creating multitudes of Labels in that code.

I hadn't considered that approach.

Is this a normal approach?  To create a new Label with new text instead of updating the text on an old Label?

Thanks

BBQ

BBQTrader

unread,
May 8, 2014, 7:44:35 AM5/8/14
to kivy-...@googlegroups.com
Still looking, but found I could adopt the Pong game on kivy.org as follows, and it would update.

So, at least this shows me one approach.  Whether or not it's the 'best', I'm too noob to know!

BBQ
=======Pong.py:=========
 


from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty,\
    ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock


class Player(Widget):
    score = NumericProperty(0)

class PongGame(Widget):
#    player1 = ObjectProperty(None)
    player1 = Player()
    
    def update(self, dt):
        self.player1.score += 1


class PongApp(App):
    def build(self):
        game = PongGame()
        Clock.schedule_interval(game.update, 1.0/2.0)
        return PongGame()

def main():
    PongApp().run()

if __name__ == '__main__':

    main()

====kv==
#I've modified this for a test I'm doing on how to dynamically update
#Label text
<PongGame>:    
    
    
    Label:
        font_size: 70  
        center_x: root.width / 2
        top: root.top / 2
        text: str(root.player1.score)
        


On Wednesday, May 7, 2014 5:05:15 PM UTC-4, BBQTrader wrote:

Bill Eaton

unread,
May 9, 2014, 12:38:36 AM5/9/14
to kivy-...@googlegroups.com
To avoid having to subclass Label, you might try using id in the kv language. id allows you to get a handle to your kv label in Python. On the Python side, you import ObjectProperty from kivy.properties

So your kv could be something like:
<Box_a>:
    mylabel
    Label:
        id: mylabel
        text: "hello, world"

and your Python could be 
import time
from threading import Thread
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty

class Box_a(BoxLayout):

   
def chgText(self):
        mylabel
= ObjectProperty()


        time
.sleep(5)
       
print("changing text")
       
print(self.display_text)

       
self.mylabel.text = "Fiddle ain't bad!"
       
print("changed text")
       
print(self.self.mylabel.text)

Also, if you're looking for examples, you should poke around in... wait for it... the examples (or kivy-examples) directory. I think it comes with Windows. And in some Linux distros, you'll have to make sure to install the kivy-examples package.




Reply all
Reply to author
Forward
0 new messages