2017-06-15 46 views
0

如何將標籤的文本立即更改爲滑塊的值。如何顯示滑塊值,因爲它在Python中變化kivy

這是我當前的代碼

import kivy 
kivy.require('1.10.0') 
from kivy.app import App 
from kivy.uix.slider import Slider 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.label import Label`` 

class app(App): 

    def build(self): 
     layout = BoxLayout(orientation='vertical') 
     slide = Slider(min=-100, max=100,value=0) 
     label = Label(text=str(slide.value)) 
     layout.add_widget(slide) 
     layout.add_widget(label) 
     return layout 
app().run() 

我想在python

+0

如果下面的答案有效,請將其標記爲已接受。謝謝! – Mox

回答

0

你需要創建一個Kivy Property和滑塊值綁定到一個ON_事件,然後可在更新標籤的文本答案。以下是一種方法:

from kivy.app import App 
from kivy.uix.slider import Slider 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.label import Label 
from kivy.properties import NumericProperty 
import kivy 
kivy.require('1.10.0') 


class Container(BoxLayout): 

    slider_val = NumericProperty(0) 

    def __init__(self, *args, **kwargs): 
     super(Container, self).__init__(*args, **kwargs) 
     self.orientation = 'vertical' 

     slide = Slider(min=-100, max=100, value=0) 
     slide.fbind('value', self.on_slider_val) 

     self.label = Label(text=str(self.slider_val)) 

     self.add_widget(slide) 
     self.add_widget(self.label) 

    def on_slider_val(self, instance, val): 
     self.label.text = str(val) 


class app(App): 

    def build(self): 
     return Container() 


app().run()