【问题标题】:Python KivyMD access widgets inside tabsPython KivyMD 访问选项卡内的小部件
【发布时间】:2021-09-04 15:21:23
【问题描述】:

长话短说,这是我的一个项目的简化代码:

test.py

from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase


class Tabs(MDFloatLayout, MDTabsBase):
    pass

class Test(MDApp):
    pass

Test().run()

test.kv

MDFloatLayout:
    MDTabs:
        id: tabs

        Tabs:
            id: one
            title: 'one'

            MDLabel: #example widget that i want to access to like PLEASE
                id: label
                text: ';-;'
                halign: 'center'
                valign: 'center'

        Tabs:
            id: two
            title: 'two'

            MDRectangleFlatButton: #also this dabhfdvsfdgbjdkfmg
                id: button
                text: 'end my misery'
                pos_hint: {'center_x': 0.5, 'center_y': 0.5}

这里的问题基本上是我不知道如何访问各个选项卡内的labelbutton 小部件。有什么解决办法吗?

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    您可以像这样self.root.ids.label 那样从应用程序类访问根小部件中的任何 id 你可以在构建方法中做到这一点

    from kivymd.uix.floatlayout import MDFloatLayout
    from kivymd.app import MDApp
    from kivymd.uix.tab import MDTabsBase
    
    
    class Tabs(MDFloatLayout, MDTabsBase):
        pass
    
    
    class Test(MDApp):
        def build(self):
            # here you will get the text of the label using id
            print(self.root.ids.label.text)
    
    
    Test().run()
    
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      相关资源
      最近更新 更多