Python KivyMD访问选项卡内的小部件



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

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小部件内的各个选项卡。有解决方案吗?

您可以从应用程序类访问根小部件中的任何id,如self.root.ids.label您可以在构建方法

中执行此操作。
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()

最新更新