在Class中更改kivy小部件属性

时间:2017-08-04 15:42:41

标签: python properties widget kivy getattr

我很难弄清楚如何在kivy小部件中更改标签的文本。为简单起见,我将标签设置为0,我想在本例中将文本更改为30。但是,我收到以下错误。

属性错误:'超级'对象没有属性' getattr '

我知道我可能没有正确定位该小部件,我希望有人可以解释如何具体引用此标签的文本(self.ids.mainel1temp.stuff_r.text =' 30& #39;)更新(比修复代码更详细)

#!/usr/bin/kivy
import kivy


from random import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.switch import Switch
from kivy.uix.label import Label

from kivy.config import Config
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')

Builder.load_string("""

<Menuscreen>:
    #Handling the gesture event.
    ScreenManager:
        id: manager
        Screen:
            id: main_screen
            name:'main_screen'
            stuff_r: mainel1temp
            FloatLayout:

                Label:
                    id: mainel1temp
                    size: self.texture_size
                    text:'0'
                    size_hint: None, None
                    text_size: 75,75
                    pos: 295,308
                    font_size:'20sp'
                    halign: 'center'



""")

class Thermostuff(Screen):
    stuff_r = ObjectProperty(None)

    def starttherm(self):
        Clock.schedule_interval((self.read_temp), 1)

    def read_temp(self, dt):
        self.ids.mainel1temp.stuff_r.text = '30'


Thermrun = Thermostuff()
Thermrun.starttherm()

class MenuScreen(Screen):
    pass

sm = ScreenManager()
menu_screen = MenuScreen(name='menu')
sm.add_widget(menu_screen)


class TestApp(App):

    def build(self):

        return sm

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

1 个答案:

答案 0 :(得分:1)

你在这里犯了一些错误。 你不想在ScreenManager内加Screen 只需要一个ScreenManager。 然后,您可以Clock __init__中的Thermostuff(Screen)开始 或者,如果您希望它启动on_enter,您需要覆盖它。在这种情况下,您可能想以某种方式检查,如果它已经全部启动,那么您将不会运行多个时钟。 然后,当您创建ObjectProperty时,您不需要self.ids,因为您已经创建了该属性。所以self.stuff_r现在是标签。

我重写了你的例子,以证明这一点 试试这个:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
from kivy.clock import Clock

sm = """


#Handling the gesture event.
ScreenManager:
    id: manager
    MenuScreen:
        Button:
            text: "Go to Thermostuff"
            on_release:
                root.current = "main_screen"
    Thermostuff:
        name:'main_screen'
        stuff_r: mainel1temp
        FloatLayout:

            Label:
                id: mainel1temp
                size: self.texture_size
                text:'0'
                size_hint: None, None
                text_size: 75,75
                pos: 295,308
                font_size:'20sp'
                halign: 'center'



"""

class Thermostuff(Screen):
    stuff_r = ObjectProperty(None)
    test_temp = 0

    def __init__(self,**kwargs):
        super(Thermostuff,self).__init__(**kwargs)
        Clock.schedule_interval((self.read_temp), 1)

    def read_temp(self, dt):
        self.test_temp += 1
        self.stuff_r.text = str(self.test_temp)


class MenuScreen(Screen):
    pass


class TestApp(App):

    def build(self):

        return Builder.load_string(sm)

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