2017-08-22 14 views
0

私はKivyとPythonで電卓を作成するチュートリアルを見てきましたが、私はこのプロパティを見てきました。ウィジェットIDの値で表示します。この値を使用すると、ウィジェットの他のプロパティにアクセスできます。ここ は、コード(.kvファイル)である:Pythonの表示プロパティKivy

<CalcGridLayout>: 
    id: calculator 
    display: entry #this is the display property 
    rows: 5 
    padding: 10 
    spacing: 10 

    BoxLayout: 
     TextInput: 
      id: entry #with the value of this 
      font_size: 32 
      multiline: False 

    BoxLayout: 
     spacing: 10 
     Button: 
      text: "7" 
      on_press: entry.text += self.text 
     Button: 
      text: "8" 
      on_press: entry.text += self.text 
     Button: 
      text: "9" 
      on_press: entry.text += self.text 
     Button: 
      text: "+" 
      on_press: entry.text += self.text 

    BoxLayout: 
     spacing: 10 
     Button: 
      text: "4" 
      on_press: entry.text += self.text 
     Button: 
      text: "5" 
      on_press: entry.text += self.text 
     Button: 
      text: "6" 
      on_press: entry.text += self.text 
     Button: 
      text: "-" 
      on_press: entry.text += self.text 

    BoxLayout: 
     spacing: 10 
     Button: 
      text: "1" 
      on_press: entry.text += self.text 
     Button: 
      text: "2" 
      on_press: entry.text += self.text 
     Button: 
      text: "3" 
      on_press: entry.text += self.text 
     Button: 
      text: "*" 
      on_press: entry.text += self.text 

    BoxLayout: 
     spacing: 10 
     Button: 
      text: "AC" 
      on_press: entry.text = "" 
     Button: 
      text: "0" 
      on_press: entry.text += self.text 
     Button: 
      text: "=" 
      on_press: calculator.calculate(entry.text) 
     Button: 
      text: "/" 
      on_press: entry.text += self.text 

その変数/プロパティとは何ですか?それは何のために使われますか?あなたのKVファイルを参照して

答えて

2

calc.kv

display: entry 

main.py

display = ObjectProperty(None) 

表示 - あなたの変数であり、それはPythonオブジェクトとして宣言され、OBJECTPROPERTY 。これは、kvルールによって作成されたTextInput子ウィジェットを参照するために使用されます。 ObjectPropertyを宣言した後、それをkvルールで作成された子ウィジェットに接続します。 "表示:エントリ"。これが完了したら、calculateメソッド内のTextInputプロパティを簡単に参照できます。

main.py

from kivy.app import App 
from kivy.uix.gridlayout import GridLayout 
from kivy.properties import ObjectProperty 


class CalcGridLayout(GridLayout): 
    display = ObjectProperty(None) 

    def calculate(self, dt): 
     print(self.display.text) 


class CalcApp(App): 

    def build(self): 
     return CalcGridLayout() 


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

calc.kv

#:kivy 1.10.0 

<CalcGridLayout>: 
    id: calculator 
    display: entry #this is the display property 
    rows: 5 
    padding: 10 
    spacing: 10 

    BoxLayout: 
     TextInput: 
      id: entry #with the value of this 
      font_size: 32 
      multiline: False 

    BoxLayout: 
     spacing: 10 
     Button: 
      text: "7" 
      on_press: entry.text += self.text 
     Button: 
      text: "8" 
      on_press: entry.text += self.text 
     Button: 
      text: "9" 
      on_press: entry.text += self.text 
     Button: 
      text: "+" 
      on_press: entry.text += self.text 

    BoxLayout: 
     spacing: 10 
     Button: 
      text: "4" 
      on_press: entry.text += self.text 
     Button: 
      text: "5" 
      on_press: entry.text += self.text 
     Button: 
      text: "6" 
      on_press: entry.text += self.text 
     Button: 
      text: "-" 
      on_press: entry.text += self.text 

    BoxLayout: 
     spacing: 10 
     Button: 
      text: "1" 
      on_press: entry.text += self.text 
     Button: 
      text: "2" 
      on_press: entry.text += self.text 
     Button: 
      text: "3" 
      on_press: entry.text += self.text 
     Button: 
      text: "*" 
      on_press: entry.text += self.text 

    BoxLayout: 
     spacing: 10 
     Button: 
      text: "AC" 
      on_press: entry.text = "" 
     Button: 
      text: "0" 
      on_press: entry.text += self.text 
     Button: 
      text: "=" 
      on_press: calculator.calculate(entry.text) 
     Button: 
      text: "/" 
      on_press: entry.text += self.text 
関連する問題