2017-07-11 29 views
1

我試圖調用kivy中按鈕按下的函數,即位於與按鈕不同的類屏幕中。我嘗試在應用程序類中運行函數好,並在那裏遇到問題。這裏就是我想調用的函數位於類:通過Kivy按鈕在不同類中調用函數

# Main screen with button layout 
class LandingScreen(Screen): 
    def __init__(self, **kwargs): 
     super(LandingScreen, self).__init__(**kwargs) 
     self.buttons = [] # add references to all buttons here 
     Clock.schedule_once(self._finish_init) 

    def ChangePic(self): 
     self.buttons[1].background_normal = 'folder.png' 

,這裏是我試圖與調用它的按鈕:

<[email protected]>: 
    name: 'input_sc' 
    FloatLayout: 
     size: 800, 480 
     id: anchor_1 
     Label: 
      text: "What would you like to bind to this button?" 
      size_hint: (1,.15) 
      text_size: self.size 
      pos_hint: {'x': 0.11, 'top': 1} 
      font_size: 28 
      font_name: 'Montserrat-Bold.ttf' 
     Button: 
      root: 'landing_sc' 
      id: filebutton 
      size: 150, 150 
      size_hint: None, None 
      background_normal: 'folder.png' 
      background_down: 'opacity.png' 
      pos_hint: {'x': 0.11, 'top': .7} 
      on_release: 
       root.manager.transition = FadeTransition() 
       root.manager.transition.duration = 1.5 
       app.MakeFolder() 
       root.IfFolder() 
       root.ChangeToSlide() 

我有什麼前綴ChangePic( )爲了從這個位置調用它?

另一種方法是從InputScreen類內輕鬆使用LandingScreen類中的按鈕嗎?

謝謝!

回答

2

您可以在App類創建一個變量:

some_variable = LandingScreen() 

,然後在你的按鈕調用ChangePic()這樣的:

on_release: app.some_variable.ChangePic() 

而且,這可以幫助你:StackOverflowKivy's Google GroupIntroduction to properties

+0

這個變量是否需要在方法內部創建或者不是?如果我不把它放在一個裏面,我會得到一個錯誤,如果我把它放在一個裏面,那個app.variable就找不到它了? – John

+0

哪個參考LandingScreen()需要你的應用程序的類內要創建的變量: '類AppClass: some_variable = LandingScreen() DEF MakeFolder(個體): #your代碼here' 所以,some_variables具有LandingScreen()的方法。 您可以使用AppClass()中的「some_variable」調用LandingScreen()的任何方法,只需執行some_variable.ChangePic() 但是,您想從您的kv調用ChangePic(),因此您需要將app.some_variable。 ChangePic() 與您在kv中使用app.MakeFolder()所做的相同,但使用ChangePic()您需要使用some_variable – favcau

相關問題