Transparent animated graphics in Kivy - 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.

Related

How to remove outer layer margin from MDTextField?

I want to have two MDTextFields touching each other at their bottom and top border
The MDTextField has an outer layer which prevents them from touching each other. Is there a property to remove this margin? Margins I want to remove(black rectangles):
MDTextfields colored to show the margins
Lines I want to touch:
Arrows pointing at parts of borders I want to merge together
BoxLayout:
orientation:"vertical"
size_hint: 1, None
height:textfield1.height+ textfield2.height
MDTextField:
id:textfield1
hint_text: "Rectangle mode"
mode: "rectangle"
MDTextField:
id:textfield2
hint_text: "Rectangle mode"
mode: "rectangle"
Final product would be something like this:
Here
Thank you!
I can't comment due to rep. This worked for me:
spacing:"-10dp" in BoxLayout

Inverted colours in Kivy ToggleButtons

Have a set of ToggleButtons where the general foreground colour is green and the general background colour is black. Then I use
background_down: 'btn_prs_grn.png'
on_state: self.color = [0,0,0,1] if self.state == 'down' else [0,1,0,1]
To make (I thought) the active button black-on-green, but what I get is more like slightly-darker-green-on-normal-green.
I assume this is because the button (label) colours are blended/tinted/whatever-it's-called together.
Edit: here is a photo of the buttons.
Left button normal, right button down. It is actually possible to make out the text "White" on the right button, in a slightly darker shade of green than the normal one.
What is the easiest way to achieve black-on-green? I understand I can draw on the canvas myself but is there a simpler method?
Output - ToggleButton
Button
In kv file, set background_normal: ''
background_color
background_color
Background color, in the format (r, g, b, a).
This acts as a multiplier to the texture colour. The default
texture is grey, so just setting the background color will give a
darker result. To set a plain color, set the background_normal to
''.
The background_color is a ListProperty and defaults to [1, 1, 1, 1].
background_normal
background_normal
Background image of the button used for the default graphical
representation when the button is not pressed.
background_normal is a StringProperty and defaults to
‘atlas://data/images/defaulttheme/button’.

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!

kivy.lang Builder doesn't evaluate simple expressions

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.

Resources