2015-11-08 64 views
0

複選框狀態我想從Python代碼我如何初始化在kivy

我試圖初始化在kivy CheckBox的值(見例),但它不工作。任何人都可以幫忙嗎?

import kivy 
from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import BooleanProperty 
from kivy.properties import StringProperty 

class MainScreen(BoxLayout): 
    BlueText = StringProperty() 
    Blue = BooleanProperty() 
    Red = BooleanProperty() 
    UseColours = BooleanProperty() 

    def __init__(self, **kwargs): 
    super(MainScreen, self).__init__(**kwargs) 
    self.BlueText='Blue' 
    self.UseColours=True 
    self.Blue=False 
    self.Red=True 

    def doBlue(self,*args): 
    pass 

    def doRed(self,*args): 
    pass 

    def doUseColours(self,*args): 
    pass 

class BasicApp(App): 
    def build(self): 
     return MainScreen() 

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

我的kv文件試圖通過設置'value'來檢查框是否被檢查。這是正確的嗎?

MainScreen: 

<MainScreen>: 
    orientation: "vertical" 
    GridLayout: 
     cols: 2 
     Label: 
      text: root.BlueText 
     CheckBox: 
      group: 'colours' 
      value: root.Blue 
      on_active: root.doBlue(*args) 
     Label: 
      text: "Red" 
     CheckBox: 
      group: 'colours' 
      value: root.Red 
      on_active: root.doRed(*args) 
     Label: 
      text: "Use colours" 
     CheckBox: 
      value: root.UseColours 
      on_active: root.doUseColours(*args) 

回答

0

使用active屬性:

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.lang import Builder 

Builder.load_string(''' 
<MyWidget>: 
    CheckBox: 
     active: False 
    CheckBox: 
     active: True 
''') 

class MyWidget(BoxLayout): 
    pass 

class MyApp(App): 
    def build(self): 
     return MyWidget() 

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

Python代碼:

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import BooleanProperty 
from kivy.lang import Builder 

Builder.load_string(''' 
<MyWidget>: 
    CheckBox: 
     active: root.is_active 
    CheckBox: 
     active: not root.is_active 
    Button: 
     text: 'toggle' 
     on_press: root.toggle() 
''') 

class MyWidget(BoxLayout): 
    is_active = BooleanProperty(False) 

    def toggle(self): 
     self.is_active = not self.is_active 

class MyApp(App): 
    def build(self): 
     return MyWidget() 

if __name__ == '__main__': 
    MyApp().run() 
+0

感謝Nykakin。我在文檔中查找並沒有發現。我應該在哪裏看? – Psionman

+0

它在這裏:http://kivy.org/docs/api-kivy.uix.checkbox.html#kivy.uix.checkbox.CheckBox.active – Nykakin

+0

謝謝。是的,我讀過,但困惑主動爲啓用:( – Psionman