Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

【初心者です】お天気アプリにエラーが出てしまいます【Python】

【初心者です】お天気アプリにエラーが出てしまいます【Python】

Pythonアカデミー【エンジニアVthuber凜】様の「Pythonでお天気アプリ作ってみた!!」という動画を基にPythonでお天気アプリを作ったのですが、実装中以下のエラーが発生しました。
URL → https://www.youtube.com/watch?v=I3xCXjpYi0c&t=77s
どなたか解決方法がありましたら教えて頂けると幸いです。

発生している問題・エラー

import tkinter as tk
import requests #お天気APIを取得

#メインウィンドウの作成
canvas=tk.Tk()
canvas.geometry("700*500")
canvas.title("Today's Weather")
a = ("Arialblack",20,"bold") #最低・最高気温を表す文字フォント
b = ("Arialblack",40,"bold") #都市入力・天気の結果を表す文字フォント

#お天気情報を得るためのget_weather関数
def getweather(canvas):
    city = textField.get()
    api = "https://api.openweathermap.org/data/2.5/weather?q"+"city"+"&appid=" #ここにopenweatherのappidを入れています 
    json_date = requests.get(api).json()
    weather = json_date['weather'][0]['main']
    temp = int(json_date['main']['temp'] - 273.15)
    min_temp = int(json_date['main']['temp_min'] - 273.15)
    max_temp = int(json_date['main']['temp_min'] - 273.15)

    #アプリに表示する項目
    final_info = weather + "\n"+str(temp)+"℃"
    final_date =  "\n"+"最低気温:"+str(min_temp)+"℃"+"\n"+"最高気温:"+str(max_temp)+"℃"
    label1.config(text=final_info)
    label2.config(text=final_date)

#テキストボックス作成
textField = tk.Entry(canvas,justify='center',width=20, font=b)
textField.pack(pady = 30)#pady=外側の縦の隙間
textField.bind('<Return>',getweather) #APIの天気情報を返す

label1 = tk.Label(canvas,font=b)
label1.pack()
label2=tk.Label(canvas,font=a)
label2.pack()

canvas.mainloop()

上記の実装で、以下のエラーコードが出ます。

File"C:\Program Files\Python310\lib\tkiner_init_.py",LIne 1921,in_call_
return self.func(*args)
File"",line 5 ,in getweather
KeyError: 'weather'
NameError (uninitialized constant World)

どなたか解決方法をご存じの方コメントを御願いします。

0 likes

2Answer

Comments

  1. @ak-olive

    Questioner

    コメントありがとうございます!
    json_dateを書き換えてみましたが、どうやらエラー箇所はこちらではなかったようでした…。

コードには5つ問題があります。

①tkinterの大きさでエラー(6行目)
②全角スペースが入っている(14行目)
③API Keyがない(14行目)
④URLがおかしい(14行目)
⑤改行コードが違う(22~23行目)

tkinterのバージョンの問題かもしれませんが、自分の環境では*ではなくxでないとエラーが出ました。

"700*500" -> "700x500"

ここのスペースが全角でした
スクリーンショット 2022-06-21 15.54.59.png

質問されているエラーの原因はコチラです。

appid=の後ろにAPI Keyがありません
APIが記載されていないのでエラーメッセージが返却されています。
返却されたJSONの中に['weather']は無いためエラーになっています。
以下が現在返却されているJSONです

{'cod': 401, 'message': 'Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.'}

URLは以下のようになるはずです
スクリーンショット 2022-06-21 18.01.17.png

qの後ろに=がありません

さらに、city は変数としてURLに入れたいので文字列にしてはいけません。
"city"からcityに変更します。(""をなくす)

おそらく22~23行目は改行をしようとしているので\nではなく\nです。

動画を見ていないので、もしかしたら動画でもそのようになっているのかもしれませんが、、

修正コード

import tkinter as tk
import requests #お天気APIを取得

#メインウィンドウの作成
canvas=tk.Tk()
-canvas.geometry("700*500")
+canvas.geometry("700x500")
canvas.title("Today's Weather")
a = ("Arialblack",20,"bold") #最低・最高気温を表す文字フォント
b = ("Arialblack",40,"bold") #都市入力・天気の結果を表す文字フォント

#お天気情報を得るためのget_weather関数
def getweather(canvas):
    city = textField.get()
-   api = "https://api.openweathermap.org/data/2.5/weather?q"+"city"+"&appid=" #ここにopenweatherのappidを入れています 
+   api = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=ここにAPIキーがはいります"
    json_date = requests.get(api).json()
    weather = json_date['weather'][0]['main']
    temp = int(json_date['main']['temp'] - 273.15)
    min_temp = int(json_date['main']['temp_min'] - 273.15)
    max_temp = int(json_date['main']['temp_min'] - 273.15)

    #アプリに表示する項目
-   final_info = weather + "\n"+str(temp)+""
-   final_date =  "\n"+"最低気温:"+str(min_temp)+""+"\n"+"最高気温:"+str(max_temp)+""
+   final_info = weather + "\n"+str(temp)+""
+   final_date =  "\n"+"最低気温:"+str(min_temp)+""+"\n"+"最高気温:"+str(max_temp)+""
    label1.config(text=final_info)
    label2.config(text=final_date)

#テキストボックス作成
textField = tk.Entry(canvas,justify='center',width=20, font=b)
textField.pack(pady = 30)#pady=外側の縦の隙間
textField.bind('<Return>',getweather) #APIの天気情報を返す

label1 = tk.Label(canvas,font=b)
label1.pack()
label2=tk.Label(canvas,font=a)
label2.pack()

canvas.mainloop()
1Like

Comments

  1. @ak-olive

    Questioner

    コメントありがとうございます!

    結果として④が正解でした!urlの書き間違いというお恥ずかしいミスでした(><;)

    ①ですが、どうやら私のpythonのバージョンだとxだとエラーが出てしまうようでした。
    ②と③ですが、qiitaに載せる前に一応個人情報かなと思って隠しちゃっていました。誤解を招くような書き方で恐れ入ります…。
    ⑤は最初GoogleColabで書いてた時なぜか全角しか出なかったので、やむを得ず使ってました。未解決の謎です…。
  2. 解決して良かったです!

Your answer might help someone💌