MDBottomAppBar(Kivymd)异常:仅显底色无控件问题求助
问题解决:MDBottomAppBar仅显示底色,控件消失
问题原因
- right_action_items格式错误:底部导航栏内的MDTopAppBar的right_action_items未使用正确的嵌套列表格式,导致Kivy无法解析动作项,控件无法正常渲染。
- Window模块未导入:ScrollView高度计算依赖Window,但Python代码未显式导入,可能引发潜在运行问题。
修改方案
1. 修正KV文件中的right_action_items格式
将底部导航栏的right_action_items修改为正确的嵌套列表结构(所有动作项包裹在一个外层列表中):
<MDBottomAppBar: MDTopAppBar: id:bottom_app_bar elevation:20 title:"My Bottom Bar!" icon:"plus-circle" type:"bottom" icon_color: 1, 0, .4, .1 md_bg_bottom_color: "orange" left_action_items:["menu",lambda x:app.chanje_mounyo()] right_action_items: [["gmail", lambda x:app.chanje_mounyo()], ["bookmark", lambda x:None], ["instagram", lambda x:None]]
2. 在Python代码中导入Window模块
在Python文件开头添加Window导入语句,确保高度计算逻辑正常运行:
from kivy.core.window import Window
修改后的完整代码
KV文件(mybanner.kv)
<EkzampBanner@Screen> MDBanner: id:banner text:["Yon sel liyn teks san aksyon."] over_widget:scroll vertical_pad:toolbar.height MDTopAppBar: id:toolbar title:"My banner for me!" md_bg_color:.3,.3,.4,.1 elevation:20 left_action_items:["menu",lambda x:app.change_items()] right_action_items:["dots-vertical",lambda x:app.change_items()] pos_hint: {'top':1} MDBottomAppBar: MDTopAppBar: id:bottom_app_bar elevation:20 title:"My Bottom Bar!" icon:"plus-circle" type:"bottom" icon_color: 1, 0, .4, .1 md_bg_bottom_color: "orange" left_action_items:["menu",lambda x:app.chanje_mounyo()] right_action_items: [["gmail", lambda x:app.chanje_mounyo()], ["bookmark", lambda x:None], ["instagram", lambda x:None]] ScrollView: id:scroll size_hint_y:None height:Window.height - toolbar.height MDGridLayout: id:box adaptive_height:True cols:1 padding:"10dp" spacing:"10dp" OneLineListItem: text: "ThreeLineBanner" on_release: banner.type = "three-line" banner.text = \ [\ "Three line string text example with two actions.", \ "This is the second line of the banner message.", \ "and this is the third line of the banner message.", ] banner.left_action = ["CANCEL", lambda x: None] banner.right_action = ["CLOSE", lambda x: banner.hide()] banner.show() OneLineListItem: text: "TwoLineIconBanner" on_release: banner.type = "two-line-icon" banner.text = \ [\ "One line string text example with two actions.", \ "This is the second line of the banner message.", \ ] banner.left_action = ["CANCEL", lambda x: None] banner.right_action = ["CLOSE", lambda x: banner.hide()] banner.show() OneLineListItem: text: "OneLineIconBanner" on_release: banner.type = "one-line-icon" banner.text = ["One line string text example with two actions."] banner.left_action = ["CANCEL", lambda x: None] banner.right_action = ["CLOSE", lambda x: banner.hide()] banner.show() OneLineListItem: text: "Banner without actions" on_release: banner.type = "one-line-icon" banner.text = ["One line string text example without actions."] banner.left_action = [] banner.right_action = [] banner.show() OneLineListItem: text: "Banner with one actions" on_release: banner.type = "one-line-icon" banner.text = ["One line string text example without actions."] banner.left_action = [] banner.right_action = ["CLOSE", lambda x: banner.hide()] banner.show()
Python文件
from kivy.lang import Builder from kivy.factory import Factory from kivy.core.window import Window from kivymd.app import MDApp from kivymd.uix.screen import MDScreen class EkzampBanner(MDScreen): pass class MyBanner(MDApp): def change_items(self): self.root.ids.bottom_app_bar.right_action_items=[["whatsapp"],["facebook"],["twitter"]] self.root.ids.bottom_app_bar.md_bg_bottom_color="lightgray" self.root.ids.bottom_app_bar.icon="account-circle" self.root.ids.toolbar.md_bg_color = "#FFA500" self.root.ids.bottom_app_bar.icon_color=[0, 0, 1, 1] def chanje_mounyo(self): self.root.ids.bottom_app_bar.md_bg_bottom_color="orange" self.root.ids.bottom_app_bar.icon="account-circle" self.root.ids.toolbar.md_bg_color= [.3,.3,.4,.1] self.root.ids.bottom_app_bar.icon_color=[1, 0, .4, .1] def build(self): Builder.load_file('mybanner.kv') return EkzampBanner() if __name__=='__main__': MyBanner().run()
内容的提问来源于stack exchange,提问作者Piets
相关产品推荐
相关产品推荐

