How do I edit a label or button written in the kivy language with python

1,380 views
Skip to first unread message

Columbus Holt

unread,
Oct 30, 2014, 7:05:52 PM10/30/14
to kivy-...@googlegroups.com
Hey,

I'm trying to change the text of a label or button that is in the kivy language with python.  For example, say I wanted to make the text of the label in <UBW> to a string of text from python - how would I go about doing so?

Here's a snippet of the code:
<UBW>:
    GridLayout:
        row_force_default: True
        row_default_height: 100
        cols: 2
        canvas.before:
            Color:
                rgb: .5, .5, .5
            Rectangle:
                pos: self.pos
                size: self.size
            Color:
                rgb: 1, 1, 1
            BorderImage:
                # BorderImage behaves like the CSS BorderImage
                border: 10, 10, 10, 10
                source: 'C:/Users/mmoma_000/Pictures/Wallpapers/fsn.jpg'
                pos: self.pos
                size: self.size
        Button:
            text: 'Back to menu'
            color: 0,0,.5
            on_press: root.manager.current = 'menu'
            background_color: 1,0,0,1
        Label:
            id: AName
            text: "F S/N: UBW"
            font_size: '24sp'

class MenuScreen(Screen):
    pass


class SettingsScreen(Screen):
    pass


class UBW(Screen):
    pass


class LH(Screen):
    pass


# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
sm.add_widget(UBW(name='UBW'))
sm.add_widget(LH(name='LH'))

class TestApp(App):

    def build(self):
        return sm

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



Thanks,

c.h.

Leon A

unread,
Oct 31, 2014, 4:02:03 AM10/31/14
to kivy-...@googlegroups.com
Ive changed it around a bit and ignore all the imports I took it from a project I am on at the mo.

Hope this helps.
Use self.ids to access your label or whatever you like.

import kivy
import socket
import re
import subprocess
import os
import time
import threading
import Queue
import sys
import ast
from kivy.uix.stacklayout import StackLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty,StringProperty,NumericProperty, ListProperty
from kivy.config import Config
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.switch import Switch
from kivy.clock import Clock
from kivy.uix.progressbar import ProgressBar
from kivy.uix.popup import Popup
from kivy.metrics import Metrics

Builder.load_string('''

<UBW>:
    GridLayout:
        row_force_default: True
        row_default_height: 100
        cols: 1

        canvas.before:
            Color:
                rgb: .5, .5, .5
            Rectangle:
                pos: self.pos
                size: self.size
            Color:
                rgb: 5, 5, 5
           
        Button:
            id: MyB
            text: '
Back to menu'
            color: 0,0,.5
            on_press: root.change_lbl()
            background_color: 1,0,0,1
        Label:
            id: AName
            text: "F S/N: UBW"
            font_size: '
24sp'
            size_hint: .5, .5
'''
)

class MenuScreen(Screen):
   
pass


class SettingsScreen(Screen):
   
pass


class UBW(Screen):

   
def __init__(self, **kwargs):
       
super(UBW, self).__init__(**kwargs)
       
self.lbl = self.ids['AName']
       
self.btn = self.ids['MyB']

   
def change_lbl(self):

       
self.lbl.text = 'Whatever you want'




class LH(Screen):
   
pass


# Create the screen manager
sm
= ScreenManager()

sm
.add_widget(UBW(name='UBW'))

sm
.add_widget(MenuScreen(name='menu'))
sm
.add_widget(SettingsScreen(name='settings'))

sm
.add_widget(LH(name='LH'))

class TestApp(App):

   
def build(self):
       
return sm

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

Columbus Holt

unread,
Oct 31, 2014, 10:35:44 AM10/31/14
to kivy-...@googlegroups.com
Thanks for the response,

How would I go about calling the change_lbl function without having to use on_press in the kivy language?

Thanks

Columbus Holt

unread,
Oct 31, 2014, 10:40:26 AM10/31/14
to kivy-...@googlegroups.com


On Friday, October 31, 2014 10:35:44 AM UTC-4, Columbus Holt wrote:
Thanks for the response,

How would I go about calling the change_lbl function without having to use on_press in the kivy language?
   Eventually I want to grab a string of text from an XML file (working on that part right now) and have the text of the label be that string, so I need to set the text of the label (written in the kivy language) using Python

Thanks

ZenCODE

unread,
Nov 4, 2014, 3:05:30 PM11/4/14
to kivy-...@googlegroups.com
Not sure if you figured this out, but you can just set the labels text by:


    self.lbl.text = 'Whatever you want'

anywhere in the UBW class. From outside this class, you could use:

    ubw_instance.lbl.text = "new text"

If that's not clear, please indicate exactly where (in your 1st example) you would like to set the text.

Peace



Reply all
Reply to author
Forward
0 new messages