6

私が理解していないkivyに何かがあり、誰かが光を当てることができることを望んでいます。私はこのトピックをたくさん読みましたが、頭の中でつながっていないようです。

私の問題は、関数をkivyボタンにリンクすることから来ています。今、私は単純な機能を実行する方法を学ぼうとしています:

def Math():
    print 1+1

もっと複雑なことをしたい:

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

aおよびはkivybからの入力ラベルであり、ボタンをクリックすると回答が出力されます。

これは私がこれまでに持っているものです:

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}
4

1 に答える 1

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

addこれは AnotherScreen インスタンスですが、メソッドがありません。

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

FloatLayoutここでは、uix コンポーネント (ウィジェット)から継承して Math クラスを宣言しています。そして、このクラスにメソッドを定義しましたadd。まだあなたはそれを使っていません。使用したkvファイルでFloatLayout.

rootkv で関数にアクセスするには、ほとんどの場合、 /selfまたはを使用して uix コンポーネントのメソッドとしてアクセスします。appたとえば、次のようにインポートすることもできます。

#: 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 

したがって、次のようにすることはできません。

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

またはこのように:

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

このトピックに関してまだ問題がある場合は、喜んでさらにサポートいたします。

于 2016-09-02T19:34:37.503 に答える