 Hey everyone, welcome back once again to more nPyScreen module library learning within Python. Cool. So in the last video we were checking out forms and that was one of the three kind of crucial and essential aspects of our nPyScreen applications. We see here in the documentation they talk about form objects, widget objects, and application objects. So I'm kind of going to go out of order here just because I think that's better for your learning kind of going to step it away from the book in this case but I want to show off application objects and how we're going to better our framework now. In fact in our code we're going to go entirely object oriented and that sounds pretty crazy. I know but that's what we're going to do So I'm going to save this as 03.python and you guys might want to do the same if you guys are keeping these files as notes or something or I don't know leaving comments for yourself. That's probably a pretty cool thing to do. But okay, let's get into apps and applications. You can see these application objects and actually the next section in the documentation. I scroll down and we're at the very bottom of the page here. It's going to take us to our application objects page. So here I'll read through some of this and we can get a better understanding for it. NPS app managed provides a framework to start and end your application and to manage the display of various forms that you've created in a way that should not create recursion death problems. Unless you have exceptionally good reasons to do otherwise, NPS app managed is probably pretty much certainly the best way to manage your application. Now there is another one the NPS app class, but unlike this one NPS app to manage you don't have to write your own main loop. NPS app managed will manage the display of every single form on your application so you just set up the objects and then call the run method and you're good to go. So there are different of course functions that do all these really cool stuff with and we'll get into more of those real real soon. There is of course the run function and that's what we're going to be using to actually start the program. And we'll read through a lot more of this documentation as we read our code. So let's try to gather my thoughts here down at the very bottom of the page. It explains more of the NPS app class. So that's the other option that we have when we're working with our application objects. But it says here to use this NPS app subclass you have to subclass it of course and provide your own main function definition. When you're ready to run the application you call run and your main loop will be executed. Now this provides the maximum flexibility but it's in almost every other way inferior to NPS apps managed. So don't use it for projects and kind of regarded as an internal base class only. Okay so that's kind of a clear indicator to us that the developer and the documentation everyone kind of wants us to be using this NPS app managed class. So let's do that. Hop on over to our code here and we'll kill this simple function because we don't need it anymore and we'll call we'll remove our simple wrapper call. So here we go let's create a new class because we want to subclass our NPS app to manage class. So I'll just call mine app you can of course call yours whatever you'd like and you're going to need some parentheses because we're inheriting and subclassing and pi screens class. Now remember we're using NPS app managed I'm assuming NPS stands for and pi screen. And we'll just pass right now we won't do anything with it but let's actually go down to our our our main call here and I'll show you how we can get it to work. So we'll create a new object remember I'm using a lowercase a for the object and a capital A for the class. So app create the object and it said we can run dot run right down here right and that'll start the applications main loop. This will activate the default form which should have given the ID of main. I don't really know what that means yet but let's let's see what our code will do right now if I say app app equals app then run create the object and we run it. We can print out object oriented programming is the best framework even though it may or may not be depending on your ideas and what you're actually writing. OK so let's call zero three run this code and what the heck we get an error what's this say here so in our file in our app dot run line. Lots of lots of garbage that we don't know and understand because it's all about how the library works it tells us key error main. What is that of course we just saw me in the documentation last next act of form equals self forms self next act of form. Huh OK let's take a look at that we go back to our documentation and it tells us here we start an NPS apps manage application with the main loop it activates the default form which would have been given an ID of main. OK so I'm sorry here I want to show you how this works I want to show you what we're missing out on now that we have our application object we of course need the same form that we had earlier so I'll call mine class. Npy screen underscore form or we want to call it doesn't really matter but form object and I'm using camel case here with the capital F and capital O so I guess not camel case but uppercase. And we're going to inherit of course from the end pie screen dot form object we're going to subclass it now. So we of course created an end pie screen form earlier when we called form equals end pie screen dot form but now we're actually going to try and make it our own we're making an object out of that predefined class for us. So there are some certain functions that come with this class and the ones that are called by NPS managed are the ones that we want to know because of course this app manager is it's going to manage our form. If we check out the documentation one more time I'll scroll down a little bit more. I want to know what it does with certain forms right. Will it tell me in here. OK it looks like it doesn't tell me on this page but it does in the next one for the form objects page so that's kind of a good segue getting actually into the new form objects page. I'm going to skip over some of this right now because I want to show you the actual method that we need to act as our constructor and actually initialize things so form dot create. This method is what's called as pretty much by the forms constructor it default by default sorry it does nothing and it's there for you to kind of override in your subclass which is what we're doing right now. It's best to set up all the widgets on a form expect this method to be called to be full of self dot add method calls so when we're adding widgets to it. OK cool so all of that jazz was just for me to say we need to type in define the function create. Of course it's an object so it needs a self keyword and we'll just have it pass right we're not going to add any widgets just yet but now we'd add it to our application manager or our app object. So what does it do how do we connect the application object manager and our form object. Now if I go back way down to the previous page in the documentation it was talking about the application objects. We can of course add forms this way and that's pretty much what we're going to have to do this is the function we need to call we need to add a form with the form class or the form ID. But since we're an object oriented programming what do we do that in it's not like we can just do it really nearly in the class we need a function. So doing a little bit more reading and kind of looking around a little bit more at the documentation. OK here we go additional services offered by nps apt manage these are usefully overridden by subclassing the class by default they do nothing. The one that I want to look at here is nps apt manage dot on start this is the function that you override to perform any initialization pretty much this is where you set up your forms. So that's what we're going to do define I've got caps lock on on start notice that always lowercase and s is capitalized. We'll say the self keyword because this is an object and for our self for our object we want to run that function we saw earlier add form. Cool now what do we pass in as arguments and back over to our documentation check it out ID ID what is that ID should be a string that uniquely identifies the form. Well is that is that our object name it's not like we're creating the object we haven't created all we've done was created an app object. So here's where I'm getting at that error that we saw earlier key error or main is what we need to pass in there a string as referring to the main form of the main screen that we're actually looking at. You'll notice run what we're actually calling at the bottom of our code here to get the main loop to begin. It starts a managed application in the main loop and it method will activate the default form which should be to have been given an ID of main. So hey that's exactly what we pass in here pass in main as the ID and I need to know what form is this so we'll say form object. And any of the arguments that you pass in after this are arguments that will be passed to the object or the form objects constructor. So we'll give it the name here we'll say n pi screen form now when I run this let's see what happens. On the code hey cool n pi screen form it's being displayed for us it's pretty nifty I'll hit enter to I'll hit enter to get out of the program. But it doesn't get out of the program why is that what's it doing come on come on program right. Okay you have to use control C to break out of this right now and I'm using this again as another segue to transition to the next tutorial where we talk about the next active form. And that's what we can use to kind of leap from this concept to the next concept. So thank you for sticking with me guys I know this was probably a little bit of a longer one and you kind of had to bear with some of the stupid stuff that I did. But hopefully you guys understand now we've got our object oriented programming framework with our application object and of course our form object. Yeah of course we haven't gotten any widgets yet and it's pretty much useless so far but we're still learning it's that's part of the meat and potatoes of all this stuff. Alright thanks again guys I'll see you in the next tutorial.