StringProperty not displaying on Button CLick

71 views
Skip to first unread message

Dave McCormick

unread,
Aug 20, 2016, 8:50:11 PM8/20/16
to Kivy users support
Hi,

I am having trouble using StringProperties.  In the below python script if I un-comment  the Clock.schedule line in the "class cb(App):" the Kivy UI displays the counting number along with the same number counting in the terminal.

If I run the code as written using the "Start" button from the Kivy UI the counting number will show in the terminal but not in the UI. 

I also included the Kivy code.

Thank you in advance for any help,
Dave
#!/usr/bin/python
import sys
import glob
import serial
import time
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
from kivy.clock import Clock
from kivy.properties import StringProperty
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '150')

num  
= 0
class Counter_Timer(BoxLayout):
    number
= StringProperty()
   
def count(self, dt):
       
global num
        num
= num + 1
       
print num
        numSTR
= str(num)
       
self.number = numSTR

   
def start_button(self):
        counter
= Counter_Timer()
       
Clock.schedule_interval(counter.count, 1.0)

class cb(App):
   
def build(self):
        counter
= Counter_Timer()
       
# Clock.schedule_interval(counter.count, 1.0)

       
return counter


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

Kivy Code.
# cb.kv
<Counter_Timer>:
    orientation
: 'vertical'
   
BoxLayout:
   
Label:
        text
: root.number + ' ticks'
        font_size
: '24dp'
   
Button:
        text
: "Start"
        on_press
: root.start_button()
   
Button:
        text
: "Stop"
       




ZenCODE

unread,
Aug 20, 2016, 10:08:57 PM8/20/16
to Kivy users support

Interesting. This is an instancing issue. Try


class Counter_Timer(BoxLayout):
number = StringProperty('')


def count(self, dt):
global num
num = num + 1
print num
numSTR = str(num)
self.number = numSTR

def start_button(self):
        Clock.schedule_interval(self.count, 1.0)

The problem with your code is that in your 'start_button' function, you create a new instance of the Count_Timer and then display on it's label. But you can't see that instance. You want to change only the value on this instance you are working with...:-)

Dave McCormick

unread,
Aug 20, 2016, 10:47:16 PM8/20/16
to Kivy users support
Thank you!!!!

It works and makes sense now.

Dave
Reply all
Reply to author
Forward
0 new messages