The key is to use a kivy property to change the value. This will automatically create a binding that updates the value of the radius when the property changes. You do not get this behavior with a python variable. Below is an interactive example. Adjust the slider to set the radius.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.properties import NumericProperty
kv = """
BoxLayout:
orientation: 'vertical'
AnchorLayout:
RRLabel:
id: rrlabel
text: 'Rounded Rectangle'
size_hint: None, None
size: 200, 200
color: 'black'
canvas.before:
Color:
rgba: 1, 1, 1, 1
RoundedRectangle:
size: self.size
pos: self.pos
radius: [self.radius] * 4
Slider:
size_hint_y: None
height: 50
max: 100
on_value: rrlabel.radius = self.value
Label:
size_hint_y: None
height: 50
text: f'The radius value = {rrlabel.radius:0.2f}'
"""
class RRLabel(Label):
radius = NumericProperty()
class RRApp(App):
def build(self):
return Builder.load_string(kv)
RRApp().run()
--
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/15b031be-8935-4be6-b637-ea46a9da5fefn%40googlegroups.com.
Side question: Getting the size of the screen in kivy always confuse me. Previously I was able to get the right size of the screen during on_enter. But while preparing this testing code, on_enter I still get just 100 as width size. only when on_size triggers for second time (also I don't know why it triggers twice), I get the correct size of the screen.
The default size for a widget is 100 x 100. You are seeing the initial value of the screen, then when the layout sets the size of the Screen size, you are seeing it get set by the Layout.
From: Barbarur
Sent: Monday, August 1, 2022 8:10 AM
To: Kivy users support
Subject: [kivy-users] Change dinamically the radius of a Rounded Rectangle
Hello everyone,
--
Share your code. I don’t know why your seeing the object rather than the value.
When you refer to properties across screens you want to access them using the screenmanager method get_screen(). Read: https://kivy.org/doc/stable/api-kivy.uix.screenmanager.html?highlight=get_screen#kivy.uix.screenmanager.ScreenManager.get_screen
In python to access one screens attributes from another:
self.manager.get_screen(‘other_screen’).other_screens_property
in kv, doing this from a kivy rule with a Screen as the root, would be similar:
root.manager.get_screen(‘other_screen’).other_sceeens_property
manager is an attribute of the Screen, it holds the ScreenManager the screen is under.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/659c3c57-f1ad-4e17-b2e9-67eb0a3d2329n%40googlegroups.com.