我将如何在 .py 文件中动态更改 kivy 按钮文本的字体大小

how I will Change the font size of the text of kivy button dynamically in .py file

实际上我想尝试更改我的 kivy 按钮的 font_Size 动态调整到其 Parnet 小部件的宽度。所以我可以根据 Kivy Gui App 的屏幕调整它的大小 我想在 .py 文件中将其更改为代码(不使用 .kv 文件或 kv 字符串) 我正在使用某种在 .kv 文件或 kv 字符串上运行良好的技巧,但它没有 在 .py 文件中工作,这个技巧是 我将按钮文本的 font_size 等于父小部件的 (width/6) [font_size=another_widget.width/6]但是没有效果,它不会动态变化,只会修复 font_size 。 I want to change the Button font size which is referred by main_text and Hint_text varaibles in the Source Code the line no. 36 and 46 respectively for these variables in the code 整个源代码在这里:-

from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.behaviors import RoundedRectangularElevationBehavior
from kivy.uix.screenmanager import ScreenManager,Screen
from kivymd.uix.fitimage import FitImage
from kivymd.uix.floatlayout import MDFloatLayout
from kivy.core.text import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class Elevation(RoundedRectangularElevationBehavior,MDCard):
    pass

class FirstWin(RoundedRectangularElevationBehavior,Screen):
    def __init__(self,**kwargs):
        super(FirstWin,self).__init__(**kwargs)
        # for i in range(0,10):
        mycard=Elevation(
            elevation=15,
            size_hint =(0.2,0.5),
            pos_hint={'center_x':0.5,'center_y':0.5},
            orientation='vertical',
            radius= [36, ],
            ripple_behavior=True,
            focus_behavior=True

        )
        image = FitImage(radius=[36,36,0,0],size_hint_y=3, size_hint_x=1,orientation="vertical")
        imagebutton = Button(background_normal="D:/Study/Python/Kivy/images/1.jpg",
                             background_down="D:/Study/Python/Kivy/images/1.jpg",
                             size_hint_y=550.0,
                             size_hint_x=1,
                              pos_hint={'x': 0, 'y': 0},
                             )
        texture_part = MDFloatLayout( md_bg_color=(46 / 255, 8 / 255, 211 / 255, .5),
                                     radius=[0, 0, 36, 36])
        main_text = Button(
            text="Tea and Coffee",
            halign="left",
            bold=True,
            pos_hint={'center_x': 0.3, 'top':1.2},
            size_hint=(1, None),
            font_size=mycard.width /6,
            background_normal='',
            background_color=(0, 0, 0, 0)
        )
        Hint_text = Button(
            text="Food Menu",
            halign="left",
            font_size=mycard.width/6,
            bold=True,
            color=(206 / 255, 203 / 255, 203 / 255, 0.2),
            pos_hint={'center_x': 0.2, 'top': 0.8},
            size_hint=(1, None),
            height=texture_part.height,
            background_normal='',
            background_color=(0, 0, 0, 0),
        )
        image.add_widget(imagebutton)
        mycard.add_widget(image)
        texture_part.add_widget(main_text)
        texture_part.add_widget(Hint_text)
        mycard.add_widget(texture_part)
        self.add_widget(mycard)



class SecondWin(Screen):
    pass
class MymdCard(MDApp):
    def build(self):
        sm = ScreenManager()
        self.theme_cls.theme_style = "Light"

        sm.add_widget(FirstWin(name='welcomeScreen'))
        sm.add_widget(SecondWin(name='functionScreen'))
        return sm




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

所以我想要解决方案,即我可以通过哪种技巧或方法动态更改按钮文本的 font_size。如果是的话,也请告诉我。这对我很有帮助。谢谢!!

一切都是合乎逻辑的,如果您在 kv 文件中编写代码,小部件会自动监视参数的变化(您可以查看源代码)。为了使 py 文件中的所有内容都能正常工作,您必须指定 bind 方法。对于您的示例,它将是这样的:

# after creating Hint_text instance
mycard.bind(width=lambda inst, width: self.change_font(width, main_text, Hint_text))

...
# in FirstWin class
def change_font(self, width, main_text, Hint_text):
    main_text.font_size = width / 6
    Hint_text.font_size = width / 6

你可以玩这个例子,它会动态计算文本的大小。如果您不想使用 kv,只需使用我上面指出的 bind

from kivy.lang import Builder

from kivymd.app import MDApp

KV = '''
MDScreen:
    MDCard:
        id: card
        orientation: "vertical"
        size_hint: .8, .8
        pos_hint: {"center_x": .5, "center_y": .5}
        md_bg_color: [0, 1, 0, 1]
            
        MDLabel:
            text: "Ride the Lightning"
            theme_text_color: "Primary"
            font_size: max(min(sp(card.width / len(self.text)), sp(card.height)), sp(12))
            halign: 'center'
'''


class TestCard(MDApp):
    def build(self):
        return Builder.load_string(KV)


TestCard().run()

它在 kv 中有效,因为 kv 自动设置绑定以调整依赖于其他属性的属性。但是,如果您不使用 kv,则必须自己设置这些绑定。如果没有这些绑定,font_size 值将在创建 main_textHint_text 按钮时根据 mycard 的宽度设置,并且不会更新。这是您的 FirstWin class 的修改版本,它执行绑定:

class FirstWin(RoundedRectangularElevationBehavior,Screen):
    def __init__(self,**kwargs):
        super(FirstWin,self).__init__(**kwargs)
        # for i in range(0,10):
        mycard=Elevation(
            elevation=15,
            size_hint =(0.2,0.5),
            pos_hint={'center_x':0.5,'center_y':0.5},
            orientation='vertical',
            radius= [36, ],
            ripple_behavior=True,
            focus_behavior=True
        )
        mycard.bind(size=self.adjust_sizes)  # This binding will cause the adjust_sizes() method to be triggered when mycard.size changes
        image = FitImage(radius=[36,36,0,0],size_hint_y=3, size_hint_x=1,orientation="vertical")
        imagebutton = Button(background_normal="D:/Study/Python/Kivy/images/1.jpg",
                             background_down="D:/Study/Python/Kivy/images/1.jpg",
                             size_hint_y=550.0,
                             size_hint_x=1,
                              pos_hint={'x': 0, 'y': 0},
                             )
        texture_part = MDFloatLayout( md_bg_color=(46 / 255, 8 / 255, 211 / 255, .5),
                                     radius=[0, 0, 36, 36])
        
        # save a reference to main_text
        self.main_text = Button(
            text="Tea and Coffee",
            halign="left",
            bold=True,
            pos_hint={'center_x': 0.3, 'top':1.2},
            size_hint=(1, None),
            font_size=mycard.width /6,
            background_normal='',
            background_color=(0, 0, 0, 0)
        )
        
        # save a reference to Hint_text
        self.Hint_text = Button(
            text="Food Menu",
            halign="left",
            font_size=mycard.width/6,
            bold=True,
            color=(206 / 255, 203 / 255, 203 / 255, 0.2),
            pos_hint={'center_x': 0.2, 'top': 0.8},
            size_hint=(1, None),
            height=texture_part.height,
            background_normal='',
            background_color=(0, 0, 0, 0),
        )
        image.add_widget(imagebutton)
        mycard.add_widget(image)
        texture_part.add_widget(self.main_text)
        texture_part.add_widget(self.Hint_text)
        mycard.add_widget(texture_part)
        self.add_widget(mycard)

    # This method adjusts the font_size properties
    def adjust_sizes(self, mycard, new_size):
        new_font_size = mycard.width / 6
        self.Hint_text.font_size = new_font_size
        self.main_text.font_size = new_font_size