2016-09-02 4 views
3

kivy에는 뭔가 이해할 수없는 부분이 있으며 누군가가 빛을 비출 수 있기를 바랍니다. 나는이 주제에 대해 많은 독서를 해왔지만 단지 내 머리 속에 연결되는 것처럼 보이지 않습니다.Kivy 버튼을 기능으로 연결

필자의 문제는 기능을 kivy 버튼과 연결 한 것입니다.

def Math(): 
    print 1+1 

내가 뭔가 더 복잡하고 싶으면 : 는 지금은 간단한 기능을 수행하는 방법을 배우려고 노력하고

def Math(a,b): 
    print a^2 + b^2 

ab이 kivy에서 입력 라벨 (label)을하고, 버튼을 클릭하면 답이 인쇄됩니다.

내가 지금까지 무엇을 가지고 :

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition 
from kivy.uix.widget import Widget 
from kivy.uix.floatlayout import FloatLayout 


#######``Logics``####### 
class Math(FloatLayout): 
    def add(self): 
     print 1+1 

#######``Windows``####### 
class MainScreen(Screen): 
    pass 

class AnotherScreen(Screen): 
    pass 

class ScreenManagement(ScreenManager): 
    pass 


presentation = Builder.load_file("GUI_Style.kv") 

class MainApp(App): 
    def build(self): 
     return presentation 

if __name__ == "__main__": 
    MainApp().run() 

이 내 kivy 언어 파일입니다

import NoTransition kivy.uix.screenmanager.NoTransition 

ScreenManagement: 
    transition: NoTransition() 
    MainScreen: 
    AnotherScreen: 

<MainScreen>: 
    name: "main" 
    FloatLayout: 
     Button: 
      on_release: app.root.current = "other" 
      text: "Next Screen" 
      font_size: 50 
      color: 0,1,0,1 
      font_size: 25 
      size_hint: 0.3,0.2 
      pos_hint: {"right":1, "top":1} 

<AnotherScreen>: 
    name: "other" 
    FloatLayout: 
     Button: 
      color: 0,1,0,1 
      font_size: 25 
      size_hint: 0.3,0.2 
      text: "add" 
      pos_hint: {"x":0, "y":0} 
      on_release: root.add 
     Button: 
      color: 0,1,0,1 
      font_size: 25 
      size_hint: 0.3,0.2 
      text: "Back Home" 
      on_release: app.root.current = "main" 
      pos_hint: {"right":1, "top":1} 
AnotherScreen 인스턴스이지만,이 add이없는

답변

1
<AnotherScreen>: 
    name: "other" 
    FloatLayout: 
     Button: 
      ... 
      on_release: root.add <-- here *root* evaluates to the top widget in the rule. 

방법.

class Math(FloatLayout): 
    def add(self): 
     print 1+1 

여기 당신은 UIX 구성 요소 -a widget- 인 FloatLayout, 상속하여 수학 클래스를 선언했다. 그리고이 클래스의 메소드를 정의했습니다 add. 아직도 당신은 그것을 사용하지 않았습니다. kv 파일에서 FloatLayout을 사용했습니다.

:

이제 당신도 root/self 또는 app를 사용하여, 대부분의 시간을 당신이 UIX 구성 요소의 메서드로 액세서 것, KV의 기능에 액세스하기 위해서는, 당신은 또한 그것을 예를 가져올 수 있습니다

<AnotherScreen>: 
    name: "other" 
    Math: 
     id: math_layout 
     Button: 
      ... 
      on_release: math_layout.add() 

또는이 같은 :

#: import get_color_from_hex kivy.utils.get_color_from_hex 
<ColoredWidget>: 
    canvas: 
     Color: 
      rgba: get_color_from_hex("DCDCDC") 
     Rectangle: 
      size: self.size 
      pos: self.pos 

그래서 당신은 이런 식으로 할 수 없습니다

class AnotherScreen(Screen): 
    def add(self): 
     print(1+1) 

이 주제에 여전히 문제가 있다면 더 많은 도움을 드리겠습니다.

관련 문제