如何通过 ID 访问小部件并将其添加到 MDGridLayout:KivyMD 中的 BOX
How do I access and add a widget to MDGridLayout by ID : BOX in KivyMD
如何通过 ID 访问小部件并将其添加到 MDGridLayout
:在我的例子中是 BOX???
我尝试了各种方法达到我想要的结果,但总是报错,root does not see ID: BOX self.root.ids。 ......
print(self.test2.ids)
提高 AttributeError: 'TestApp' object has no attribute 'test2'
同时 print(self.root.ids)
returns
{'screen_manager': <WeakProxy to <kivy.uix.screenmanager.ScreenManager object at 0x00000221CA016580>>,
'toolbar': <WeakProxy to <kivymd.uix.toolbar.MDToolbar object at 0x00000221CA0B6E40>>,
'nav_drawer': <WeakProxy to <kivymd.uix.navigationdrawer.MDNavigationDrawer object at 0x00000221CA133DD0>>}
test.py
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
Builder.load_file('test2.kv')
class ContentNavigationDrawer(MDBoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class TestApp(MDApp):
def build(self):
return
TestApp().run()
test.kv
<ContentNavigationDrawer>
md_bg_color: 0.231, 0.231, 0.231, 1
ScrollView:
MDList:
OneLineAvatarIconListItem:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
text: "Main Menu"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr1"
IconLeftWidget:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
icon: "menu-open"
OneLineAvatarIconListItem:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
text: "Test"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "test2"
IconLeftWidget:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
icon: "fire"
MDScreen:
md_bg_color: 0.164, 0.164, 0.164, 1
MDNavigationLayout:
x: toolbar.width
ScreenManager:
id: screen_manager
MDScreen:
name: "scr1"
MDBoxLayout:
cols: 1
orientation: 'vertical'
md_bg_color: 0.235, 0.247, 0.254, 1
Test2:
MDToolbar:
id: toolbar
title: "SCUM INFO"
pos_hint: {"top": 1}
md_bg_color: 0.231, 0.231, 0.231, 1
specific_text_color: 1, 1, 1, 1
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
right_action_items:
[["magnify", lambda x: banner.show()], ["cog-outline", lambda x: app.set_screen("setting")]]
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
test2.kv(第二屏)
<Test2@MDScreen>:
name: "test2"
MDBoxLayout:
cols: 1
orientation: 'vertical'
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .11
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .89
ScrollView:
MDGridLayout:
id:box # <---------------- ID ID ID ID
cols: 1
size_hint_y: None
height: '200dp'
padding: 5, 5, 5, 5
spacing: dp(15)
# VVVVVVVVVVVVVVVVVVVVVVVVVVVV DUPLICATE,DUPLICATE ---->
MDGridLayout:
cols: 1
padding: 5, 5, 5, 5
md_bg_color: 0.231, 0.231, 0.231, 1
spacing: dp(5)
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 1
rectangle: self.x, self.y, self.width, self.height
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .30
md_bg_color: 0.235, 0.247, 0.254, 1
MDRelativeLayout:
cols: 1
orientation: 'vertical'
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
FitImage:
source: 'img.jpg' # <----------
MDLabel:
id: news_title
text: "Some text" # <-------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
theme_text_color: "Custom"
text_color: app.theme_cls.accent_color
pos_hint: {"center_y": .5}
font_style: "H6"
font_size: root.width/18
您似乎想访问动态 class Test2
的 ids
。您可以通过以下方式实现:
首先通过在 kvlang
中创建其 id
作为引用,
Test2:
id: test2
现在您应该可以像往常一样通过 self.root.ids.test2.ids
访问其内部 ID(在 App
的子 class 中完成,即在 TestApp
中)
如何通过 ID 访问小部件并将其添加到 MDGridLayout
:在我的例子中是 BOX???
我尝试了各种方法达到我想要的结果,但总是报错,root does not see ID: BOX self.root.ids。 ......
print(self.test2.ids)
提高 AttributeError: 'TestApp' object has no attribute 'test2'
同时 print(self.root.ids)
returns
{'screen_manager': <WeakProxy to <kivy.uix.screenmanager.ScreenManager object at 0x00000221CA016580>>,
'toolbar': <WeakProxy to <kivymd.uix.toolbar.MDToolbar object at 0x00000221CA0B6E40>>,
'nav_drawer': <WeakProxy to <kivymd.uix.navigationdrawer.MDNavigationDrawer object at 0x00000221CA133DD0>>}
test.py
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
Builder.load_file('test2.kv')
class ContentNavigationDrawer(MDBoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class TestApp(MDApp):
def build(self):
return
TestApp().run()
test.kv
<ContentNavigationDrawer>
md_bg_color: 0.231, 0.231, 0.231, 1
ScrollView:
MDList:
OneLineAvatarIconListItem:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
text: "Main Menu"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr1"
IconLeftWidget:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
icon: "menu-open"
OneLineAvatarIconListItem:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
text: "Test"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "test2"
IconLeftWidget:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
icon: "fire"
MDScreen:
md_bg_color: 0.164, 0.164, 0.164, 1
MDNavigationLayout:
x: toolbar.width
ScreenManager:
id: screen_manager
MDScreen:
name: "scr1"
MDBoxLayout:
cols: 1
orientation: 'vertical'
md_bg_color: 0.235, 0.247, 0.254, 1
Test2:
MDToolbar:
id: toolbar
title: "SCUM INFO"
pos_hint: {"top": 1}
md_bg_color: 0.231, 0.231, 0.231, 1
specific_text_color: 1, 1, 1, 1
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
right_action_items:
[["magnify", lambda x: banner.show()], ["cog-outline", lambda x: app.set_screen("setting")]]
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
test2.kv(第二屏)
<Test2@MDScreen>:
name: "test2"
MDBoxLayout:
cols: 1
orientation: 'vertical'
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .11
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .89
ScrollView:
MDGridLayout:
id:box # <---------------- ID ID ID ID
cols: 1
size_hint_y: None
height: '200dp'
padding: 5, 5, 5, 5
spacing: dp(15)
# VVVVVVVVVVVVVVVVVVVVVVVVVVVV DUPLICATE,DUPLICATE ---->
MDGridLayout:
cols: 1
padding: 5, 5, 5, 5
md_bg_color: 0.231, 0.231, 0.231, 1
spacing: dp(5)
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 1
rectangle: self.x, self.y, self.width, self.height
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .30
md_bg_color: 0.235, 0.247, 0.254, 1
MDRelativeLayout:
cols: 1
orientation: 'vertical'
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
FitImage:
source: 'img.jpg' # <----------
MDLabel:
id: news_title
text: "Some text" # <-------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
theme_text_color: "Custom"
text_color: app.theme_cls.accent_color
pos_hint: {"center_y": .5}
font_style: "H6"
font_size: root.width/18
您似乎想访问动态 class Test2
的 ids
。您可以通过以下方式实现:
首先通过在 kvlang
中创建其 id
作为引用,
Test2:
id: test2
现在您应该可以像往常一样通过 self.root.ids.test2.ids
访问其内部 ID(在 App
的子 class 中完成,即在 TestApp
中)