[Python] Kivy label positioning trouble

Discussão em 'Python' iniciado por Stack, Março 31, 2025.

  1. Stack

    Stack Membro Participativo

    I'm a Python beginner, and I was trying to build a Youtube Downloader. I just needed a GUI for easy usage. So I decided on Kivy.

    In the GUI I have this label named self.labelOutput. It is placed under a button. The script is very basic, so pressing the button only echoes the input.

    The problem is that this label is displaying weird behaviour (for me at least). It grows when multiple lines are entered (next step would be word wrapping). But the label seems to be growing upward, instead of downward. Like it's position is pinned at its bottom. Or as if my app only use half of the screen. I have searched the interwebs, including this site, but, while there are a lot of issues with labels, my particular issue is not here yet. I even asked AI generators, but that made my code only more messy.

    From CSS I remember certain attributes don't work properly when other properties aren't set or set wrong. Maybe it's something like that.

    I hope you guys can help.

    This is my code so far (far from pretty or efficient, I know. It's mostly AI generated to get started):

    import kivy
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.textinput import TextInput
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.uix.scrollview import ScrollView

    class MyApp(App):
    def build(self):
    float_layout = FloatLayout()
    # Make a BoxLayout to organiseren the widgets
    box_layout = BoxLayout(orientation='vertical', size_hint=(1, None), pos_hint={'top': 0.78})

    # Make a Label to display text
    self.label = Label(text="YTDownloader", font_size=90, size_hint_y=None, height=100)
    box_layout.add_widget(self.label)

    self.label2 = Label(text="Youtube Downloader", size_hint_y=None, height=100)
    box_layout.add_widget(self.label2)
    #spacer label
    self.label3 = Label(text="", size_hint_y=None, height=20)
    box_layout.add_widget(self.label3)

    # Make a TextInput-widget
    self.text_input = TextInput(hint_text="Voer zoektermen in...", size_hint=(None, None), height=100, width=900, pos_hint={'center_x': 0.5}, padding=(20,20))
    box_layout.add_widget(self.text_input)
    #another spacer label
    self.label4 = Label(text="", size_hint_y=None, height=30)
    box_layout.add_widget(self.label4)

    # Make a Button-widget
    button = Button(text="Zoek!", size_hint=(None, None), height=100, width=300, pos_hint={'center_x': 0.5})
    # connect a function to the button
    button.bind(on_press=self.on_button_press)
    box_layout.add_widget(button)

    self.labelOutput = Label(text="", size_hint_y=None, width=960, text_size=(960, None), valign='top')
    self.labelOutput.bind(texture_size=self.update_label_height)
    box_layout.add_widget(self.labelOutput)

    float_layout.add_widget(box_layout)

    return float_layout

    def on_button_press(self, instance):
    # get the input text and display it in the output label
    input_text = self.text_input.text
    self.labelOutput.text = f"You entered: {input_text}"

    def update_label_height(self, instance, value):
    instance.height = instance.texture_size[1]

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

    Continue reading...

Compartilhe esta Página