Python 如何通过id获取2个文本输入值
所以我有一个类,它必须创建任务,它看起来像这样Python 如何通过id获取2个文本输入值,python,kivy,Python,Kivy,所以我有一个类,它必须创建任务,它看起来像这样 class CreateTask(Screen): def CreateTask(self,*args): bl = BoxLayout(orientation="vertical",spacing=10) name=self.root.ids.TaskName.text desc=self.root.ids.TaskDesc.text bl.add_wid
class CreateTask(Screen):
def CreateTask(self,*args):
bl = BoxLayout(orientation="vertical",spacing=10)
name=self.root.ids.TaskName.text
desc=self.root.ids.TaskDesc.text
bl.add_widget(name)
bl.add_widget(desc)
self.root.MainLayout.add_widget(bl)
但是我得到的错误是“CreateTask”对象没有属性“root”,那么如何通过id从2个TextInputs获取文本呢
这是我的kv代码
<CreateTask>
name:"third"
AnchorLayout:
size:root.size
anchor_x:"center"
anchor_y:"top"
BoxLayout:
orientation:"vertical"
size_hint:[0.6,0.6]
Label:
text:"Input Task Name"
TextInput:
size_hint:[0.2,0.2]
id:"TaskName"
TextInput:
size_hint:[0.6,0.6]
id:"TaskDesc"
multiline:False
Button:
size_hint:[0.4,0.4]
text:"come_back"
on_release:
app.root.current ="second"
root.manager.transition.direction = "left"
Button:
size_hint:[0.4,0.4]
text:"Create"
on_release:root.CreateTask(*args)
另外,制表似乎有错误,没有,只是粘贴成这样,'CreateTask'对象没有属性'root',因为'self'已经是根。并且不要使用字符串作为id
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
class CreateTask(Screen):
def CreateTask(self, *args):
bl = BoxLayout(orientation="vertical",spacing=10)
name=self.ids.TaskName.text
desc=self.ids.TaskDesc.text
lbl_name = Label(text=name)
lbl_desc = Label(text=desc)
bl.add_widget(lbl_name)
bl.add_widget(lbl_desc)
self.ids.MainLayout.add_widget(bl)
sm = ScreenManager()
Builder.load_file("mainscreen.kv")
sm.add_widget(CreateTask())
class MainScreenApp(App):
def build(self):
return sm
if __name__ == "__main__":
MainScreenApp().run()
姓名:“第三”
主持人安排:
大小:root.size
主播x:“中心”
主播:“顶部”
盒子布局:
方向:“垂直”
尺寸提示:[0.6,0.6]
标签:
文本:“输入任务名称”
文本输入:
大小提示:[0.2,0.2]
id:任务名
文本输入:
尺寸提示:[0.6,0.6]
id:TaskDesc
多行:False
按钮:
尺寸提示:[0.4,0.4]
文字:“回来”
发布时:
app.root.current:“秒”
root.manager.transition.direction:“左”
按钮:
尺寸提示:[0.4,0.4]
文本:“创建”
发布时:root.CreateTask(*args)
盒子布局:
尺寸提示:[0.4,0.2]
id:主布局
方向:“垂直”
按钮:
尺寸提示:[0.3,0.3]
文本:“添加任务”
发布时:
app.root.current=“第三个”
root.manager.transition.direction=“右”
请发布一个。是的,它确实解决了问题,但现在我得到了AttributeError:“super”对象没有指向self.ids.MainLayout.add\u小部件(bl)的属性“getattr”。是否有其他方法通过id获取boxlayout对象并向其添加新元素?是否将id:“TaskName”更改为id:TaskName?是的,我将每个id从字符串更改为普通外观什么是MainLayout?您没有在代码中定义它抱歉,现在我将它添加到我的问题中,它是在另一个类中定义的,只是一个简单的BoxLayout
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
class CreateTask(Screen):
def CreateTask(self, *args):
bl = BoxLayout(orientation="vertical",spacing=10)
name=self.ids.TaskName.text
desc=self.ids.TaskDesc.text
lbl_name = Label(text=name)
lbl_desc = Label(text=desc)
bl.add_widget(lbl_name)
bl.add_widget(lbl_desc)
self.ids.MainLayout.add_widget(bl)
sm = ScreenManager()
Builder.load_file("mainscreen.kv")
sm.add_widget(CreateTask())
class MainScreenApp(App):
def build(self):
return sm
if __name__ == "__main__":
MainScreenApp().run()
<CreateTask>
name:"third"
AnchorLayout:
size:root.size
anchor_x:"center"
anchor_y:"top"
BoxLayout:
orientation:"vertical"
size_hint:[0.6,0.6]
Label:
text:"Input Task Name"
TextInput:
size_hint:[0.2,0.2]
id: TaskName
TextInput:
size_hint:[0.6,0.6]
id: TaskDesc
multiline:False
Button:
size_hint:[0.4,0.4]
text:"come_back"
on_release:
app.root.current: "second"
root.manager.transition.direction: "left"
Button:
size_hint:[0.4,0.4]
text:"Create"
on_release: root.CreateTask(*args)
BoxLayout:
size_hint:[0.4,0.2]
id:MainLayout
orientation:"vertical"
Button:
size_hint:[0.3,0.3]
text:"Add Task"
on_release:
app.root.current="third"
root.manager.transition.direction = "right"