Kivy GridLayout is At Bottom-Left and Looking Weird

Question:

Those are the codes:

app.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget

Builder.load_file("view.kv")

class LoginWidget(Widget):
    pass

class ClientApp(App):
    def build(self):
        return LoginWidget()

if __name__ == '__main__':
    ClientApp().run()

view.kv

#:kivy 1.9.0

<LoginWidget>:
    f_username: username
    f_password: password

    GridLayout:
        rows: 2
        cols: 2

        Label:
            text: "Okul Numarası"
        TextInput:
            id: username

        Label:
            text: "Şifre"
        TextInput:
            id: password
            password: True

However, it is at the bottom-left, in a compressed view as below:

enter image description here

I had written some bunch of codes like a month ago and I remember I was not having these issues. Am I missing something?

Info

  • Ubuntu 14.04
  • Python 3.x
  • Kivy 1.9.0
Asked By: Eray Erdin

||

Answers:

Your GridLayout has a default size of 100×100 pixels. Set the size property to change it:

<LoginWidget>:
    f_username: username
    f_password: password

    GridLayout:
        size: root.size # set the size manually

        rows: 2
        cols: 2
        Label:
            text: "Okul Numarası"
        TextInput:
            id: username

        Label:
            text: "Şifre"
        TextInput:
            id: password
            password: True

That’s because the parent is a subclass of Widget. You can also derive from a Layout class instead:

class LoginWidget(FloatLayout):
    pass

Then you could use size_hint property to controll precisely how much space it should take.

Answered By: Nykakin

I just wanted to admit that I found the answer helpful.
I had written my code only in .py file.
In my case I wrote these two lines:
self.size=self.size
self.size_hint=(1,1)
and it has been resolved after days of ideas, so thank you.

def __init__(self):
    GridLayout.__init__(self)
    self.hours=[]
    self.cols=1
    self.rows=9

    self.allthemoney=TextInput(hint_text="", height=50, width=350,font_size=30)
    self.allthemoney.show_keyboard()
    self.add_widget(self.allthemoney)
    for i in range(7):
        self.hours.append(TextInput(hint_text="Worker number "+str(i+1)+" hours", height=50, width=500,font_size=30))
        self.hours[i].show_keyboard()
        self.add_widget(self.hours[i])
    self.add_widget(Button(text="submit", size=(300,70),on_press=self.submit,font_size=60))
    self.size = self.size
    self.size_hint = (1, 1)
Answered By: גלעד קרפל
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.