##### main.py #####
from kivy.app import App
from kivy.app import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty, ObjectProperty, StringProperty, ListProperty, BooleanProperty
class MainScreen(Screen):
def __init__(self,**kwargs):
super(MainScreen,self).__init__(**kwargs) # implements the features of a GridLayout (the base class of MainScreen)
def save(self):
'''Saves the data from the input to a text file.
It is bound to the save button'''
status = self.ids.workout1.ids.status.active
workout = self.ids.workout1.ids.workoutInput.text
day = self.ids.workout1.ids.dayInput.text
time = self.ids.workout1.ids.timeInput.text
with open('workoutData.txt','a') as f:
f.write('%s, %s, %s, %s\n' %(status, workout, day, time))
return None
def totalTime(self):
'''This function doesn't do anything right now! Add code to compute the total exercise time, and document it in this string'''
return None
class WorkoutLayout(BoxLayout):
pass
class SecondScreen(Screen):
pass
class AppScreenManager(ScreenManager):
name = StringProperty('Num Num') ### <----line 37
def __init__(self,**kwargs):
super(AppScreenManager,self).__init__(**kwargs)
self.name = 'Num Num' ### <----line 40
class WorkoutApp(App):
def build(self):
sm = AppScreenManager()
print (sm.name)
# sm.name="123"
sm.add_widget(MainScreen(name='main'))
sm.add_widget(SecondScreen(name='secondary'))
return sm
if __name__=='__main__':
WorkoutApp().run()
# NOTE: running is diff on diff platforms
# python main.py
# kivy main.py
##### part of workout.kv #####
<MainScreen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
Label:
text: 'Workout'
size_hint_x: 2/3.
Label:
text: 'Day'
size_hint_x: 1/6.
Label:
text: 'Time'
size_hint_x: 1/6.
WorkoutLayout:
id: workout1
WorkoutLayout:
id: workout2
WorkoutLayout:
id: workout3
WorkoutLayout:
id: workout4
WorkoutLayout:
id: workout5
WorkoutLayout:
id: workout6
WorkoutLayout:
id: workout7
BoxLayout:
orientation: 'horizontal'
Button:
text: 'Save'
on_press: root.save()
Button:
text: 'Next screen'
on_press: root.manager.current = 'secondary'
<SecondScreen>:
BoxLayout:
orientation: 'vertical'
Label:
size_hint_y: 3/4.
text: root.manager.name ### <----line 45
# text: 'Hello second screen!' ### <----line 46
Button:
size_hint: (1/4.,1/4.)
text: 'Back to main screen'
on_press: root.manager.current = 'main'
<WorkoutLayout>:
size_hint_x: 1
orientation: 'horizontal'
CheckBox:
id: status
size_hint_x: 1/8.
TextInput:
id: workoutInput
size_hint_x: 5/8.
TextInput:
id: dayInput
size_hint_x: 1/8.
TextInput:
id: timeInput
size_hint_x: 1/8.
TextInput:
multiline: False
這些都在線上修改了一些示例代碼。當我編譯時,它說"NoneType" object has no attribute "name"
,在我的kv文件中第45行中有一個錯誤。我在第37或40行的main.py中嘗試發表評論,但仍然有同樣的錯誤。Kivy編譯錯誤「NoneType」對象沒有「名稱」屬性
如果我在workout.kv第45行中註釋並取消註釋第46行,則可以編寫代碼,main.py中的第46行可以打印出「NumNum」,這意味着sm確實具有屬性名稱。