gpt4 book ai didi

python - Kivy 更改标签文本

转载 作者:行者123 更新时间:2023-12-01 07:33:14 24 4
gpt4 key购买 nike

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


self.Translation.text = "test"

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


MyWidget.ThisDoesntWork("self_dummy")

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

如何更改类“Swiping_class”调用函数“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()

最佳答案

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

MyWidget.ThisDoesntWork("self_dummy")

用这一行:

MyWidget.ThisDoesntWork(self.parent)

这样,您就不必将字符串传递给您的方法,而是传递标签对象,其中包含您尝试修改的文本属性。

关于python - Kivy 更改标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57120494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com