How do i make an MDTextButton adapt to the lenght of text it holds?

70 views
Skip to first unread message

Sulaiman Micheal

unread,
Jun 28, 2023, 4:37:50 AM6/28/23
to Kivy users support

Hello, I tried creating a text button that will hold any length of text. The width and height will adapt to the length of the text it holds.

`

MDTextButton: text:"KivyMD is a collection of Material Design compliant widgets for use with Kivy, a framework for cross-platform, touch-enabled graphica" pos_hint:{"center_x":.5, "center_y":.34} size_hint:None, None # size_hint_y: None text_size: None, None md_bg_color: [0,0,0,1] width: self.texture_size[0] + dp(5) height: self.texture_size[1] color: 1 , 0 , 0 , 1

`

I've tried everything but still, it's not working. Instead, I keep getting this error [CRITICAL] [Clock ] Warning, too much iteration done before the next frame. Check your code, or increase the Clock.max_iteration attribute. Remaining events:

I hope to get help here. Thank you.

Ovide

unread,
Jun 28, 2023, 5:34:23 AM6/28/23
to kivy-...@googlegroups.com
You got " too much iterations done" error because you are trying to initialize something by itself during initialization process(when the initial value has not been calculated yet). 
As i see this could only be due to these two lines


width: self.texture_size[0] + dp(5) height: self.texture_size[1]

I suspect that self.texture_size depends of self.width and self.height that you are trying to initialize. But it could be helpful to show us the full code. Because it is strange. 

Try to remove these lines and let us know what will happen. 


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/cc3ad53b-8fa3-4c70-b291-fc734c3dbed0n%40googlegroups.com.

elli...@cox.net

unread,
Jun 29, 2023, 10:59:29 AM6/29/23
to kivy-...@googlegroups.com

MDTextButton adapts to the size of the text as the default behavior.  Here is a small example:

 

from kivymd.app import MDApp
from kivy.lang import Builder

kv =
"""
MDBoxLayout:
    orientation: 'vertical'
    AnchorLayout:
        MDTextButton:
            text: 'Short Text'
            on_release: print(f'short button: {self.size}')
    AnchorLayout:
        MDTextButton:
            text: 'Much longer text, does the button changes size?'
            on_release: print(f'long button: {self.size}')

"""

class TextButtonApp(MDApp):
   
def build(self):
       
return Builder.load_string(kv)

TextButtonApp().run()

--

You received this message because you are subscribed to the Google Groups "Kivy users support" group.

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.

Sulaiman Micheal

unread,
Jun 30, 2023, 9:41:37 AM6/30/23
to kivy-...@googlegroups.com
Thank you very much. 

But what if the text is to be added dynamically? I am confused. But I'll implement the solution you provided first.

elli...@cox.net

unread,
Jun 30, 2023, 9:11:50 PM6/30/23
to kivy-...@googlegroups.com

Here is an example that dynamically sizes the button.  A Label is used to calculate the texture size, and then set the size of the button.

 

from kivy.lang import Builder
from kivy.uix.label import Label
from kivymd.app import MDApp
from kivymd.uix.button import MDTextButton

kv =
"""
MDBoxLayout:
    orientation: 'vertical'
    AnchorLayout:
        MDTextField:
            id: input
            hint_text: 'Enter Button Text'
            on_text: sb.set_width(self.text)
    AnchorLayout:
        StretchButton:
            id:sb
            text: input.text
            on_release: print(f'button size: {self.size}')

"""


class StretchButton(MDTextButton):
   
def set_width(self, text):
       
# create a label
       
label = Label(text=text, font_size=16)
       
# update the texture_size
       
label.texture_update()
       
# text size is now valid
       
self.width = label.texture_size[0]


Reply all
Reply to author
Forward
0 new messages