- Kivy Tutorial
- Kivy - Home
- Kivy Basics
- Kivy - Getting Started
- Kivy - Installation
- Kivy - Architecture
- Kivy - File Syntax
- Kivy - Applications
- Kivy - Hello World
- Kivy - App Life Cycle
- Kivy - Events
- Kivy - Properties
- Kivy - Inputs
- Kivy - Behaviors
- Kivy Buttons
- Kivy - Buttons
- Kivy - Button Events
- Kivy - Button Colors
- Kivy - Button Size
- Kivy - Button Position
- Kivy - Round Buttons
- Kivy - Disabled Buttons
- Kivy - Image Button
- Kivy Widgets
- Kivy - Widgets
- Kivy - Label
- Kivy - Text Input
- Kivy - Canvas
- Kivy - Line
- Kivy - Checkbox
- Kivy - Dropdown List
- Kivy - Windows
- Kivy - ScrollView
- Kivy - Carousel
- Kivy - Slider
- Kivy - Images
- Kivy - Popup
- Kivy - Switch
- Kivy - Spinner
- Kivy - Splitter
- Kivy - Progress Bar
- Kivy - Bubble
- Kivy - Tabbed Panel
- Kivy - Scatter
- Kivy - Accordion
- Kivy - File Chooser
- Kivy - Color Picker
- Kivy - Code Input
- Kivy - Modal View
- Kivy - Toggle Button
- Kivy - Camera
- Kivy - Tree View
- Kivy - reStructuredText
- Kivy - Action Bar
- Kivy - Video Player
- Kivy - Stencil View
- Kivy - VKeyboard
- Kivy - Touch Ripple
- Kivy - Audio
- Kivy - Videos
- Kivy - Spelling
- Kivy - Effects
- Kivy - Input Recorder
- Kivy - OpenGL
- Kivy - Text
- Kivy - Text Markup
- Kivy - Settings
- Kivy Layouts
- Kivy - Layouts
- Kivy - Float Layout
- Kivy - Grid Layouts
- Kivy - Box Layouts
- Kivy - Stack Layout
- Kivy - Anchor Layout
- Kivy - Relative Layout
- Kivy - Page Layout
- Kivy - Recycle Layout
- Kivy - Layouts in Layouts
- Kivy Advanced Concepts
- Kivy - Configuration Object
- Kivy - Atlas
- Kivy - Data Loader
- Kivy - Cache Manager
- Kivy - Console
- Kivy - Animation
- Kivy - Multistroke
- Kivy - Clock
- Kivy - SVGs
- Kivy - UrlRequest
- Kivy - Clipboard
- Kivy - Factory
- Kivy - Gesture
- Kivy - Language
- Kivy - Graphics
- Kivy - Drawing
- Kivy - Packaging
- Kivy - Garden
- Kivy - Storage
- Kivy - Vector
- Kivy - Utils
- Kivy - Inspector
- Kivy - Tools
- Kivy - Logger
- Kivy - Framebuffer
- Kivy Applications and Projects
- Kivy - Drawing App
- Kivy - Calculator App
- Kivy - Stopwatch App
- Kivy - Camera Handling
- Kivy - Image Viewer
- Kivy - Bezier
- Kivy - Canvas Stress
- Kivy - Circle Drawing
- Kivy - Widget Animation
- Kivy - Miscellaneous
- Kivy Useful Resources
- Kivy - Quick Guide
- Kivy - Useful Resources
- Kivy - Discussion
Kivy - Text
The "kivy.core.text" module in Kivy library acts as a backend layer for rendering text. However, it should be used only if the "kivy.uix.label.Label" widget is not giving satisfactory result.
This module has two important uses: one is to derive the texture from the core Label object and apply it to any widget in the application, and second is apply custom fonts to the text property of widgets like label, button or Text input (or any widget with text property)
Using Texture
The Label widget (in "kivy.uix.label" module) is often not efficient in loading in the memory. To overcome this, the approach is to have an object of core.label class, and use its texture with the conventional widget.
First import the Label class from "kivy.core.label" (to avoid confusion, name it as CoreLabel).
from kivy.core.text import Label as CoreLabel cl=CoreLabel(text="Hi there!", font_size=50, color=(1, 0, 0, 1))
Call the refresh() method on this object to compute things and generate the texture.
cl.refresh()
Obtain the texture and the texture size.
texture = cl.texture texture_size = list(texture.size)
This can now be used with any widget now.
Example
In the following code, we add a label widget to a vertical box layout and use the texture of a core level object with it.
from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.graphics import * from kivy.core.text import Label as CoreLabel from kivy.uix.label import Label from kivy.uix.widget import Widget from kivy.core.window import Window Window.size = (720,400) class CoreLabelApp(App): def build(self): cl=CoreLabel(text="Hi there!", font_size=50, color=(1, 0, 0, 1)) cl.refresh() texture = cl.texture main=BoxLayout(orientation='vertical') l=Label() texture_size = list(texture.size) l.canvas.add(Rectangle(texture=texture, size=texture_size)) main.add_widget(l) return main CoreLabelApp().run()
Output
It will produce the following output −
Custom Fonts
All the widgets that have a text property are rendered with a default set of fonts as per the settings in the config.ini file of Kivy installation.
default_font = ['Roboto', 'data/fonts/Roboto-Regular.ttf', 'data/fonts/Roboto-Italic.ttf', 'data/fonts/Roboto-Bold.ttf', 'data/fonts/Roboto-BoldItalic.ttf']
However, you may want to use a specific font in which a text on any widget, it may be a label, or a TextInput box or a button. To do that, you need to download the relevant font file (The True Type Fonts are represented by .ttf files) and place it in the application folder.
To make these fonts available for our application, they must be registered with the application. The register() method of the LabelBase class is invoked for the purpose. LabelBase is an abstract class used by the specific font renderer used by your operating system.
The register() is a static method with following parameters −
register(name, fn_regular, fn_italic=None, fn_bold=None, fn_bolditalic=None)
where "name" is the font name with which you will refer the font in your program, and "fn_regular" parameter is the TTF file of the font.
Download the TTF files for Consolas font and Monotype Corsiva font and store them in the application folder.
Example
In the following program, we are showing the same string in three text input boxes. The first box renders the text in the default font. The second box uses Monotype Corsiva and the third box applies the Consolas font.
from kivy.app import App from kivy.uix.gridlayout import GridLayout from kivy.uix.textinput import TextInput from kivy.core.text import LabelBase from kivy.core.window import Window Window.size = (720,400) class CustomFontApp(App): def build(self): main=GridLayout(cols=1) LabelBase.register(name='font1', fn_regular='consolas.ttf') LabelBase.register(name='font2', fn_regular='MonotypeCorsivaFont.ttf') txt='Simple Is Better Than Complicated' t1=TextInput(text=txt, halign='center', font_size=48) t2=TextInput( text=txt, halign='center', font_name='font2', font_size=48 ) t3=TextInput( text=txt, halign='center', font_name='font1', font_size=48 ) main.add_widget(t1) main.add_widget(t2) main.add_widget(t3) return main CustomFontApp().run()
Output
It will produce the following output window −
To Continue Learning Please Login
Login with Google