2013-07-02 79 views
0

我對Kivy(昨天開始)頗爲陌生,並且試圖創建一個足夠簡單的應用程序,該應用程序具有用於計算體積的多個高度和麪積值的輸入框。我找不到任何這樣做的工作方法。到目前爲止,所有我有是這樣的:Kivy簡單計算的輸入值

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


Builder.load_string(""" 
<MenuScreen>: 
    FloatLayout: 

     Label: 
      text: 'Please Select an Area to Work With:' 
      pos: 230, 490 
      size_hint: .15, .05 
      font_size: 23 



     Button: 
      text: "A" 
      pos: 230, 100 
      size_hint: .4,.1 
      font_size: 23 
      on_press: root.manager.current = 'settings' 


     Button: 
      text: "B" 
      pos: 230, 210 
      size_hint: .4,.1 
      font_size: 23 
      on_press: root.manager.current = 'settings' 


     Button: 
      text: "C" 
      pos: 230, 320 
      size_hint: .4,.1 
      font_size: 23 
      on_press: root.manager.current = 'settings' 



     Button: 
      text: "D" 
      pos: 230, 420 
      size_hint: .4,.1 
      font_size: 23 
      on_press: root.manager.current = 'settings' 






<SettingsScreen>: 
    GridLayout: 
     Label: 
      text: 'Room 1' 
      pos: 6, 460 
      size_hint: .15, .05 
      font_size: 23 

     Label: 
      text: 'Room 2' 
      pos: 6, 420 
      size_hint: .15, .05 
      font_size: 23 

     Label: 
      text: 'Room 3' 
      pos: 6, 380 
      size_hint: .15, .05 
      font_size: 23 

     Label: 
      text: 'Room 4' 
      pos: 6, 340 
      size_hint: .15, .05 
      font_size: 23 

     Label: 
      text: 'Room 5' 
      pos: 6, 300 
      size_hint: .15, .05 
      font_size: 23 

     Label: 
      text: 'Room 6' 
      pos: 6, 260 
      size_hint: .15, .05 
      font_size: 23 

     TextInput: 
      text1: "0" 
      multiline: False 
      pos: 200,420 
      font_size: 23 
      on_text: viewer.text = self.text1 
      size_hint: .001, .001 

     TextInput: 
      text2: "0" 
      multiline: False 
      pos: 200, 420 
      font_size: 23 
      on_text: viewer.text = self.text2 
      size_hint: .001, .001 

     TextInput: 
      text3: "0" 
      multiline: False 
      pos: 200,380 
      font_size: 23 
      on_text: viewer.text = self.text3 
      size_hint: .001, .001 

     TextInput: 
      text4: "0" 
      multiline: False 
      pos: 200,340 
      font_size: 23 
      on_text: viewer.text = self.text4 
      size_hint: .001, .001 


     TextInput: 
      text5: "0" 
      multiline: False 
      pos: 200,300 
      font_size: 23 
      on_text: viewer.text = self.text5 
      size_hint: .001, .001 

     TextInput: 
      text6: "0" 
      multiline: False 
      pos: 200,240 
      font_size: 23 
      on_text: viewer.text = self.text6 
      size_hint: .001, .001 
""") 

# Declare both screen 
class MenuScreen(Screen): 
    pass 

class SettingsScreen(Screen): 
    pass 

# Create the screen manager 
sm = ScreenManager() 
sm.add_widget(MenuScreen(name='menu')) 
sm.add_widget(SettingsScreen(name='settings')) 

class TestApp(App): 

    def build(self): 
     return sm 

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

林規劃有獨特的每個按下按鈕的第二頁,但想任何幫助,將不勝感激。

+0

你有什麼具體問題?你是否看到某種錯誤? – andersschuller

回答

1

我希望我能理解你的問題。你正在要求的方法來做到這一點。簡單的答案是你有整個Python來完成你想要的所有操作。 Kivy是一個提供大量組件來設計GUI的庫。它還爲您提供了一個由Builder.load_string()解析的語言。這裏有一個例子可能或多或少是你正在尋找的。它是第一個屏幕上的計算器。第二個屏幕是空的,您可以使用底部按鈕在它們之間移動。

第一個屏幕上的計算器有兩個InputTexts和兩個按鈕(SumProduct)。 Sum Button具有直接在kivy語言上執行的總和。產品按鈕在根中調用一個方法(Calc的一個實例)。該方法本身不存在。我在kivy部分下面的python代碼中創建。對於我所說的代碼有一些評論。

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.floatlayout import FloatLayout 

Builder.load_string(""" 
<Calc>: 
    # This are attributes of the class Calc now 
    a: _a 
    b: _b 
    result: _result 
    AnchorLayout: 
     anchor_x: 'center' 
     anchor_y: 'top' 
     ScreenManager: 
      size_hint: 1, .9 
      id: _screen_manager 
      Screen: 
       name: 'screen1' 
       GridLayout: 
        cols:1 
        TextInput: 
         id: _a 
         text: '3' 
        TextInput: 
         id: _b 
         text: '5' 
        Label: 
         id: _result 
        Button: 
         text: 'sum' 
         # You can do the opertion directly 
         on_press: _result.text = str(int(_a.text) + int(_b.text)) 
        Button: 
         text: 'product' 
         # Or you can call a method from the root class (instance of calc) 
         on_press: root.product(*args) 
      Screen: 
       name: 'screen2' 
       Label: 
        text: 'The second screen' 
    AnchorLayout: 
     anchor_x: 'center' 
     anchor_y: 'bottom' 
     BoxLayout: 
      orientation: 'horizontal' 
      size_hint: 1, .1 
      Button: 
       text: 'Go to Screen 1' 
       on_press: _screen_manager.current = 'screen1' 
      Button: 
       text: 'Go to Screen 2' 
       on_press: _screen_manager.current = 'screen2'""") 

class Calc(FloatLayout): 
    # define the multiplication of a function 
    def product(self, instance): 
     # self.result, self.a and self.b where defined explicitely in the kv 
     self.result.text = str(int(self.a.text) * int(self.b.text)) 

class TestApp(App): 
    def build(self): 
     return Calc() 

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