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

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

Related

Popup fails depending on what I BIND

Here is the relevant portion of my Animal Picker in KIVY file shown below:
AniPic#ANIMALPICKER
id: aroot
labelText: ''
imageSource: ''
animalCode: 'AB'
animalName: 'GOGGA'
BoxLayout:
orientation: 'horizontal'
width: root.width
pos: 0,0
canvas.before:
RoundedRectangle:
pos: 5, 5
# the next two lines determine the position of the rectangle for the image
size: root.width-10, root.height-10
source: root.imageSource
radius:[10]
PinButton:
id: _pin
on_release:
root.pin_action(root.labelText)
Label:
id: _label
text: root.labelText
width: root.width
color: (1, 1, 1, 1)
MapButton:
id: _map
on_release:
root.map_show(root.labelText)
#==========================================================================
AnimalWindow:
:
id: animalid
name: 'animal'
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
Label:
id: choice
text: "Sightings"
color: 0, 0, 0, 1
size_hint_y: 0.05
canvas.before:
Color:
rgba: 0.5, 1, 0.5, 1
Rectangle:
pos: self.pos
size: self.size
ScrollView:
do_scroll_x: False
do_scroll_y: True
GridLayout:
size: (root.width, root.height)
cols: 1
rows: 9
padding: 10
spacing: 10
size_hint_x: None
size_hint_y: 9
height: self.minimum_height
row_default_height: 120
row_force_default: True
AniPic:
labelText: 'LION'
imageSource: 'images/lion_pic.jpg'
animalCode: 'LI'
AniPic:
labelText: 'CHEETAH'
imageSource: 'images/cheetah_pic.png'
animalCode: 'CH'
Now when I click on a 'PinButton' of a particular animal (CHEETA) I get the app executing
def pin_it(_animal):
print('aaaa', _animal )
and shows.
'aaaa CHEETAH'
BUT the POPUP code below, where I want 'confirmation' for pin action, does not show the popup but crashes:
class ANIMALPICKER(RelativeLayout):
def pin_action(self, _animal):
# Prepare to confirm that you want to Pin the sighting
title_text = _animal + ' ' + 'SIGHTING'
content_text = 'Confirm Sighting?'
content_box = BoxLayout(orientation='vertical')
btn_box = BoxLayout(orientation='horizontal')
btn_box.height = 24
content_label = Label()
content_label.text = content_text
yes_btn = Button(text='Yes')
yes_btn.background_color = 0, 1, 0, 1
no_btn = Button(text='No')
no_btn.background_color = 1, 0, 0, 1
btn_box.add_widget(yes_btn)
btn_box.add_widget(no_btn)
content_box.add_widget(content_label)
content_box.add_widget(btn_box)
# Now confirm that you want to Pin the sighting
popup = Popup(title=title_text,
separator_height=4,
title_size='20sp',
content=content_box,
size_hint=(None, None),
size=(200, 240),
auto_dismiss=False)
# dismiss popup and proceed to pin the sighting, on release of yes button
yes_btn.bind(on_press=pin_it(_animal))
# dismiss popup on release of the NO button
yes_btn.bind(on_release=popup.dismiss)
popup.open()
with the following message:
File "/home/sib/PycharmProjects/SpotMap/animalWindow.py", line 66, in pin_action
yes_btn.bind(on_press=pin_it(_animal))
File "kivy/_event.pyx", line 444, in kivy._event.EventDispatcher.bind
AssertionError: None is not callable
However when I bind a simple 'yes_btn.bind(on_press=dismiss)' instead of 'yes_btn.bind(on_press=pin_it(_animal))' the popup does display and the the popup dismisses when the button to which it is attached is pressed.
Please at 82 years of age and a limited knowledge of Python, Kivy and OO I desperately need help in completing this app.
A work around will also satisfy me
Many Thanks
I think the problem is that pin_it(_animal) completes the function instead of referencing the function object.
from functools import partial
myfun = partial(pin_it, animal=_animal)
# this should be a function, not function()
yes_btn.bind(on_press=myfun)

Kivy .kv file global variables puzzle

I declared some color constants as global variables in .kv file. They work in certain situations but not in others. An example ColorConstants.kv is
#:kivy 2.0.0
#:set BLUE (0, 0, 1, 1)
#:set WHITE (1, 1, 1, 1)
<Header#BoxLayout>:
# color constants don't work here: NoneType errors
# font_color: WHITE
# header_color: BLUE
# need to use numeric lists for font_color, header_color
font_color: (1, 1, 1, 1)
header_color: (0, 0, 1, 1)
header_text: ""
size_hint: 1, None
height: dp(50)
canvas.before:
Color:
rgba: self.header_color
Rectangle:
size: self.size
pos: self.pos
Label:
color: root.font_color
text: root.header_text
bold: True
BoxLayout:
orientation: "vertical"
Header:
header_text: "My Header"
Label:
# color constants work here, no errors
color: BLUE
text: "This is blue text on white"
canvas.before:
Color:
rgba: WHITE
Rectangle:
size: self.size
pos: self.pos
Accompanying ColorConstants.py:
from kivy.app import App
class ColorConstantsApp(App):
pass
ColorConstantsApp().run()
I am confused why I can use BLUE and WHITE in Label but not in Header. If I use WHITE and BLUE for font_color and header_color instead of the (1,1,1,1) and (0,0,1,1), I encounter a TypeError: 'NoneType' object is not iterable error.
It appears that the canvas instructions are being created before the header_color is assigned. You can work around that by changing:
rgba: self.header_color
to:
rgba: self.header_color if self.header_color else (0,1,0,1)
This just checks if header_color is None and uses something else in that case. Then, when header_color is assigned, the correct color is used.

Kivy. Checkbox on top of background image

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

Changing the icon of Kivy FileChooserIconView

I am looking for a way to change the file icon of FileChooserIconView and set it programatically. I am looking at Kivy's filechooser.py source code but cannot find where the icon is being set.
Currently, the default background color of FileChooserIconView is black and the current icon works well with that background. I need to change my app's background to white, and the current icon doesn't looks nice with white background and I also have a specific file icon that needs to be used for the white background.
The file icon is defined in the style.kv file of your Kivy installation. And it is referenced in the FileChooserIconView and the FileChooserIconLayout classes as:
_ENTRY_TEMPLATE = 'FileIconEntry'
You can redefine that template using something like:
Builder.load_string('''
[FileIconEntry#Widget]:
locked: False
path: ctx.path
selected: self.path in ctx.controller().selection
size_hint: None, None
on_touch_down: self.collide_point(*args[1].pos) and ctx.controller().entry_touched(self, args[1])
on_touch_up: self.collide_point(*args[1].pos) and ctx.controller().entry_released(self, args[1])
size: '100dp', '100dp'
canvas:
Color:
rgba: 1, 1, 1, 1 if self.selected else 0
BorderImage:
border: 8, 8, 8, 8
pos: root.pos
size: root.size
source: 'atlas://data/images/defaulttheme/filechooser_selected'
Image:
size: '48dp', '48dp'
source: 'atlas://data/images/defaulttheme/filechooser_%s' % ('folder' if ctx.isdir else 'file')
pos: root.x + dp(24), root.y + dp(40)
Label:
text: ctx.name
text_size: (root.width, self.height)
halign: 'center'
shorten: True
size: '100dp', '16dp'
pos: root.x, root.y + dp(16)
Label:
text: '{}'.format(ctx.get_nice_size())
font_size: '11sp'
color: .8, .8, .8, 1
size: '100dp', '16sp'
pos: root.pos
halign: 'center'
''')
The above code just duplicates the template definition from style.kv, but you can make any changes to the above template, and those changes will affect the FileChooserIconView. The Image is the actual icon.

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.

Resources