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:
Related
I have a boxlayout (1) of two labels placed next to each other. I use this widget to build to another boxlayout containing a list of the widgets of (1).
For readability, I like to have each line separated by a thin line and I also like to have a vertical line to separate two labels.
something similar to:
Label 1 content
label 2 content
Label 1 content 1
label 2 content 2
:----------------:
:-----------------:
etc. And alternate the back ground color of each line.
Could I get some help in how to do this, please.
Many thanks.
You can define a background color, then define Labels that obscure that background color except for a small area around the Labels. Here is a kv file that does that:
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
spacing: 2
padding: 2
# define background that will appear as outlines
canvas.before:
Color:
rgba: 1,0,0,1 # outline color
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
spacing: 2
size_hint_y: None
height: self.minimum_height
LabelNotTransparent:
text: 'Label 1 content'
size_hint_y: None
height: self.texture_size[1]
LabelNotTransparent:
text: 'Label 2 content'
size_hint_y: None
height: self.texture_size[1]
BoxLayout:
spacing: 2
size_hint_y: None
height: self.minimum_height
LabelNotTransparent:
text: 'Label 1 content 1'
size_hint_y: None
height: self.texture_size[1]
LabelNotTransparent:
text: 'Label 2 content 2'
size_hint_y: None
height: self.texture_size[1]
BoxLayout:
spacing: 2
size_hint_y: None
height: self.minimum_height
LabelNotTransparent:
text: ':----------------:'
size_hint_y: None
height: self.texture_size[1]
LabelNotTransparent:
text: ':----------------:'
size_hint_y: None
height: self.texture_size[1]
<LabelNotTransparent#Label>:
# normal Labels are transparent, this makes it not transparent
canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size
The spacing and padding values are the areas that will show through and appear as outlines.
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
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
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()
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.