2017-01-31 20 views
1

実行中にキャンバスの色を変更したいと思います。実行中にKivyでキャンバスの色を変更する

キャンバスの色は次のようになります。

  • len(inputtext)%3 == 0
  • グリーン私はcolor()メソッドを記述する方法がわからないlen(inputtext)%3 == 1
  • len(inputtext)%3 == 2

場合場合場合以下のコードで:

kv =""" 
RootWidget: 
    orientation: 'vertical' 

    TextInput: 
     id: my_id 
     text: 'text' 
     on_text: root.color() 

    Label: 
     id: my_Label 
     text: ' ' 
     canvas.before: 
      Color: 
       rgb: (1., 1., 0.) 
      Rectangle: 
       size: self.size 
       pos: self.pos 
""" 

import kivy 
kivy.require('1.8.0') 

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

class RootWidget(BoxLayout): 

    def __init__(self): 
     super().__init__() 

    def color(self): 
     pass # <-- here 

class TestApp(App): 
    def build(self): 
     return Builder.load_string(kv) 

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

ようこそスタックオーバーフロー!あなたの質問を編集して、コードの書式を簡単に見つけることができました。 – bfontaine

答えて

2

解決方法は次のとおりです。ラベルに色を定義する属性(kv内)を追加するだけです。次にcolorの方法では、この属性を適切に設定してください。

import kivy 
kivy.require('1.8.0') 

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



kv = """ 
RootWidget: 
    orientation: 'vertical' 

    TextInput: 
     id: my_id 
     text: 'text' 
     on_text: root.color(self.text) 

    Label: 
     id: my_Label 
     col: (1., 1., 0.) 
     text: ' ' 
     canvas.before: 
      Color: 
       rgb: self.col 
      Rectangle: 
       size: self.size 
       pos: self.pos 
""" 

class RootWidget(BoxLayout): 

    def __init__(self, **kwargs): 
     super().__init__(**kwargs) 

    def color(self, inputtext): 
     if len(inputtext)%3 == 0: 
      col = (1,0,0) 
     elif len(inputtext)%3 == 1: 
      col = (0,1,0) 
     else: 
      col = (0,0,1) 
     self.ids.my_Label.col = col 

class TestApp(App): 
    def build(self): 
     return Builder.load_string(kv) 

if __name__ == '__main__': 
    TestApp().run() 
関連する問題