how to change the canvas color according to another widget value

147 views
Skip to first unread message

husam alkdary

unread,
Apr 12, 2020, 5:37:34 PM4/12/20
to Kivy users support
It's possible to make the canvas color changes according to another widget value like slider or switch or …..

Elliot Garbus

unread,
Apr 12, 2020, 6:56:10 PM4/12/20
to kivy-...@googlegroups.com
Yes. It’s quite easy. 
For example use a slider to set a kivy numeric property, put the property values in the color field of the canvas. 



Sent from my iPhone

On Apr 12, 2020, at 2:37 PM, husam alkdary <husamal...@gmail.com> wrote:


It's possible to make the canvas color changes according to another widget value like slider or switch or …..

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/fdca5c07-a447-44ee-aa2e-ebb4b10ff645%40googlegroups.com.

husam alkdary

unread,
Apr 13, 2020, 1:08:24 AM4/13/20
to Kivy users support
but I'm using kv language how to set a numerical property 


On Monday, April 13, 2020 at 12:56:10 AM UTC+2, Elliot Garbus wrote:
Yes. It’s quite easy. 
For example use a slider to set a kivy numeric property, put the property values in the color field of the canvas. 



Sent from my iPhone

On Apr 12, 2020, at 2:37 PM, husam alkdary <husamal...@gmail.com> wrote:


It's possible to make the canvas color changes according to another widget value like slider or switch or …..

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Apr 13, 2020, 1:39:59 AM4/13/20
to kivy-...@googlegroups.com

Two examples below.  Top examples create a class and explicitly declare NumericProperties.

The lower example creates kivy object properties in the kv file.

I prefer to use the NumericProperties directly.

 

 

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

kv =
"""
<ColorBox>:
    orientation: 'vertical'
    canvas:
        Color:
            rgb: root.red, root.green, root.blue
        Rectangle:
            size: self.size
            pos: self.pos
    Slider:
        max: 100
        value: 50
        step: 1
        on_value: root.red = self.value/100
    Slider:
        max: 100
        value: 50
        step: 1
        on_value: root.green = self.value/100
    Slider:
        max: 100
        value: 50
        on_value: root.blue = self.value/100

ColorBox:      
"""


class ColorBox(BoxLayout):
    red = NumericProperty(
.5)
    green = NumericProperty(
.5)
    blue = NumericProperty(
.5)


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


ColorSlidersApp().run()

 

 

 

Here is how to do it declaring red, green and blue all in kivy.

 

from kivy.app import App
from kivy.lang import Builder


kv =
"""
BoxLayout:
    orientation: 'vertical'
    red: 0.5
    green: 0.5
    blue: 0.5
    canvas:
        Color:
            rgb: root.red, root.green, root.blue
        Rectangle:
            size: self.size
            pos: self.pos
    Slider:
        max: 100
        value: 50
        step: 1
        on_value: root.red = self.value/100
    Slider:
        max: 100
        value: 50
        step: 1
        on_value: root.green = self.value/100
    Slider:
        max: 100
        value: 50
        on_value: root.blue = self.value/100
      
"""


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


ColorSlidersApp().run()

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/0e76cdc0-5461-45e9-bb13-43615e67ff2e%40googlegroups.com.

 

husam alkdary

unread,
Apr 13, 2020, 2:15:26 PM4/13/20
to Kivy users support
what's on_value mean I can't find it the doc also didn't work with the switch 

Elliot Garbus

unread,
Apr 13, 2020, 2:30:09 PM4/13/20
to kivy-...@googlegroups.com

Any kivy property creates an associated event.  The event is named on_property_name

If you create a property ‘question’ there will be an event created on_question.  The event will fire with the value of question is changed.

 

In a slider value is the value of the slider, it is a kivy property, and so on_value fires when the value of the slider changes.

A switch uses active, there is an on_active event that fires when the switch state is changed.

 

Read about properties: https://kivy.org/doc/stable/api-kivy.properties.html?highlight=properties#observe-using-on-propname

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/7fd2884b-f032-4eea-8474-c4d9d52aec07%40googlegroups.com.

 

husam alkdary

unread,
Apr 13, 2020, 6:15:26 PM4/13/20
to Kivy users support
if I will use numerical property in side a boxlayout that is inaide a screen inside the root widget which is the screen manager what I should replace the root in 
root.red

I tried to use
 app.root.sm.ids.get_screen('screen name').ids.boxlayout_id.red

but is not working !!

Elliot Garbus

unread,
Apr 13, 2020, 8:15:31 PM4/13/20
to kivy-...@googlegroups.com

Show the code.  It all depends on how you put the pieces together.

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63d3258c-c466-4cde-bc0e-da5bc3db246f%40googlegroups.com.

 

husam alkdary

unread,
Apr 14, 2020, 4:10:55 AM4/14/20
to Kivy users support
it's ok I just replace the root with the id of the boxlayout that contain the numerical property amd every thing works fine
thanks
Reply all
Reply to author
Forward
0 new messages