hk22
hk22

Reputation: 49

How to show languange specific characters on Kivy like Ä?

Im getting empty box character instead of Ä character. How to make them show on my kivy application. I read some posts suggesting changing font type to some which support these characters. i tried few but it didnt seem solving it.

Tried pretty basic Calibri font and also just randomly picked font which was said to be suitable with my languange( AlegreyaSansSC)

Python file:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty, BooleanProperty


class Row(BoxLayout):
    x1 = StringProperty('')
    x2 = StringProperty('')
    x3 = BooleanProperty(False)

    def __init__(self, x1, x2, x3, **kwargs):
        super(Row, self).__init__(**kwargs)
        self.x1 = x1
        self.x2 = x2
        self.x3 = x3

class MyPopup(Popup):
    pass


class MainScreen(Screen):
    pass

class SecondScreen(Screen):
    def fire_popup(self):
        pops = MyPopup()
        pops.open()

class ScreenManagement(ScreenManager):
    def changescreen(self, value):

        try:
            if value !='main':
                self.current = value
        except:
            print('No Screen named'+ value)




class testiApp(App):
    def build(self):
        self.title = 'Hello'

    def add_more(self, x1, x2, x3):
        addbutton = self.root.get_screen('Page2').ids.empty
        addbutton.add_widget(Row(x1, x2, x3))

    def remove(self):
        container = self.root.get_screen('Page2').ids.empty
        if len(container.children) > 0:
            container.remove_widget(container.children[0])

testiApp().run()

KV. file:

<MyPopup>:
    id:pop
    size_hint: .4, .4
    auto_dismiss: False
    title: 'XXX!!'
    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            orientation:'horizontal'
            Label:
                text:'X1'
            TextInput:
                id: X1

            Label:
                text:'X2'
            TextInput:
                id:X2

            CheckBox:
                id:X3

        BoxLayout:
            orientation:'horizontal'
            Button:
                text:'Lisää'
                on_release: app.add_more(X1.text, X2.text, X3.active)
            Button:
                text: 'Close'
                on_press: pop.dismiss()

<Row>:
    x1:''
    x2:''
    x3:False

    Label:
        text: root.x1
    Label:
        text: root.x2
    CheckBox:
        active: root.x3


ScreenManagement:
    MainScreen:
        name:'Main'
    SecondScreen:
        name:'Page2'



<MainScreen>:
    name:'Main'
    BoxLayout:
        Button:
            text:'Next Page'
            on_release: app.root.current ='Page2'



<SecondScreen>:
    name:'Page2'


    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            orientation:'vertical'
            Label:
                text:'Popup Test'
            ScrollView:
                bar_width: 5
                bar_color: 1,0,0,1 #red
                bar_inactive_color: 0,0,1,1 #blue
                effect_cls: 'ScrollEffect'
                scroll_type:['bars','content']
                GridLayout:
                    orientation: "vertical"
                    size_hint_y: None
                    height: self.minimum_height
                    row_default_height: 60
                    cols:1
                    id:empty
            BoxLayout:
                Button:
                    text:'Open Popup'
                    on_press: root.fire_popup()

                Button:
                    text:'Add'
                    on_release: app.add_more()

                Button:
                    text:'remove'
                    on_release: app.remove()

enter image description here

Upvotes: 0

Views: 552

Answers (1)

ikolim
ikolim

Reputation: 16021

Question

Seems that problem happens only if i use separate py. and kv file.

Answer

I don't think the problem is caused by separate py and kv file. The following example displayed the character correctly.

main.py

from kivy.base import runTouchApp
from kivy.lang import Builder


runTouchApp(Builder.load_file("main.kv"))

main.kv

GridLayout:
    cols: 2
    Label:
        text: 'Alegreya Sans SC font'
    Label:
        text: 'Roboto font (Default)'
    Label:
        text: 'Ä'
        font_size: 56
        font_name: '/home/iam/share/fonts/Alegreya_Sans_SC/AlegreyaSansSC-Medium.ttf'
    Label:
        text: 'Ä'
        font_size: 56

Problem

I'm getting empty box character instead of Ä character. How to make them show on my kivy application.

Solution

  • Download and extract Alegreya Sans SC font from Google Fonts. This font is under Open Font License
  • Add attribute, font_name: and provide the path to the extracted folder plus the type of font (e.g. AlegreyaSansSC-Medium.ttf).

Example

The following example illustrates displaying character, Ä using attribute, font_name to use different font. The default font use by Kivy is Roboto.

main-kv.py

from kivy.base import runTouchApp
from kivy.lang import Builder


runTouchApp(Builder.load_string("""
GridLayout:
    cols: 2
    Label:
        text: 'Alegreya Sans SC font'
    Label:
        text: 'Roboto font (Default)'
    Label:
        text: 'Ä'
        font_size: 56
        font_name: '/home/iam/share/fonts/Alegreya_Sans_SC/AlegreyaSansSC-Medium.ttf'
    Label:
        text: 'Ä'
        font_size: 56
"""))

Output

Result

Upvotes: 1

Related Questions