How To Create A Splash Screen - Python Tkinter GUI Tutorial #139

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys john elder here from konami.com and in this video i'm going to show you how to do a splash screen with kinter and python alright guys like i said in this video we're going to look at splash screens but before we get started if you like this video you want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out codamy.com where i have dozens of courses with hundreds of videos that teach you to code use coupon code youtube1 to get 30 off membership that's all my courses videos and books for one time fee it's just 49 which is insanely cheap okay i've been getting a lot of questions about splash screens lately so i thought i'd do a video on it in fact we've already pretty much learned almost everything we need to learn in order to do splash screens throughout other videos in this playlist but i'm going to put it all together in this video and show you at least one way to do it now there are probably dozens of ways to do splash screens there because you're really just throwing up a window doing whatever you want to it setting it on a timer for three or five seconds and then making it go away right so that's all it is so we know how to make other windows with kinter we've done lots of videos on that we know how to use timers we've done a few videos on that so we're really just kind of putting it together with a little bit of a spin on it so it is monday morning here in vegas man it got cold last night we got a polar front came through in the desert it's crazy it's like well cold for us it's like 40 degrees outside so it's gonna warm up throughout the day but man it is cold right now so all right so we've got some basic kinder starter code that we always have here i've got a file called splash.pi and we're using the sublime text editor and get bash terminal as always so this is going to be a little bit different right off the bat we're going to get rid of this root or the main loop and i'll show you why in just a second we're just going to call a main loop so let's map this out let's think about what we want to do we want to create a little window that pops up right when our program first starts and it's going to be the splash screen and it's gonna say stay up for three seconds five seconds something like that so we need a window for that so instead of starting with the root stuff like we usually do i'm gonna kind of push this down a little bit let's just start up by creating right off the bat another window and i'm going to call this splash underscore root as opposed to just the regular root you can call anything you want and this is a tk instance capital t lowercase k right and then we can just do all the things we always do so splash splashroot.title and we actually aren't going to have a title bar i'll show you how to take that off but if you didn't want to take off the title bar you would put you know splash screen right whatever it's a lowercase p there and we could go splash dot geometry to pick whatever you know size we want so let's say 300 by 200 now i'm also going to go plus negative 1 500 plus 250 you will not do this right you do not do this i'm just doing this because i have two monitors and i want this splash screen to show up on this monitor so you guys can see it right off the bat so i don't have to drag it over because we're not gonna have a title bar for this later on so there's nothing for me to be able to drag over so i'm just going to have it like this so that it appears on this monitor but like i said you will take off this whole thing so yours will just be 300 by 200 and then quotation marks right okay so all right geometry now i'm gonna leave the title bar on there for now we'll take that off in just a second so we've got this thing and it's gonna it's gonna start right off the bat and i'm gonna grab this little bit of code here and put it down here too as well so if we just save this and run it now and let's go python dot pi oh oh type o it's monday morning splash underscore root there we go so okay that looks good now let's run this guy again and we can see both of these things just pop right up right so that's no good what we need is only the splash screen to start at first so what we can do is take all of this and put it in its own function so i'm going to define our own function and i'm going to call this main main window whatever right and then i'm going to grab all of this stuff and tab it over inside of this function so now if we save this we're not actually ever calling this function we're just calling this one right off the bat so if we save this and run it you'll see boom we just get the splash screen and nothing else and we can close it nothing else happens okay so we're getting there now we need to put that splash screen up only for a certain amount of time right so and actually so let's create a label i'm going to call this splash underscore label and this is going to be a label and we want to put this in splash underscore root now normally we put these in root but we want this in the splash screen so we're going to put it in splash root i'm going to give this a text of let's say splash screen exclamation point and let's give this a font equals let's say helvetica and like size 18 just to make it a little bigger so that we can read it and then we can splash underscore label dot pack this guy and let's give this a pad y of 20 just to push it down the screen a little bit now let's go ahead and save this and run it just to make sure i didn't mess that up because hey it's monday morning and okay it says splash screen so all right yippity dude no big deal but we've got something on there now now you would obviously put whatever you wanted on your splash screen there make it any size you want make it you know add images add graphics whatever you want you know how to do that already you can watch other videos from this playlist the link is in the comment section below if you wanted to do that but i'm just going to assume you already know what you want in your splash screen thing so i'm just going to leave it like that so okay now we want to get rid of this whole window after let's say three seconds so let me make a comment here so let's put splash screen timer now we've done timers before so let's go splash underscore root which is the window right which is what we've called it up here right so splash underscore root and let's go dot after and so let's say after three seconds three thousand milliseconds what do we wanna do well we wanna run this main window function so we can just boom call this main window function right so this will then throw up this new window but we also need to get rid of our splash screen right so we need to get let's just right off the bat let's just kill this guy and to do that we can call splash underscore root dot destroy okay so that will destroy this guy and then make all the rest of this stuff so okay let's give this a try see if this worked so let's run this guy 1001 2002 1003 boom disappears here's our main app and if we move this around we can see yep the splash screen is gone if we close this boom that's closed now one thing to notice everything we need to do for the rest of our app needs to be inside of this function now so for instance we want to create a label so let's go main underscore label and that's going to be a label and we want to put it in root and we want the text to equal main screen and let's give this a font equals trustee helvetica and make this size 18 so it's nice and big and then we can main underscore label.pack give this pad y of 20 just to push it down the screen a little bit okay so if we save this and run it now we can count 1001 1002 1003 boom disappears here's our main screen now what happens if we take it out of there poster to have problems right so i could just copy this and instead of putting it in this function let's put it outside of the function function see now it's not indented inside of this function here so save this and run it we get a we get an error because it's calling root right if we look at our code this label we're saying hey put it in a root but root doesn't exist yet because this function hasn't been called yet right so that's a problem so like i said everything you need to do you need to put it in this new function now you can move these around instead of putting this in the function you can put the splash root in the function and then you could call your main root window when your program starts and then you can like hide it de-iconify it we've looked at that in other videos if you need to know how to do that go check the video that i did on creating new windows to show you all about hiding windows i think it's been a while since i did that video but i think it says it in there if not let me know and i'll do a video on how to hide windows but you could just create this regular window off the bat right at the beginning and hide it and then throw up this thing on a timer and then delete it in the same way we just did right here after the timer extends or after the timer ends right and then you wouldn't have to put everything else of your app inside of a function like this or you could just do it like that it really doesn't matter it's monday morning and this is the first way that came to my mind when i thought about how to do this so i just did it this way like i said there are dozens of ways you can do this the main thing here is this timer right throwing up a timer and this is three seconds we could put it five seconds if we want now one thing to check out let's save this and run it let's look at our splash screen we still got this title bar at the top with these buttons up here right four or five boom disappears if we want to get rid of that we can do that which is a splash screen you probably want to get rid of that thing right um so to do that let's come up here where we defined this thing and let's go splash underscore root and this is going to be dot override redirect and we want to set that equal to true and let's make a little comment here let's say hide the title bar so that's over ride there we go ride redirect right so if we save this and run it you see the title bar now is gone and we just have a screen that's more like a splash screen right there's usually not a title bar and then it disappears and boom our main screen pops up so again down here we're calling main loop instead of the normal root dot main loop why is that well that's because remember our root window is only defined once this function gets called and when the program first starts there has this function hasn't been called yet so there is no root so if we called a root.main loop right here we'd get that same error again and say hey what's root right so it's like right here name root is not defined right so just to get around that we just call the main loop just like that and this tends to work so we can get away with it and there we go four five there you go and that's all there is to it so that's all for this video if you like to be sure to smash the like button below subscribe to that channel give me a thumbs up for the youtube algorithm and check out codename.com where you can use coupon code youtube1 to get 30. membership say page is 49 dollars to access all my courses over 47 courses hundreds of videos and the pdfs of all my best-selling coding books join over a hundred thousand students learning to code just like you my name is john elder from codenbu.com and i'll see you in the next video
Info
Channel: Codemy.com
Views: 27,394
Rating: undefined out of 5
Keywords: tkinter splash screen, tkinter splash screens, python tkinter splash screen, tkinter hide title bar, python tkinter hide title bar, tkinter timer, tkinter after method, tkinter after command, tkinter after, python tkinter after, tkinter after(), overrideredirect tkinter, tkinter overrideredirect, splash screen tkinter python, tkinter timer python, tkinter timer delay, tkinter timer countdown, splash screen python tkinter
Id: LTVvHObxc4E
Channel Id: undefined
Length: 11min 26sec (686 seconds)
Published: Mon Oct 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.