Bohr
Bohr

Reputation: 103

Kivy Label and Popup + long text

So I am trying to show some long text in a Popup. It would be ideal if the height of the Popup could change for the length of text. But that's not my only problem I've tried to implement this without the size change with this code

    boxl = uix.boxlayout.BoxLayout(orientation="vertical")
    boxl2 = uix.boxlayout.BoxLayout(orientation="horizontal")
    pop = Popup(title="Title", content=boxl, size_hint=(0.75,0.8))
    text = "Really long text"

    document = uix.label.Label(text=text,markup=True, valign='top')

    button = uix.button.Button(text='back', size_hint_y=None, height=40)
    button2 = uix.button.Button(text="Button Title", size_hint_y=None, height=40)
    button.bind(on_press=(lambda x:pop.dismiss()))
    button2.bind(on_press=(lambda x,data=data:(self.set_vorteil(data),pop.dismiss())))
    boxl.add_widget(document)

    boxl2.add_widget(button)
    boxl2.add_widget(button2)
    boxl.add_widget(boxl2)
    document.bind(size=document.setter('text_size'))
    pop.open()

The interesting thing with this is that my text is cut off, even if there is a huge free space before the buttons. How do I fix this or even make it right? I'm confused with Kivy's way of using Labels.

Upvotes: 3

Views: 2296

Answers (3)

yoel
yoel

Reputation: 315

If anyone is interested, Here is my implementation of a popup that grows according to the text in it:

<NotificationPopup@Popup>
height: content_id.height + 50
width: '400dp'
size_hint: (None, None)

BoxLayout:
    orientation: 'vertical'
    id: content_id
    size_hint: (None, None)
    height: self.minimum_height + 25
    width: root.width - 25

    Label:
        size_hint_y: None
        height: self.texture_size[1]
        text_size: (self.width, None)
        text: 'A' * 400
        padding: (0, 10)

    Button:
        size_hint_y: None
        height: '40dp'
        text: 'ok'
  • See how it grows horizontally by changing the text length (text: 'A' * 400)
  • You can change the popup's width by changing the Popup's width.

Upvotes: 0

Peter Badida
Peter Badida

Reputation: 12169

document.bind(size=document.setter('text_size'))

limits the text area to a specific size, that's a size of the widget itself. It works pretty well except the fact, that you did:

boxl.add_widget(boxl2)

which is a good thing, but you forgot to set size_hint=(None, None) and a specific size for the BoxLayout, therefore:

enter image description here

On the other hand, you set it for the Buttons themselves, which made you think the layout itself is changed. (It's not.) Fix it with e.g. BoxLayout(size_hint=(None, None), size=<desired size>).

Also, this way is not really nice as it limits the user e.g. when the mobile layout is too small. I'd rather go for a scrollable label:

from kivy.lang import Builder
from kivy.base import runTouchApp
runTouchApp(Builder.load_string('''
ScrollView:
    Label:
        size_hint_y: None
        text_size: (self.width, None)
        text: 'lorem ipsum dolor ' * 1000
        height: self.texture_size[1]
'''))

or for an implementation of that in a RecycleView, which would make it efficient.

Upvotes: 4

ODiogoSilva
ODiogoSilva

Reputation: 2414

The reason why you get so much free space between the Label and the Button widgets is that the two BoxLayout widgets (boxl and boxl2) are actually splitting the space in half (even if you don't notice it). If you want your Label to have all that space, you'll need to set the height of the boxl2 widget:

boxl2 = BoxLayout(orientation="horizontal", size_hint_y=None, height=40)

This sets the boxl2 widget to the same height as the Button widgets

Upvotes: 3

Related Questions