MDRectangleFlatIconButton 的文本在更新为较长文本时显示为两行

MDRectangleFlatIconButton's text is displayed in two lines when updated with a longer text

我电脑上安装的程序: python3.10.4 KivyMD 1.0.0.dev0 基维 2.1.0.

我想更新 MDRectangleFlatIconButton 的文本。它随新文本而变化,但当新文本比以前的文本长时,文本会分成两行。当我使用普通按钮时,新文本经过适当调整后会在一行中显示。

但由于 MDRectangleFlatIconButton 中有一个图标,当新文本比以前的文本长时,文本会排成两行。

在运行程序中,在“信息”按钮的弹出窗口中添加一个比“信息”长的新应用程序名称window,然后单击“更新顶部栏的名称”。然后,它更新了前面主应用程序“信息”按钮的标题和文本。

我试图通过添加按钮的 text_size: self.width、valign:"center"、haling: "center" 或手动添加 text_size: cm(10 ), 厘米 (10)。另外,我尝试使用 on_release: "app.root.ids.bt_information.text_size = self.width, None

但到目前为止没有任何效果。

非常感谢你的帮助。

当我输入比之前的名称“Info”更长的 App 名称“Info12”时,它会在按钮中排成两行。 This is the result of it

Python代码 '''

from kivy.uix.widget import Widget
'''Setting the size of first window for program'''
from kivy.config import Config                 #another way of setting size of window
Config.set('graphics', 'width', '600')         # from kivy.core.window import Window
Config.set('graphics', 'height', '750')        # Window.size = ("600", "750")

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty

Builder.load_file('new_window_popup.kv')

class Dex(Popup):
    pass
    
class Remi(Popup):
    pass

class Info(Popup):
    pass

class MyLayout(Widget):
    pass
class AwesomeApp(MDApp):
    def build(self):
        self.title = "My house"
        return MyLayout()

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

'''

kivy文件:new_window_popup.kv

'''

#:import Factory kivy.factory.Factory
#:import MDRaisedButton kivymd.uix.button

<Dex>:
    auto_dismiss: False
    size_hint: 1, 1

    title: "Weight-Based Dose Calculator "   
    canvas.before:
        Color:
            rgba: (0,1,0,1)
        Rectangle:
            pos:self.pos
            size:self.size
    
    BoxLayout:
        
        orientation: "vertical"
        size:root.width, root.height

        Label:
            text: "Dex 1" 
        Button:
            text: "Close"
            font_size: 24
            on_release: root.dismiss()    
    
<Remi>:
    auto_dismiss: False
    size_hint: 1, 1

    title: "Weight-Based Dose Calculator "   
    canvas.before:
        Color:
            rgba: (0,1,0,1)
        Rectangle:
            pos:self.pos
            size:self.size
    
    BoxLayout:
        
        orientation: "vertical"
        size:root.width, root.height

        Label:
            text: "Remi" 
        Button:
            text: "Close"
            font_size: 24
            on_release: root.dismiss()

<Info>:

    appName:appName
    auto_dismiss: False
    size_hint: 1, 1

    title: "Change Info"   
    canvas.before:
        Color:
            rgba: (0,1,0,1)
        Rectangle:
            pos:self.pos
            size:self.size
    
    BoxLayout:
        
        orientation: "vertical"
        size:root.width, root.height

        Label:
            text: "What is your App name?" 
        BoxLayout:
            orientation: "horizontal"
            
            MDTextField:
                id: appName
                hint_text: "App Name"
                color_mode: 'primary'
                current_hint_text_color: 1,1,1,1
                hint_text_color_focus: 1,1,1,.9 
                line_color_focus: 1,1,1,1
                font_size: '25sp'
                text_color_normal: 1,1,1,.9
                text_color_focus: 0,0,1,.9
                focus: True
                write_tab: False
            Button:
                text: "Update Top Bar\'s name"
                font_size: 24
                size_hint: .8, .2
                # on_release: root.updateName()    
                on_release: 
                    app.title = appName.text
                    app.root.ids.bt_information.text = appName.text
        Button:
            text: "Close"
            font_size: 24
            on_release: root.dismiss()

<MyLayout>
    MDBoxLayout:
        orientation:"vertical"
        size: root.width, root.height
        
        MDRaisedButton:
            text: "Dex"
            font_size: 32
            text_color: 0,0,0,.9
            size_hint: 1,.5
            on_press: Factory.Dex().open()
        MDRaisedButton:
            text: "Remi"
            font_size: 32
            size_hint: 1,.5
            on_press: Factory.Remi().open()
        MDRectangleFlatIconButton:
            id: bt_information
            text: "Info"
            icon: "youtube-studio"
            font_size: 32
            size_hint: 1,.2
            text_size: self.width, None
            md_bg_color: 0.95,0.61,0.73,1
            on_press: Factory.Info().open()

'''

MDRectangleFlatIconButton 根据初始 text 设置其包含的 MDLabelwidth 并且从不更改该宽度,因此向 [=13= 添加文本] 被迫添加更多行。这是通过计算所需宽度并显式更改 MDLabel:

的宽度来解决该问题的技巧
from kivy.core.text import Label as CLabel
class AwesomeApp(MDApp):
    def build(self):
        self.title = "My house"
        Clock.schedule_interval(self.change_text, 2)
        return MyLayout()

    def change_text(self, *args):
        butt = self.root.ids.bt_information  # this is the MDRectangleFlatIconButton
        label = butt.ids.lbl_txt  # this is the MDLabel of concern
        butt.text += ' qwe'  # change the text of the Label
        
        # calculate the required width
        core_label = CLabel(text=label.text, font_name=label.font_name, font_size=label.font_size, max_lines=1)
        core_label.refresh()
        
        # set the width of the label to hold the new text on one line
        label.width = core_label.width