1

I would like to change a Label-text in Python/Kivy after a swipe event has been detected. Changing the text works basically via the following line


    self.Translation.text = "test"

but I have to change the text after detecting a swipe event from another class, in which I call a function to change the label text:


    MyWidget.ThisDoesntWork("self_dummy")

In this function the exact same line as above gives me an error.

How can I change the Label-text from class "Swiping_class" calling function "MyWidget.ThisDoesntWork("self_dummy")"?


    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.anchorlayout import AnchorLayout
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.uix.textinput import TextInput
    from kivy.base import EventLoop
    from kivy.clock import Clock
    from kivy.factory import Factory
    from kivy.uix.stacklayout import StackLayout
    from kivy.uix.image import Image
    from kivy.uix.floatlayout import FloatLayout
    from kivy.graphics import *
    from kivy.properties import ListProperty
    from kivy.lang import Builder
    from kivy.core.window import Window
    from kivy.uix.screenmanager import ScreenManager, Screen
    from random import random
    import pickle
    import random


    kv = '''
    <ColoredLabel>:
        size: (self.size_x,self.size_y)
        pos: (0,0) # no effect
        background_color:
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                pos: self.pos
                size: (self.size_x,self.size_y)
        '''

    Builder.load_string(kv)

    class ColoredLabel(Label):
        background_color = ListProperty((0,0,0,1))
        s_global = Window.size
        size_x = s_global[0]
        size_y = s_global[1]/3

    class MyWidget(BoxLayout):
        #init
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            s_global = Window.size
            size_x = s_global[0]
            size_y = s_global[1]/3
            self.ForeignLanguage = ColoredLabel(text="str_fl", size_hint=(None, None),size = (size_x,size_y), background_color=(0/255,171/255,169/255, 1))
            self.Translation = ColoredLabel(text="str_tr", size_hint=(None, None),size = (size_x,size_y), background_color=(45/255,137/255,239/255, 1))
            self.Example = ColoredLabel(text="str_ex", size_hint=(None, None),size = (size_x,size_y), background_color=(43/255,87/255,151/255, 1))

            self.verticalBox     = BoxLayout(orientation='vertical')
            self.verticalBox.add_widget(self.ForeignLanguage)
            self.verticalBox.add_widget(self.Translation)
            self.verticalBox.add_widget(self.Example)

            self.Translation.text = "test"

            s=Swiping_class()
            s.add_widget(self.verticalBox)
            self.add_widget(s)

        def ThisDoesntWork(self):
            print("this is printed")
            self.Translation.text = "I wanna change this via fucntion"
            print("this is not printed anymore")

    class Swiping_class(Screen):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.initial = 0
        def on_touch_down(self, touch):
            self.initial = touch.x

        def on_touch_up(self, touch):
            if touch.x < self.initial:
                print("swiped left")
                MyWidget.ThisDoesntWork("self_dummy")
            else:
                print("swiped right")

    class BoxLayoutDemo(App):
        def build(self):
            return MyWidget()

    if __name__ == '__main__':
        BoxLayoutDemo().run()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Dan
  • 49
  • 1
  • 8

1 Answers1

1

I think I found a solution. Within your Swiping_class, replace this line:

MyWidget.ThisDoesntWork("self_dummy")

with this line:

MyWidget.ThisDoesntWork(self.parent)

That way, instead of passing a string to your method, you pass the label object, which contains the text attribute you are trying to modify.

Nordicca
  • 46
  • 1
  • 8