Clark Clark - 26 days ago 12
Python Question

Change button text dynamically in kivy

I wrote a service in python using OSC library here is my code:

def __init__(self, * * kwargs):

super(ElectronApp, self).__init__( * * kwargs)

self.electronController = ElectronController()

self.button = ElectronTHView()

def build(self):
return Builder.load_file('ElectronApp.kv')


def build(self):

self.button = ElectronTHView()

self.service = None

self.start_service()

osc.init()

oscid = osc.listen(port = 3002)

osc.bind(oscid, self.display_message, '/message')

osc.bind(oscid, self.date, '/date')

Clock.schedule_interval(lambda * x: osc.readQueue(oscid), 0)

self.root = Builder.load_file('ElectronApp.kv')

return self.root
....


def date(self, message, * args):

print(message[2])

# here i get message from my service and i want to update text on button
self.button.ids.tem.text = message[2]

class ElectronAppMain(BoxLayout):

pass

class ElectronAppMainView(TabbedPanel):

pass

class ElectronAppBottomBar(BoxLayout):

pass


class ElectronButton(Button):

pass

class ElectronTHView(BoxLayout):
pass


*.KV file code:

ElectronAppMain:

<ElectronAppMain>:

padding: "-2dp"

orientation: "vertical"

canvas.before:

Rectangle:

size: self.size

pos: self.pos

source: "background.png"

ElectronAppMainView:

ElectronAppBottomBar:


<ElectronAppBottomBar>:
size_hint: 1, None
height: "50dp"
padding: "0dp"
orientation: "horizontal"
spacing: "0dp"
ElectronButton:
text: "STOP"
size_hint: 1, None
height: "50dp"
on_release:
ElectronButton:
text: "EXIT"
size_hint: 1, None
height: "50dp"
on_release:

<ElectronAppMainView>:
tab_pos: "top_left"
do_default_tab: False
tab_height: "50dp"
tab_width: root.width/3
background_image:
font_size: "12dp"
background_normal:
background_down:
text: "T/H"
font_name: "fonts/Roboto-Medium.ttf"
ElectronTHView:

TabbedPanelItem:
font_size: "12dp"
background_normal:
background_down:
text: "CUSTOM"
font_name: "fonts/Roboto-Medium.ttf"

TabbedPanelItem:
font_size: "12dp"
background_normal:
background_down:
text: "PATTERN"
font_name: "fonts/Roboto-Medium.ttf"

<ElectronTHView>:
orientation: "vertical"
spacing: "5dp"
padding: "10dp"


ElectronButton:
id: temp
background_normal: ....
background_down:
size_hint: 1, .5
height: self.height
text: "Temperature"
on_release:
ElectronButton:
background_normal:
background_down:
size_hint: 1, .5
height: self.height
text: "Humidity"
on_release:


I tried more options but nothing... when i try to update text in

def date(self, message, *args):

print(message[2])
#here i get message from my service and i want to update text on button
self.button.ids.tem.text = message[2]


i receive such error:

Sat Oct 22 16:35:27 2016
OSC callback /date caused an error: 'super' object has no attribute '__getattr__'
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/kivy/lib/osc/OSC.py", line 288, in dispatch
callbackfunction(message, source)
File "main.py", line 128, in date
self.button.ids.temp.text = '1'
File "kivy/properties.pyx", line 757, in kivy.properties.ObservableDict.__getattr__ (kivy/properties.c:11882)
AttributeError: 'super' object has no attribute '__getattr__'
---------------------
[INFO ] [Base ] Leaving application in progress...
[ERROR ] [OSC ] Error in Tuio recv()
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/kivy/lib/osc/oscAPI.py", line 229, in run
message = self.socket.recv(65535)
error: [Errno 4] Interrupted system call


I'm new in kivy ...Thanks in advance for help!

Answer

First hi to all and excuse that i have forgotten to share the solution of my issue! It's was very simple - but i was in bewilderment and not at once have understood All that i do - I have just rewritten *.kv file in such mode:

 ElectronAppMain:

<ElectronAppMain>:

   padding: "-2dp"
    orientation: "vertical"
    canvas.before:
        Rectangle:
            size: self.size
            pos: self.pos
            source: "background.png"
    ElectronMainView:

        tab_pos: "top_left"
        do_default_tab: False
        tab_height: "50dp"
        tab_width: root.width/3
        TabbedPanelItem:
            font_size: "12dp"
            text: "T/U"
            font_name: "fonts/Roboto-Medium.ttf"
            ElectronTHView:
                orientation: "vertical"
                spacing: "5dp"
                padding: "5dp"

                ElectronLabel:
                    id: date
                    color: 0,0,0,0.54
                    halign: "center"
                    font_size: "16dp"
                    size_hint: 1, 1
                    height: self.texture_size[1]
                    text_size: self.width, None
                    font_name: "fonts/Roboto-Regular.ttf"

                ElectronButton:
                    id: test  
                    on_press: app.send()


        TabbedPanelItem:
            font_size: "12dp"
            text: "CUSTOM"
            font_name: "fonts/Roboto-Medium.ttf"

        TabbedPanelItem:
            font_size: "12dp"

            text: "PATTERN"
            font_name: "fonts/Roboto-Medium.ttf"

Thanks to all again!

Comments