Build a Weather App - Python Tkinter GUI Tutorial #24

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from Cody be calm and in this video I'm going to show you how to build a weather app with kinter and Python alright guys in this video we're gonna build this very basic little weather air quality monitoring app with kinter and Python and in the next few videos we'll make this maybe a little bit more interesting in this video we're just gonna start out with the basics connect to a third party API grab some weather data bring it back do stuff to it put it out on the screen and so we're gonna do in this video but before we get started if you like this video want to see more like it be sure to smash the like button below subscribe to the channel and check out coding me comm where I have dozens of courses with hundreds of videos that teach you to code use coupon code youtube to get $22 off membership that's all my courses videos and books for one time via just $27 which is insanely cheap so alright it's been a while since we've done a tkinter kinder a tutorial so i'm gonna dive in with this one this is just a basic air quality monitoring thing so I think air quality think like smog pollens stuff like that I live in Vegas I love to hike in the mountains around here we're pretty high up above sea level so a bad ozone day could really sort of affect you more at these higher altitudes so I always want to check the smog and the the air quality before I go hiking and there's this cool website I'm gonna see if I can pull it up here air now gov and you can punch in your zip code and get the current you know air quality level in your area so you know good is green we have moderate this unhealthy for sensitive groups unhealthy for everyone very unhealthy and you know run for your lives so we're gonna build this app and you see right now it's green 38 if it was moderate this would be yellow right if it was unhealthy this would be red etc so we're gonna build out all this functionality in the next videos now there's an API that comes with this you can connect to to get this data so that's really what we're gonna be learning here how to connect to an API out on the internet with a kinter project so you know there are millions of API is out there and if you learn how to connect to them from kinter you can use it all of them so you know if you're not interested in air quality that's no big deal you still want to watch this video just to learn how to connect to api's and stuff like that so check out air now not gov and I should mention as we build this thing out in the next few videos we'll make this a little bit more interesting we'll make it bigger and we'll put maybe a search bar where you can look up specific zip codes and things like that for just for now in this video we're just gonna connect to this thing and do the very very basic stuff so first thing you want to do is head over to Doc's airnow API org and this is their website where they have the API stuff and just go over to the login page and see request an error now API account click this link and fill out this form it's completely free you just have to fill it out with your email address they'll email you a little link that you can click to you know prove you are who you are or whatever just go ahead and do that once you've done that come back here and just log in there we go and you'll see this page so go to the web services and there's all kinds of stuff we can see the forecast or where you see the current data and you can do it by zip code you can do it by lonna longitude and latitude if you happen to know your current latitude and longitude let's see you can do it by I think there's something yeah bounding box I'm not really sure what that is we just want Simcoe so if you want to quick take a look at the documentation here this is the stuff that's going to return so if you want to read through here it'll return the hour you know the local time the area so you know in ours here says Las Vegas it returns us Las Vegas so we know where the data is coming from it also returns the state you could put Nevada on there if you want to do I don't really care so I didn't put that on there it returns the latitude and longitude that's interesting right if you need to look up latitude and longitude for some other project and you just don't want to mess with it it's really hard use this it will reach you can enter a zip code into this thing it will return that line at latitude longitude that might be a good lookup tool for you in the future and keep an eye on Kiba keep that in mind parameter name the AQI this is the actual number now this thing will return several numbers it will return the AQI which is this which is an average if I guess these three so it returns ozone level particles pm10 and particles PM 2.5 and PM 10 and 2.5 those are just the size of the particles so these are particles under 10 I don't know millimeters whatever unit of measure they use and this one's 2.5 I'm gonna say millimeters you can click on here there's a frequently asked questions somewhere yeah fact sheet maybe all right here fact your last questions gonna go into all that if you're interested we don't really care what else category number and category name these are the good moderate unhealthy you know run for your life numbers that sort of correspond to these things down here so that's cool okay so that's the documentation not to use this thing we have to create a query so they have a little query tool it's very cool you could just enter your zip code put the radius you want I'm going to put 5 miles and then here it lets you select what kind of data you want to receive no we want a JSON you almost always want to use JSON when you're getting API data so now you click build now this is your URL you can grab this and use it you can also run it just to see this is the stuff it's gonna return if you're interested in that so ok we're good to go now so I'm gonna head back over I've just created a new file called weather dot pi this is the same sort of starter code that we've always used I'm gonna change this really quickly though to 400 by 50 so it's smaller right and I just wanna I'm just gonna paste in our URL just for future reference ok so now we want to talk about how exactly do you connect to a third party API out in the World Wide Web the wild wild web and then bring the data back into your your program in your project well there's lots of different ways you can do it I always use something called requests and it is it's just imported here import requests and notice that it's plural we also need to decode the stuff that it brings back now it's bringing it back as JSON so we need to use and we need to import JSON now JSON JavaScript object notation comes with Python but requests does not so a head over to your terminal and I'm just in our c GUI directory where we've been doing all the stuff throughout this playlist and we just want a pip install requests i've already done this so it says hey you've already got this but you probably donate will download and install it for you and you're ready to go so that's really all we need in order to start using this request and i think you're gonna be surprised just how easy this is so what we want to do is let's just create a variable i'm gonna call it api request because that's what we're gonna do and this is gonna be a requests dot get and then oops you want a spell request right requests right and that's just this guy right up here right we're gonna get and then we just want to paste in our our whole query URL that we created just a minute ago and you notice it has my api key and I don't use my API key I'm gonna delete it as soon as I finish recording this video so it won't work for you go ahead and sign up for your own and where do we get that API key well it put it in its you see it's right here but it it added it for us automatically because we're logged in here so it knows who we are it knows what our API key and it just puts it in there for so that's very cool all right pull this back up ok so we're almost there now we want to create a new variable it's called API you call it anything you want and set it equal to JSON loads and we just want to pass in whatever content we got from this so we go that dot content right and that's pretty much it right so actually I'm gonna change this back to 404 now now we need to do something else we need to set up a little bit error handling so let's go try we want to put this in a try block right so if it doesn't work we want to throw an exception so let's go accept call an exception as II and there we go and inside of here let's say API to just error for now right so basically what we're gonna do is we're gonna say hey go back go get this actually we should probably just put all of this in the there we go inside of here right so basically we're gonna say hey try to go get this information from this URL once you get it try and parse it you know strip it out of its JSON and make it into a Python useable thing if there's a problem if you can't connect to the website if something goes wrong throw an error where instead of our output being API the output is error so then when we put API on to the screen it'll say error instead of the stuff right so okay now let's just create a label let's call it my label call it whatever you want and set this is just a kinter label and so we want to put it in root we want the text to equal a P I and that's pretty much it for now I think and now we just want to pack that onto the screen so let's just go my label pack okay so let's go ahead and save this and have a do for now so let's head over to our terminal and let's run this Python weather type i/o invalid syntax oh yes it is we need it : right obviously all right so save that let's try this again all right so here we have our box right and let's make this because we can you can see there's all kinds of stuff in here and it's just kind of spewed it up onto the screen right so what is all that stuff well it's just this exact stuff right it's returned it looks like a Python list right and we know that because it has square brackets on the outside so it's a list but inside of the list is a Python dictionary and we can tell that by the squiggly brackets so I'm just going to copy this real quick and instead of looking at it in the GUI I'm just gonna oops it's gonna create a new file real quick and paste this in here so okay notice these square brackets on the outside so that's a list and then notice the squiggly brackets on the inside but let's look through here we've got looks like up here is one list item right so we have an opening squiggly bracket and closing squiggly bracket here is the second list item so let's go through here opening squiggly bracket closing squiggly bracket and here's the third list item now this is what you're always gonna do with API data you're gonna see how it gets returned and then you're gonna just figure it out because no two api's return data in the same way some api's return them as dictionaries some return them as lists some return them as lists with dictionaries in them like we're seeing here you just have to look through here and figure it out to figure out what it is so we're seeing a list with three items in it right so how do we sort of grab the first item well you remember Python lists start at 0 they're numbered right so 0 1 2 so there's 3 items but they start at 0 1 2 so this is the second item right here oops right here right this is the first item and this is the 0th item if we look through these things they're all the same except for this the first one is just returning the AQI the average of all three of those things Oh no actually the first one here is just returning ozone the second one is returning that PMI 10 maybe not PMI 2.5 XT right here and the third one is returning PMI 10 if we can find it yep right there so for me I don't really care about the PMI is that's sort of like pollen count you know little bits little particles of pollen I just want the ozone if you wanted those you can grab those so we just want this first thing which is the zero a thing right so to get that just grab our code here and what we want is instead of putting on our label API just the whole API we can call the specific item so if we want the zeroth item we could save this and let's run this guy again real quick goodnight didn't I fix that I must have accidentally deleted it okay so save that but the colon there for sure all right let's try this again now it's kind of hard to tell but we only have we could see one thing the date observed hours observed we don't care reporting area Las Vegas AQI 38 in the category number now notice the category number itself is another dictionary because it has two items in it so we'll have to play with that a little bit so okay we're getting there so what do we want we actually just want well we want three things we want this reporting area we want the actual AQI and we're gonna want this category so one thing at a time let's grab the reporting area how do we just make that appear well pretty simple we just drill down even further and inside a parentheses go reporting area alright so if we save this let's run this guy boom we have just Las Vegas right if we want just the AQI instead of reporting area we go AQI now EQI is the actual number right the air quality indicator or index or whatever and right now we can see it's 38 very cool what was the other one let's look at our code here the other one was category so if we go category who's come back he remember this one was a little different right it returns a dictionary with the number and the name we don't know we don't care that out the number we want the name we wanted to say good air quality if we returned one what would that mean no we want the name so how do we get the name well let's see code backup we once again just keep stringing these guys along right and boom we get good so alright that's how you do that but now this is all we don't want to keep doing this inside of our label because that's just kind of weird so let's come up here now let's create some variables so let's go City equals and let's go eight let's go quality well lit T equals and let's go what do we call this category equals and we'll put this one as category just paste in all of these actually so the city was what was that reporting area I think reporting area your that and the quality was just a Qi I believe if there's no space there reporting area let me check to make sure uh yeah reporting area back we could just copy and paste that okay a Qi and category name okay so we've got our three variables here instead of doing it like this let's just sort of how do we want to do this let's go well the first one we want is city and then let's concatenate and let's type in air quality and concatenate again and we want equality but this is a number equality returns a number and this needs to be a string because we're putting these all in strings so let's real quick wrap this in a string function we could have done that up here I guess but this is fine too so air quality and what else let's go let's put a space and then let's just finally put the category for now just to make sure we got it right cat - Gauri all right so let's see if this run it see what errors I made because I always make errors as you know okay so Las Vegas air quality 38 good all right so we're getting there all right so let's play around with this a little bit more now I'm gonna change this from 400 to 50 and we can actually change the font size of our label just by coming up here and at the end of our label stuff you just type in font and now we can pass in let's go I want this [Music] Helvetica despot right I think so and I want font size 20 so let's save this run it see how that looks okay so Las Vegas air quality 38 good this might be a little bit smaller so let's play around with this just a little bit because I like to play around with these things so instead of 50 let's try 40 alright that looks better I think now we have good as the air quality and so we want this thing to change color based on that and this video is getting a little bit long so I'll just start to do that in this video we'll pick that up in the next video but for now just at the end of this in our label I'm just gonna put another comma and we can just type in background and set that equal to green now we'll do some logic later on to decide what color to put in there based on what the actually AQI number is but for now I'm just gonna put green you just sort of wrap this up for today and make it look nice okay so that's starting to look good but now there are rest of it oops there we go there's white around the edges and stuff so the background of our thing needs to be changed as well so will you do that a bunch of different ways probably the easiest for right now is just to come up here to our configuration of the actual project and I can go a route dot configure and then just set the background to I don't know green for now we use hex color codes in the future so save this and let's come back here run it one more time all right so now we have Las Vegas air quality 38 good and we can put hyphens in the middle of this if we wanted to or like right here or something but we're gonna change this later anyway so we'll just leave it like this for now so okay that's pretty much it for this video now let's look at this one more time very quickly because this was insanely easy right we're connecting to a third party API by doing nothing more than calling this requests which we pip installed right telling it hey go out and get this URL bring it back here and you know convert it from JSON into a Python list which then we just broke apart and slapped it into a label just that easy and that's one of the really nice things about Python you know if you do use like node for instance running out and getting stuff from an API with node can be kind of a hassle because it's asynchronous and it gets crazy this is just as easy as can be and I think you'll probably see that from giving this a shot so all right I think that's all for this video the next video will dive into this a little bit more detail we'll start working on the logic of the color scheme and start adding more functionality eventually we'll add a search form so you can fill out your zip code and get the actual thing and you know make some other cool things whatever you guys want to do leave me some comments to tell me what you want to do and we'll go from there so that's all for this video if you liked it be sure to smash the like button below subscribe to the channel and check out Kota Meachem or you can use coupon code youtube if you get twenty two dollars off membership pay just $27 to access all my courses hundreds of videos and the PDFs of all on best-selling coding books join over sixty thousand students learning to code just like you my name is John elder from khou-tv comm we'll see in the next video
Info
Channel: Codemy.com
Views: 29,270
Rating: undefined out of 5
Keywords: python, tkinter, gui, python tkinter, tkinter python, python gui, tkinter gui, gui python, graphical user interfaces python, tkinter grid system, grid system tkinter, python grid system, build graphical user interfaces with python, how to build graphical user interfaces with python, how to python tkinter, tkinter API, API with tkinter, tkinter weather app, using api's with tkinter, python api, python tkinter api
Id: vJCjDevYDt8
Channel Id: undefined
Length: 22min 57sec (1377 seconds)
Published: Mon Sep 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.