Search code examples
pythonalignmentkivykivy-language

Python: Kivy: How to align a label itself and text box?


I've tried multiple pieces of code from here, but they all seem to align the text within the label or text box. I want to align the text box and the label vertically and horizontally. I am a beginner at kivy, so please excuse me if the answer is very obvious.

  1. Is there a way to align the label itself and not the text inside?
  2. Is there also a way to align the text box itself and not the text inside?

Thanks in advance!

This is my .kv file firstkivy.kv

#Filename: firskivy.kv

<MyGrid>:
    Label:
        text: "Writing"
        font_size: 80
        bold: True
        color: 0.204, 0.204, 0.204, 1
        size: self.texture_size
        text_size: self.size
        halign: 'center'
        valign:'middle'
        canvas.before:
            Color:
                rgba: 0.549, 1, 0.984, 1
            Rectangle:
                size: self.size
                pos: self.pos


    TextInput:
        size_hint: (.2, None)
        height: 100
        width: 360
        multiline: True
        size_hint: (None, None)
        font_size: 30
        halign: 'right'
        pos_hint: (None, None)
        focus: True

This is the .py file firstkivy.py

# setting up and fixing the window size
#to prevent it from being resized
from kivy.config import Config
Config.set('graphics', 'resizable', False)
Config.set('graphics','width', '360')
Config.set('graphics', 'height', '540')

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window

# going back to this one...
Window.clearcolor = (0.549, 1, 0.984, 1)

class MyApp(Widget):
    pass

#building the app
class FirstKivy(App, BoxLayout):
    def build(self):

        self.title = 'Arabic Language Learning App'
        return MyApp()

# running the app
if __name__ == '__main__':
    FirstKivy().run()

Here is the Output


Solution

  • Try writing your firstkivy.py like this:

    from kivy.config import Config
    Config.set('graphics', 'resizable', False)
    Config.set('graphics','width', '360')
    Config.set('graphics', 'height', '540')
    
    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.uix.textinput import TextInput
    from kivy.uix.boxlayout import BoxLayout
    from kivy.core.window import Window
    
    
    class MyApp(BoxLayout):
        pass
    
    
    class FirstKivy(App):
        title = 'Arabic Language Learning App'
        def build(self):
    
            return MyApp()
    
    
    if __name__ == '__main__':
        FirstKivy().run
    

    And your firstkivy.kv like:

    <MyApp>:
        orientation: "vertical"
        spacing: 10
        space_x: self.size[0]/3
        canvas.before:
            Color:
                rgba: 0.549, 1, 0.984, 1
            Rectangle:
                size: self.size
                pos: self.pos
    
        FloatLayout:
    
            Label:
                text: "Writing"
                font_size: 80
                bold: True
                color: 0.204, 0.204, 0.204, 1
                pos_hint: {'center_x': .5, 'center_y': .8}
                #size: self.texture_size
                #text_size: self.size
                #halign: 'center'
                #valign:'middle'
    
    
            TextInput:
                size_hint: 1, .5
                #height: 100
                #width: 200
                multiline: True
                pos_hint: {'center_x': .5}
                #size_hint: None, None
                #font_size: 30
                #halign: 'right'
                #pos_hint: None, None
                #focus: True
    

    Hope it achieves what you want.