2016-12-31 74 views
0

我想更新另一個屏幕中存在的字段,但未成功。 當有人能告訴我我在這裏做錯了什麼時,我會非常高興。如何使用kivy screenmanager引用其他屏幕中的對象

myscreenskv.py:

style = r''' 
# File: myscreenskv.py 
#: import myscreens myscreens 

<ScreenManagement>: 
    MainScreen: 
    Screen1: 
    Screen2: 

<MainScreen>: 
    name: 'main' 
    mainlog:mainlog 
    id: scrmain 
    BoxLayout: 
     orientation: "vertical" 
     Label: 
      text: "Main" 
     Label: 
      id: mainlog 
     Button: 
      text: "go to screen 1" 
      on_press: 
       app.root.current = "screen1" 
       root.action1() 
     Button: 
      text: "go to screen 2" 
      on_press: 
       app.root.current = "screen2" 
       root.action2() 

<Screen1>: 
    name: 'screen1' 
    sc1log:sc1log 
    id: scr1 
    BoxLayout: 
     orientation: "vertical" 
     Label: 
      text: "Screen1" 
     Label: 
      id: sc1log 
     Button: 
      text: "go to main screen" 
      on_press: app.root.current = "main" 
     Button: 
      text: "go to screen 2" 
      on_press: app.root.current = "screen2" 

<Screen2>: 
    name: 'screen2' 
    id: scr2 
    sc2log:sc2log 
    BoxLayout: 
     orientation: "vertical" 
     Label: 
      text: "Screen2" 
     Label: 
      id: sc2log 
     Button: 
      text: "go to main screen" 
      on_press: app.root.current = "main" 
     Button: 
      text: "go to screen 1" 
      on_press: app.root.current = "screen1" 

''' 

的.py:

from kivy.app import App 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.lang import Builder 
from myscreenskv import style 

class MainScreen(Screen): 
    def __init__(self, **kwargs): 
     super(MainScreen, self).__init__(**kwargs) 

    def action1(self): 
     self.ids.scr1.sc1log.text = 'Coming from main' 

    def action2(self): 
     self.ids.scr2.sc2log.text = 'Coming from main' 

class Screen1(Screen): 
    def __init__(self, **kwargs): 
     super(Screen1, self).__init__(**kwargs) 

    def action1(self): 
     self.ids.main.mainlog.text = 'Coming from screen1' 

    def action2(self): 
     self.ids.scr2.sc2log.text = 'Coming from screen1' 


class Screen2(Screen): 
    def __init__(self, **kwargs): 
     super(Screen2, self).__init__(**kwargs) 

    def action1(self): 
     self.ids.main.mainlog.text = 'Coming from screen2' 

    def action2(self): 
     self.ids.scr1.sc1log.text = 'Coming from screen2' 

class MyscreensApp(App): 
    def build(self): 
     Builder.load_string(style) 
     sm = ScreenManager() 
     sm.add_widget(MainScreen()) 
     sm.add_widget(Screen1()) 
     sm.add_widget(Screen2()) 
     sm.current = 'main' 
     return sm 


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

回答

1

您試圖訪問ids字典,這很好,但在一個完全不同的情況,這就是爲什麼這個錯誤:

AttributeError: 'super' object has no attribute '__getattr__' 

您需要訪問正確的實例,才能訪問其屬性(在您的情況下需要訪問ScreenManager)以訪問其屬性(實例列表),您可以從中進行所需的編輯例如,文本:

MainScreen.action1():

self.manager.screens[1].sc1log.text = 'Coming from main' 
# no ids, because you put it into a variable before 

要理解爲什麼它的工作原理讓我們看看widget樹:

<MainScreen>: 
    id: scrmain 
    BoxLayout: 
     Label: 
     Label: 
      id: mainlog 
     Button: 
     Button: 

這裏idsMainScreen訪問從MainScreen().ids(實例)的字典,這是輸出:

{'mainlog': <WeakProxy to <kivy.uix.label.Label object at 0x12345678>>} 

這意味着你不能在根小部件真的分配給自己的字典 - 至少不是這樣+無論如何都沒有意義,因爲你可以調用root,它給你一個根部件的實例。

+0

親愛的KeyWeeUsr,新年快樂,非常感謝您的澄清。現在我明白了 :) –

相關問題