我是kivy和kivymd的新手。我想从文本输入中获取文本。但是我在从按钮中调用python文件中的Submit函数时遇到了问题。我使用了导航抽屉代码 >。 我是否必须在类ContentNavigationDrawer 中定义 submit函数或 TestNavigationDrawer类? 提前致谢 以下是代码
.py
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty, ListProperty
from kivymd.uix.list import OneLineIconListItem, MDList
from kivymd.app import MDApp
class ItemDrawer(OneLineIconListItem):
icon = StringProperty()
text_color = ListProperty((0, 0, 0, 1))
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class TestNavigationDrawer(MDApp):
namee = ObjectProperty()
def build(self):
kv= Builder.load_file("screenmd.kv")
self.theme_cls.primary_palette = "Red"
return kv
def submit(self):
print(self.namee.text)
TestNavigationDrawer().run()
.kv
<ContentNavigationDrawer>:
orientation: "vertical"
padding: "8dp"
spacing: "8dp"
AnchorLayout:
anchor_x: "left"
size_hint_y: None
height: avatar.height
Image:
id: avatar
size_hint: None, None
size: "56dp", "56dp"
source: "data/logo/kivy-icon-256.png"
MDLabel:
text: "KivyMD library"
font_style: "Button"
size_hint_y: None
height: self.texture_size[1]
MDLabel:
text: "kivydevelopment@gmail.com"
font_style: "Caption"
size_hint_y: None
height: self.texture_size[1]
ScrollView:
MDList:
OneLineListItem:
text: "Screen 1"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 1"
OneLineListItem:
text: "Screen 2"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 2"
Screen:
MDToolbar:
id: toolbar
background_color: app.theme_cls.primary_dark
pos_hint: {"top": 1}
elevation: 10
title: "Iota World"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
NavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
Screen:
name: "scr 1"
namee:namee
MDTextField:
id:namee
hint_text: "Name"
helper_text:"forget"
pos_hint : {'center_x':0.5 ,'center_y':0.5}
size_hint_x : None
width: 300
MDRectangleFlatButton:
text: "Submit"
pos_hint : {'center_x':0.5 ,'center_y':0.4}
text_color: 0, 0, 1, 1
md_bg_color: 1, 1, 0, 1
on_release:
print("success")
app.submit()
Screen:
name: "scr 2"
MDLabel:
text: "Screen 2"
halign: "center"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
id: content_drawer
错误
print(self.namee.text)
AttributeError: 'NoneType' object has no attribute 'text'
答案 0 :(得分:0)
您需要在打印声明中使用self.root.ids.namee.text
而不是self.namee.text
。