kivy.lang Builder doesn't evaluate simple expressions - kivy

While working with kivy.lang Builder I performed some simple arithmetic and it's not working, no error message even.
Builder.load_string ('''
<RootWidget>:
text: 'beautiful Flower Pics '
font_size: 50
Image:
pos: root.pos
size: root.width * 0.5, root.height
source: 'newflower.png'
allow_stretch: True
keep_ration: False
here at root.widht * 0.5 multiplication is not being done. suggestion, hint, advice?

Assuming that RootWidget is a Layout of some sort, you need to set your size_hint. size_hint defaults to 1, 1, and will override your specified sizes. If you set size_hint: None, None then this should size as expected - and depending on the specific type of Layout, you may be able to skip setting size altogether and just use size_hint: 0.5, 1 to get the same effect.

Related

Understanding KivyMD Screen Layouts

I want to understand how to layout a KivyMD screen using a GridLayout with ScrollView, and with a BottomNavigation. My code doesn't work because the GridLayout displays over the BottonNavigation which then can't be seen. What is the correct way to setup this layout? And, generally are there rule-of-thumbs for KivyMD screen layouts? Thanks
<MyScreen>:
name: 'myscreen'
ScrollView:
orientation:'vertical'
MDGridLayout:
cols: 3
adaptive_height: True
padding: dp(4), dp(4)
spacing: dp(4)
etc.
BoxLayout:
orientation:'vertical'
MDBottomNavigation:
panel_color: .2, .2, .2, 1
MDBottomNavigationItem:
name: 'left'
text: 'Left'
icon: 'chevron-left'
MDLabel:
text: 'Left'
halign: 'center'
font_style: 'Icon'
MDBottomNavigationItem:
name: 'right'
text: 'Right'
icon: 'chevron-right'
MDLabel:
text: 'Right'
halign: 'center'
font_style: 'Icon'
etc.
The Layout classes monitor the sizes of their children, and recalculates the layout and redraws if that size changes. So, I believe that if your Image size does not change, then only that Image will be updated.
The kivy Builder maintains a set of rules based on the kv files loaded, so whether those rules come from separate files doesn't really matter. I would recommend structuring your kv file(s) based on things like maintainability, readability, and logical partitioning

Transparent animated graphics in Kivy

I am new to Kivy and want to create a widge graphic image of a tank with a transparent cutout that "fills" with another graphic image of the product. How do I stack 2 graphic images, one with a transparent layer, on top of one another to get this effect? I have created this with a circle/rectangle and fill color, but I want both a custom foreground image and a custom fill image.
You can stack Image widgets by simply placing them at the same position. For example, in kv you can do:
FloatLayout:
Image:
source: 'fill.png'
size_hint: 0.25, 0.25
pos_hint: {'center_x':0.5, 'center_y':0.5}
allow_stretch: True
Image:
source: 'tank.png'
size_hint: 0.25, 0.25
pos_hint: {'center_x':0.5, 'center_y':0.5}
allow_stretch: True
This will show the tank.png with the fill.png showing through any transparent areas of tank.png.

Wrong Kivy widget positions on Surface Pro4

I am relatively new in Kivy. Having studied quite a bit on my laptop, everything works fine until i started working on my surface pro 4. All my buttons were placed in wrong positions, irrespective of how I specify the position, pos or pos_hint, eg:
the window
above is the window that appears when the following code is run:
<Button>:
color: .8, .9, 0, 1
font_size: 31
size_hint: .4, .3
<FloatLayout>
Button:
text: 'button1'
pos_hint: {'x':0, 'top': 1}
Button:
text: 'button2'
pos_hint: {'right': 1, 'y': 0}
could this be due to issues with OpenGL on surface pro 4 or could I have done something totally dumb?
After going through the documentation, I was able to locate the config.ini file on my c: drive and changed the rotation value from 90 to 0. rotation = 0 and my widgets are placed as expected. Kudos to the Kivy.org team, great documentation.

rotate entire widget, not only its canvas

In this example, we can see a rotation of the canvas. But clicking the button's corners isn't registered. How can I rotate the entire button?
You can probably override its collide_point method to account for the rotation by transforming the touch coordinates.
If you use the widget system (e.g. putting the Button in a Scatter), the collision is taken care of for you.
Based on inclement's answer, I've created a button on a scatter layout, which has its hitbox set correctly.
from kivy.app import App
from kivy.lang import Builder
kv = '''
FloatLayout:
ScatterLayout:
size_hint: None, None
size: 200, 200
pos_hint: {'center_x': .5, 'center_y': .5}
rotation: 45
do_rotation: False
do_scale: False
do_translation: False
Button:
text: 'hello world'
'''
class RotationApp(App):
def build(self):
return Builder.load_string(kv)
RotationApp().run()
I used scatter layout instead of scatter, because it passes its size to children widgets. The do_x: False aren't needed in this example, because button intercepts touching events, but if I placed a label, it would move on touch.

Prevent image stretching in Kivy canvas

The situation
I've made a Widget in Kivy in order to animate an image.
<AnimatedImage>:
pos_hint: root.pos_hint
size: root.size
size_hint: .3, .3
canvas:
Rectangle:
source: 'images/image.png'
pos: self.pos[0], self.pos[1]
size: root.size[0], 435. / 770 * root.size[0]
As you can see in the last line, I used the image ratio to calculate the rectangle size.
The problem
I would like to load different images and size the widget in adjustment.
What didn't work
Using Image instead of Widget. I was not able to animate the Image size_hint using Animation object.
Thanks!

Resources