如何通过 KivyMD 中的 ID : BOX 访问小部件并将其添加到 MDGridLayout
在我的例子中,如何通过 ID : BOX 访问小部件并将其添加到 MDGridLayout
???
我尝试通过各种方式达到预期的结果,但它不断给出错误,root看不到ID:BOX self.root.ids。 ......
print(self.test2.ids)
引发 AttributeError: 'TestApp' 对象没有属性 'test2'
而 print(self.root .ids)
返回
{'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
How do I access and add a widget to MDGridLayout
by ID : BOX in my case???
I tried to achieve the desired result in various ways, but it constantly gives an error, root does not see ID: BOX self.root.ids. ......
print(self.test2.ids)
raises AttributeError: 'TestApp' object has no attribute 'test2'
While 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 (second screen)
<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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎想要访问动态类
Test2
的ids
。您可以按如下方式实现此目的:首先通过在
kvlang
中创建其id
来引用它,现在您应该能够像往常一样通过
self 访问其内部 id。 root.ids.test2.ids
(在App
的子类中完成时,即在TestApp
中)It seems you want to access the
ids
of the dynamic classTest2
. You may achieve this as follows:First refer it by creating its
id
inkvlang
as,Now you should be able to access its inner ids as usual by
self.root.ids.test2.ids
(when done inApp
's subclass i.e. here inTestApp
)