猕猴桃更改标签文本

时间:2019-07-19 23:10:20

标签: python kivy kivy-language

在检测到滑动事件后,我想在Python / Kivy中更改标签文本。更改文本基本上可以通过以下行进行


    self.Translation.text = "test"

但是我必须在检测到另一个类的滑动事件后更改文本,在该类中我调用一个函数来更改标签文本:


    MyWidget.ThisDoesntWork("self_dummy")

在此函数中,与上面完全相同的行给我一个错误。

如何从调用函数“ MyWidget.ThisDoesntWork(“ self_dummy”)“的“ Swiping_class”类更改标签文本?


    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()

1 个答案:

答案 0 :(得分:1)

我想我找到了解决方案。在您的Swiping_class中,替换以下行:

[[1.5078927]]
[[1.5078927]]

Training

[[1.3262703]]
[[1.5078927]]

有这行:

MyWidget.ThisDoesntWork("self_dummy")

这样,您无需传递字符串到您的方法,而是传递了label对象,该对象包含您要修改的text属性。