2017-03-23 3 views
0

Ich habe eine Python-Variable val, die basierend darauf aktualisiert wird, wenn eine Position in der Zeichenfläche gedrückt wird, und ich habe Probleme zu verstehen, wie der Wert in der KV-Datei aktualisiert wird.Kivy - numerische Eigenschaft in KV-Datei aktualisieren on_touch_down

Ich denke, ich brauche eine Art von Bindungsereignis, aber ich bin mir nicht sicher, wie ich das machen soll. Kann jemand eine Lösung vorschlagen?

Minimal Arbeitsbeispiel (wenn Sie in der Nähe der Spitze des Schiebers auf der linken Seite berühren, möchte ich es auf die andere Seite zu springen)


main.py

import kivy 
from kivy.config import Config 
kivy.require('1.9.1') 
Config.set('graphics', 'resizable', 1) 
Config.set('graphics', 'width', '400') 
Config.set('graphics', 'height', '400') 

from kivy.app import App 
from test import TestWidget 

class TestApp(App): 

    def build(self): 
     return TestWidget() 

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

test.py

from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.relativelayout import RelativeLayout 
from kivy.properties import NumericProperty 

class TestWidget(RelativeLayout): 

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

     Builder.load_file('test.kv') 

     sm = ScreenManager() 
     sm.add_widget(MainScreen(name='MainScreen')) 
     self.add_widget(sm) 

class MainScreen(Screen): 
    lineX = 395/2 
    lineY = 405/2 
    circleRad = 400/2 
    val = NumericProperty(2500) 

    def on_touch_down(self, touch): 
     # left 
     if 50 <= touch.x <= 75 and 195 <= touch.y <= 210: 
      val = 2500 
      print val 

     # right 
     elif 320 <= touch.x <= 350 and 200 <= touch.y <= 215: 
      val = 7500 
      print val 

test.kv

#:import math math 

<MainScreen>: 
    FloatLayout: 
     canvas: 
      Line: 
       points: [root.lineX, root.lineY, root.lineX +.75 *(root.circleRad *math.sin((math.pi /5000 *(root.val)) +math.pi)), root.lineY +.75 *(root.circleRad *math.cos((math.pi /5000 *(root.val)) +math.pi))] 
       width: 2 

Antwort

1

Sie ändern nicht val Eigenschaft MainScreen, Sie sind eine lokale Variable val in Ihre if-Klausel genannt erklärt. Verwenden Sie einfach self.val anstelle von val

if 50 <= touch.x <= 75 and 195 <= touch.y <= 210: 
     self.val = 2500 
     print self.val 

    # right 
    elif 320 <= touch.x <= 350 and 200 <= touch.y <= 215: 
     self.val = 7500 
     print self.val 
Verwandte Themen