简体   繁体   中英

How do i allign text inside a kivy label within a .kv file?

I try to make a gui for a text based RPG, and now i want to allign the text of some labels to the top left, but "halign:" and "valign:" don't seem to do anything.

So how do I allign the text inside my labels? Is there something I have done horribly wrong?

This is how the GUI looks at this moment and I marked where the text should be with green arrows: gui_test

This is how my .kv file looks:

BoxLayoutExample:


<BackgroundColor@Widget>:
    background_color: 1,1,1,1
    canvas.before:
        Color:
            rgba: root.background_color
        Rectangle:
            size: self.size
            pos: self.pos

<BackgroundLabel@Label+BackgroundColor>:
    background_color: 0, 0, 0, 0 
            
<BoxLayoutExample>:
    orientation: "vertical"
    BoxLayout:
        orientation:"horizontal"

        BackgroundLabel:
            background_color: 1,0,0,1
            text: "Placeholder Text\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST" 
            halign: "left"
            valign: "top"
            font_size: "10sp"
            size_hint: .5, 1

        Label:       
            text: "Placeholder Map/Enemy #TEST TEST TEST TEST TEST \nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
            halign: "left"
            valign: "top"
            size_hint: 1, 1.3

        BoxLayout:
            orientation:"vertical"
            size_hint: .5, 1
            
            Label: 
                background_color: 1,1,1,.5      
                text: "Placeholder Stats\nHP\nMP\nDMG\nXP\nLVL"
                halign: "left"
                valign: "top"
                size_hint: 1, .3
                
            ScrollView:
                size_hint: 1, .7
                scroll_distance: 100
                 
                Label:                      
                    text: "Placeholder Inventory\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
                    size_hint: None, None     
                    size: self.texture_size
                    halign: "left"
                    valign: "top"


    BoxLayout:
        orientation: "horizontal"
        size: "60dp","60dp"        
        size_hint: None,None

        Label:
            size: "60dp","60dp"        
            size_hint: None,None
        
        Button:
            text: "go\nnorth"
            size: "60dp","60dp"        
            size_hint: None,None

        Label:
            size: "60dp","60dp"        
            size_hint: None,None

    BoxLayout:
        orientation: "horizontal"
        size: "180dp","60dp"
            #pos: "0dp","60dp"
        size_hint: None,None
        Button:
            text: "go\nwest"
            size: "60dp","60dp"
            #pos: "0dp","60dp"
            size_hint: None,None
            pos_hint: {"x":0}
        Button:
            text: "go\nsouth"
            size: "60dp","60dp"
            size_hint: None,None
        Button:
            text: "go\neast"
            size: "60dp","60dp"
            #pos: "0dp","60dp"
            size_hint: None,None
        Label:
            size: "60dp","60dp"        
            size_hint: None,None

        Button:
            text: "use\nitem"
            size: "60dp","60dp"        
            size_hint: None,None

        Button:
            text: "equip\ngear"
            size: "60dp","60dp"        
            size_hint: None,None

        Button:
            text: "unequip\ngear"
            size: "60dp","60dp"        
            size_hint: None,None

Thanks for your help, i really appreciate it.

You need to add this argument in your label:

text_size: self.size

Then the halign and valign arguments will work properly, for example:

Label:       
    text: "Placeholder Map/Enemy #TEST TEST TEST TEST TEST \nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
    text_size: self.size
    halign: "left"
    valign: "top"
    size_hint: 1, 1.3

More details in the official kivy documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM