Dylan D.
Dylan D.

Reputation: 55

AnchorLayout stuck in bottom left in Kivy

i've started rewriting a very simple Tkinter app for kivy and i'm having an issue my AnchorLayout is stuck in the bottom left and i cant fix it. Here is my code:

from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.textinput import TextInput 
from kivy.uix.button import Button 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout



class MyApp(App):

    def build(self):
        controls = AnchorLayout(anchor_x='left', anchor_y='top')
        box = BoxLayout()


        upc_l = Label(text='UPC:', font_size=40, size_hint_y=None, 
                      size_hint_x=None, height=50, width=100,)
        entry = TextInput(font_size=40, size_hint_y=None, size_hint_x=None, 
                          height=50, width=350)
        search_b = Button(text='Search', font_size=40, size_hint_y=None,       
                          size_hint_x=None, height=50, width=200)


        controls.add_widget(box)
        box.add_widget(upc_l)
        box.add_widget(entry)
        box.add_widget(search_b)


        return controls




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

Upvotes: 0

Views: 654

Answers (2)

Dylan D.
Dylan D.

Reputation: 55

Ok i dont know if im supposed to answer my own question or just comment (new to SO) but here is what i figured out after many hours of frustration last night. And thank you for your answer, inclement. I've been watching your tutorials actually.

from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.textinput import TextInput 
from kivy.uix.button import Button 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout



class MyApp(App):

    def build(self):
        controls = AnchorLayout(anchor_x='right', anchor_y='top', height=200)
        box = BoxLayout(size_hint_y=None, height=50)



        upc_l = Label(text='UPC:', font_size=40, size_hint_x=None, width=100,)
        entry = TextInput(font_size=40, size_hint_x=None, width=350)
        search_b = Button(text='Search', font_size=40, size_hint_x=None,
                          width=200, background_color=[0,1.7,0,1])


        controls.add_widget(box)
        box.add_widget(upc_l)
        box.add_widget(entry)
        box.add_widget(search_b)


        return controls




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

Upvotes: 1

inclement
inclement

Reputation: 29460

The BoxLayout fills the window, because its size_hint is 1, 1. Its children all have manual heights and widths set, so it just places them one after another in the bottom left of itself, which is the bottom left of the screen, even though the AnchorLayout is working fine.

Give the BoxLayout a manual size to have this work better. Actually, since you're manually setting the size of its children, you'd be better with a GridLayout whose size is set to track its minimum_width and minimum_heightproperties.

Upvotes: 1

Related Questions