Python KivyMD 访问选项卡内的小部件
Python KivyMD access widgets inside tabs
长话短说,这是我的一个项目的代码,经过简化:
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}
基本上这里的问题是我不知道如何访问各个选项卡中的 label
和 button
小部件。有什么解决办法吗?
您可以像这样从应用程序 class 访问根小部件中的任何 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()
长话短说,这是我的一个项目的代码,经过简化:
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}
基本上这里的问题是我不知道如何访问各个选项卡中的 label
和 button
小部件。有什么解决办法吗?
您可以像这样从应用程序 class 访问根小部件中的任何 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()