1

I'm looking for a way to give my BoxLayout ability to stretch when we resize the window with wrapped text inside, so when the wrapped text down the line, the BoxLayout containing it will stretch down with them avoiding text dissapear.

Before resizing

Before resize

After resizing, the BoxLayout size still remain which cause some text lost.

After resize

Edit: My code (it's working now)

.py file

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.metrics import dp

class Chat_history_update(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = App.get_running_app()
    def Chat_history_generate(self):
        self.ids['label'] = Wrapped_Label(text="hello"*200,halign="right",valign="top",size_hint=(1,None))
        self.ids['box'] = Wrapped_Box_Layout(pos_hint={"right":1},size_hint=(.7,None))
        self.ids['box'].add_widget(self.ids['label'])
        self.add_widget(self.ids['box'])
        
class Wrapped_Label(Button):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = App.get_running_app()
        self.bind(
            width=lambda *x: self.setter('text_size')(self, (self.app.root.ids.box.width,None)),
            texture_size=lambda *x: self.setter('size')(self, (self.texture_size[0],self.texture_size[1])))

class Wrapped_Box_Layout(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = App.get_running_app()
        self.bind(
            size=lambda *x: self.setter('height')(None,self.app.root.ids.label.height))
        
class Test(App):
    pass

Test().run()

.kv file

Chat_history_update:
<Chat_history_update>:
    orientation: "vertical"
    Button:
        text: "generate"
        size_hint: None,None
        size: "100dp","100dp"
        pos_hint: {"top":1}
        on_press: root.Chat_history_generate()
    
Ten Kho
  • 184
  • 10
  • 1
    Please post a [mcve]. – John Anderson Feb 04 '22 at 17:54
  • Please either refer to your previous post or do as @John Anderson said. Based on your previous post I think you need some pre calculation to get it done, in that case you can check [this post](https://stackoverflow.com/questions/70513237/how-to-calculate-the-length-of-a-string-in-pixels-for-specific-font-and-size-us/70516874). – ApuCoder Feb 04 '22 at 19:05
  • I updated the code after messing with it, for some reason it's working pretty well now... – Ten Kho Feb 05 '22 at 00:31

0 Answers0