我有一个想要在按下按钮时创建的“条纹”对象。我所拥有的是多个TextInputs和一个按钮。我希望按钮能够获取所有输入并将其存储在“ Streak”对象中。我将如何去做呢?这是.kv代码:
<ScreenOne>:
name: "one"
AnchorLayout:
anchor_x: 'left'
anchor_y: 'top'
Button:
on_release: app.root.current = "main"
text: "Back"
size: 50, 25
size_hint: None, None
font_size: 18
GridLayout:
cols:1
rows:6
Label:
text: "Action"
font_size: 18
size: 600, 50
size_hint: None, None
TextInput:
id: action_entry
multiline: False
size: 600, 28
size_hint: None, None
Label:
text: "Streak #"
size: 600, 50
size_hint: None, None
TextInput:
id: streak_entry
multiline: False
size_hint: None, None
size: 600, 28
Label:
size: 600, 50
size_hint: None, None
text: "Due every"
GridLayout:
cols:3
rows:2
Label:
text: "Day(s)"
size_hint: None, None
font_size: 18
Label:
text: "Hour(s)"
size_hint: None, None
font_size: 18
Label:
text: "Minute(s)"
size_hint: None, None
font_size: 18
TextInput:
id: day_entry
multiline: False
size_hint: None, None
size: 200, 28
TextInput:
id: hour_entry
multiline: False
size_hint: None, None
size: 200, 28
TextInput:
id: minute_entry
multiline: False
size_hint: None, None
size: 200, 28
AnchorLayout:
anchor_x: "right"
anchor_y: "bottom"
Button:
text: "Add"
size: 50, 25
size_hint: None, None
font_size: 18
on_press: Streak.create(instance)
这是.py代码:
class Streak():
def __init__(self, action, action_num, day, hour, minute):
self.action = action
self.action_num = action_num
self.day = day
self.hour = hour
self.minute = minute
class MainScreen(Screen):
pass
class ScreenOne(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("StreakStar.kv")
class MainApp(App):
def build(self): # build() returns an instance
return presentation
def create(self, instance):
streak = Streak(action_entry.text, streak_entry.text, day_entry.text,
hour_entry.text, minute_entry.text)
if __name__ == '__main__':
MainApp().run()
当我按下按钮时,我收到一个NameError:名称'Streak'未定义
答案 0 :(得分:0)
所有ID已存储在ids
库中-假设您想将此变量用作字符串,则可以串联所需的ID。
出现错误的原因是因为函数中先前未定义streak
。
def create(self):
streak = ""
streak = self.ids.action_entry.text +", "+
self.ids.streak_entry.text +", "+
self.ids.day_entry.text....
将为您提供这些ids
答案 1 :(得分:0)
当按下Add
按钮时,它尝试调用Streak.create(instance)
,但是没有名称为Streak
的实例化对象,因此,它引发了以下错误:
NameError:名称'Streak'未定义
Streak.create(instance)
替换为app.create()
Button:
text: "Add"
size: 50, 25
size_hint: None, None
font_size: 18
on_press: app.create()
create()
如下:def create(self):
obj = self.root.get_screen('one')
self.streak = Streak(obj.ids.action_entry.text, obj.ids.streak_entry.text, obj.ids.day_entry.text,
obj.ids.hour_entry.text, obj.ids.minute_entry.text)