2017-08-04 56 views
1

我很難弄清楚如何更改kivy小部件中的標籤文本。爲了簡單起見,我將標籤設置爲0,並且我想在此示例中將文本更改爲30。但是,我收到以下錯誤。更改類中的kivy小部件屬性

AttributeError的:「超級」對象有沒有屬性「GETATTR

我明白,我可能沒有很好地針對小部件,我希望有人能解釋一下如何具體引用的這個文本標籤(self.ids.mainel1temp.stuff_r.text =「30」)來更新(比修改代碼更詳細)

#!/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

你做了幾件事錯在這裏。 您不想在Screen內放置ScreenManager 只需要一個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()