Kivy. Checkbox on top of background image - kivy

My goal is to set the checkbox on top of the image in below class. I'm struggling to find a layout in which I can position one layout above another. The only way I could think of at the moment is to show the image as the background image of RelativeLayout not as AsyncImage but I believe there is a clean way to achieve this.
Class
<FriendTile>
orientation: 'vertical'
size_hint: None, None
height: pic.height + username.height
width: pic.width
background_color: app.alert_color
active_color: app.main_bcolor
unactive_color: app.main_bcolor
canvas.before:
Color:
rgba: (0, 0, 1, 1) if self.background_color is None else self.background_color
Rectangle:
pos: self.pos
size: self.size
RelativeLayout:
id: pic
size_hint: None, None
size: 100, 100
CheckBox:
size_hint: .1, .1
pos_hint: {'top': 1, 'right': 1}
canvas.after:
Color:
rgba: (0, 1, 0, 1)
Rectangle:
pos: self.pos
size: self.size
AsyncImage:
id: image
size_hint: .9, .9
pos_hint: {'center_x': .5, 'center_y': .5}
source: root.avatar
canvas.before:
Color:
rgba: (0, 1, 1, 1) # if root.background_color is None else root.background_color
Rectangle:
pos: self.pos
size: self.size
Expectations
Reality

Related

BeReal UI in KivyMD

I'm trying to create a UI similar to BeReal's where there's a large image and then a smaller image in the top left corner of the larger image.
I've tried this solution:
MDBoxLayout:
size_hint: None, None
size: root.size
orientation: "vertical"
canvas:
Rectangle:
src: "Image1.jpg"
FitImage:
source: "Image2.jpg"
size_hint: .2, .3
pos_hint: {"center_x": .3, "center_y": .7}
I'm using the canvas to display the larger image as a background image and then adding the smaller one to the BoxLayout. This doesn't work, however, as the background image doesn't take up the entire screen and seems to be colliding with the smaller image.
Is there any way to replicate BeReals UI in Kivy?
Use a MDFloatLayout instead of a MDBoxLayout. ie.
MDFloatLayout:
FitImage:
source: 'image_cover.jpg'
pos: 0, 0
size_hint: 1, 1
FitImage:
source: 'top_left_image.jpg'
pos_hint: {'x': 0.01, 'top': 0.99}
size_hint: 0.45, 0.45
radius: 36, 36, 36, 36
Something like this?
Screen:
FitImage:
source: "Image1.jpg"
FitImage:
source: "Image2.jpg"
size_hint: .2, .3
pos_hint: {"center_x": .3, "center_y": .7}

How can I make border to the text in the label?

I'm still learning kivy language .
please can you tell me how to add a border to a text in a label in the kv file
and thanks
In the kivy language documentation, you can redefine a widget's style by adding a - to the beginning of the kv rule. So, in the kv you can define a new widget like this:
<-LabelWithBorder#Label>:
border_width: 0
border_color: [1,1,1,1]
canvas.before: # draw the border
Color:
rgba: root.border_color if root.border_width > 0 else [0,0,0,1]
Rectangle:
size: self.size
pos: self.pos
Color:
rgba: 0, 0, 0, 1
Rectangle:
size: self.width - 2*root.border_width, self.height - 2*root.border_width
pos: int(self.center_x - (self.width - 2*root.border_width)/2.), int(self.center_y - (self.height - 2*root.border_width)/2.)
canvas: # modified from Label
Color:
rgba: 1, 1, 1, 1
Rectangle:
texture: self.texture
size: self.texture_size[0] - 2*root.border_width, self.texture_size[1] - 2*root.border_width
pos: int(self.center_x - self.width/2.) + root.border_width, int(self.center_y - self.height/2.) + root.border_width
The canvas.before is the section that draws the border, and the canvas section is the normal Label style with slight modifications to account for the border.
This can be used, for example, like this:
FloatLayout:
LabelWithBorder:
text: 'Hello, World'
font_size: 50
border_width: 10
border_color: [1,0,0,1]
size_hint: None, None
size: self.texture_size
pos_hint: {'center_x':0.5, 'center_y':0.5}
I believe you are looking for 'outline'
This is the docs link for it Label Outline - Kivy Docs
Here's some example code (color defaults to black .. 0, 0, 0, 1):
Label:
id: label_StackOverflowSample
pos_hint: {"x": 0.25, "y": 0.18}
size_hint: 0.6, 0.365
font_size: 18
text_size: self.size
halign: 'left'
italic: True
outline_width: 10
outline_colour: (0, 0, 0, 1)
text:
'''
long long long
long long long
long long long
long long long
long long long
textt
'''
And this is a picture showing its example:
sorry, not enough reputation to post pictures yet
Hope this helps

Kivy. Position of GridLayout inside ScrollView

I give up. I think I have tried everything I possibly could. I need some explatanion how to position nested layouts classes. In below code I need these labels to be positioned in the center of the screen. Anything I've tried leaves the Labels on the left side.
from kivy.app import App
from kivy.lang import Builder
kv = """
<StorageLabel#Label>
background_normal: ''
size_hint: None, None
size: 65, 50
canvas.before:
Color:
rgba: (1, 0, 0.5, 1)
Rectangle:
pos: self.pos
size: self.size
halign: "left"
valign: "middle"
<Storage#BoxLayout>
ScrollView:
size_hint_x: 1
bar_width: 10
scroll_type: ['bars', 'content']
bar_color: [0, 0, 0, 1]
bar_inactive_color: [0, 0, 0, 1]
pos_hint: {'center_x': 0.5}
GridLayout:
cols: 3
size_hint_y: None
size: self.minimum_size
height: self.minimum_height
StorageLabel:
text: '1m'
StorageLabel:
text: '2m'
StorageLabel:
text: '3m'
Storage:
"""
sm = Builder.load_string(kv)
class Main(App):
def build(self):
return sm
if __name__ == '__main__':
Main().run()
The easiest way to center the Labels is to let the GridLayout size and position them. This results in a larger width for each Label, but they are centered:
from kivy.app import App
from kivy.lang import Builder
kv = """
<StorageLabel#Label>
background_normal: ''
# leave size_hint_x at default of 1
size_hint_y: None
height: 50
canvas.before:
Color:
rgba: (1, 0, 0.5, 1)
Rectangle:
pos: self.pos
size: self.size
<Storage#BoxLayout>
ScrollView:
size_hint_x: 1
bar_width: 10
scroll_type: ['bars', 'content']
bar_color: [0, 0, 0, 1]
bar_inactive_color: [0, 0, 0, 1]
pos_hint: {'center_x': 0.5}
GridLayout:
cols: 3
# add some padding and spacing
padding: 5
spacing: 5
size_hint_y: None
height: self.minimum_height
StorageLabel:
text: '1m'
StorageLabel:
text: '2m'
StorageLabel:
text: '3m'
Storage:
"""
sm = Builder.load_string(kv)
class Main(App):
def build(self):
return sm
if __name__ == '__main__':
Main().run()
If you want the labels to be your original size, then you can place each in its own Layout (perhaps a FloatLayout), and let the GridLayout size and position those Layouts:
from kivy.app import App
from kivy.lang import Builder
kv = """
<StorageLabel#Label>
background_normal: ''
size_hint: None, None
size: 65, 50
# position the label in the center of its FloatLayout
pos_hint: {'center_x':0.5}
canvas.before:
Color:
rgba: (1, 0, 0.5, 1)
Rectangle:
pos: self.pos
size: self.size
<Storage#BoxLayout>
ScrollView:
size_hint_x: 1
bar_width: 10
scroll_type: ['bars', 'content']
bar_color: [0, 0, 0, 1]
bar_inactive_color: [0, 0, 0, 1]
pos_hint: {'center_x': 0.5}
GridLayout:
cols: 3
size_hint_y: None
height: self.minimum_height
# set the default row height to the height of the Labels
row_default_height: 50
FloatLayout:
StorageLabel:
text: '1m'
FloatLayout:
StorageLabel:
text: '2m'
FloatLayout:
StorageLabel:
text: '3m'
Storage:
"""
sm = Builder.load_string(kv)
class Main(App):
def build(self):
return sm
if __name__ == '__main__':
Main().run()

Kivy TextInput Text Overflow. Cursor Outside TextInput Viewport

The last letter is overflowing
I'm using Kivy 1.10.1. I've added a text input (multiline = False). The sentence entered above is "This is a long sentence being typed into Kivy". All the letters have been entered. But the last letter - the "y" is outside the text input.
I've tried changing the padding size, the border size and changing the size of the textinput itself. I don't know if I'm doing something wrong or it is a bug with the library.
Edit:( I should have added the widget code here )
text_input: input_text_field
canvas:
Color:
rgba: self.background_color_intensity_red, self.background_color_intensity_green, self.background_color_intensity_blue, 1
Rectangle:
size: 396, 768
pos: self.pos
source: "sprites/phone.png"
BoxLayout:
size: 354, 598
pos: 21, 96
orientation: 'vertical'
BoxLayout:
size_hint: 1, 0.1
pos: root.pos
orientation: 'horizontal'
Image:
size_hint: 0.15, 1
source: "sprites/profile_pic.png"
TextInput:
id: input_text_field
size_hint: 0.85, 0.5
pos_hint: {'top': 0.75}
background_color: 1,1,1,1
border: (20, 20, 20, 20)
multiline: False
halign: 'center'
focus: True
on_text_validate: self.parent.parent.parent.on_text_input()
BoxLayout:
size_hint: 1, 0.9
pos: root.pos
orientation: 'vertical'
Thank you.

Kivy: TextInput border radius

I would like to know if it was possible in python & Kivy, to customize TextInput widgets like in HTML/CSS. Is there any way to do that directly into my CustomTextInput(TextInput) class ?
I want my TextInput looks like this:
Of course you can, but it's a bit more complicated in kivy:
test.kv:
#:import C kivy.utils.get_color_from_hex
<MyTextInput#TextInput>:
font_size: '14dp'
background_color: 0,0,0,0
cursor_color: C('#ffffff')
canvas.before:
Color:
rgba: C('#ffffff')
canvas.after:
Color:
rgb: C('#0f192e')
Ellipse:
angle_start:180
angle_end:360
pos:(self.pos[0] - self.size[1]/2.0, self.pos[1])
size: (self.size[1], self.size[1])
Ellipse:
angle_start:360
angle_end:540
pos: (self.size[0] + self.pos[0] - self.size[1]/2.0, self.pos[1])
size: (self.size[1], self.size[1])
Color:
rgba: C('#3f92db')
Line:
points: self.pos[0] , self.pos[1], self.pos[0] + self.size[0], self.pos[1]
Line:
points: self.pos[0], self.pos[1] + self.size[1], self.pos[0] + self.size[0], self.pos[1] + self.size[1]
Line:
ellipse: self.pos[0] - self.size[1]/2.0, self.pos[1], self.size[1], self.size[1], 180, 360
Line:
ellipse: self.size[0] + self.pos[0] - self.size[1]/2.0, self.pos[1], self.size[1], self.size[1], 360, 540
BoxLayout:
orientation:'vertical'
BoxLayout:
size_hint_y: 20
canvas.before:
Color:
rgba: C('#3f92db')
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
orientation: 'vertical'
size_hint_y: 80
canvas:
Color:
rgba: C('#18294a')
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
size_hint_y: 10
BoxLayout:
size_hint_y: 20
orientation:'vertical'
Label:
size_hint_y: 8
text: 'Password'
color: C('#ffffff')
font_size:'20dp'
padding: 5,5
text_size: self.size
halign: 'center'
valign: 'center'
BoxLayout:
size_hint_y: 8
BoxLayout:
size_hint_x: 25
BoxLayout:
size_hint_x: 50
canvas.before:
Color:
rgba: C('#0f192e')
Rectangle:
pos: self.pos
size: self.size
MyTextInput:
BoxLayout:
size_hint_x: 25
BoxLayout:
size_hint_y: 10
BoxLayout:
size_hint_y: 20
orientation:'vertical'
Label:
size_hint_y: 8
text: 'Confirm'
color: C('#ffffff')
font_size:'20dp'
padding: 5,5
text_size: self.size
halign: 'center'
valign: 'center'
BoxLayout:
size_hint_y: 8
BoxLayout:
size_hint_x: 25
BoxLayout:
size_hint_x: 50
canvas.before:
Color:
rgba: C('#0f192e')
Rectangle:
pos: self.pos
size: self.size
MyTextInput:
BoxLayout:
size_hint_x: 25
BoxLayout:
size_hint_y: 40
python file:
from kivy.app import App
class TestApp(App):
pass
if __name__ == '__main__':
TestApp().run()
output:

Resources