こんにちは、みやびのです。
今回はPythonのtkinterライブラリの使い方について説明します。
具体的には、
・Python tkinter.canvasでGUI表示する方法
・Python tkinterライブラリで天気情報を表示する方法
の2つについて紹介します。
Python tkinter.canvasでGUI表示する方法
tkinterライブラリの基本的な使い方について紹介します。
tkinterライブラリの詳しい使い方については公式ドキュメントをお読みください。
キャンバスを表示する
キャンバスの表示は以下の通りです。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import tkinter # キャンバスの表示処理 root = tkinter.Tk() # タイトルの設定 root.title("お天気GUI") root.resizable(False, False) # キャンバスのサイズ設定 canvas = tkinter.Canvas(root, width=600, height=600) # キャンバスの表示 canvas.pack() root.mainloop() |
title()はタイトルを表示するメソッドです。
tkinter.Canvas()メソッドでキャンバスのサイズを指定します。
canvas.pack()はキャンバスを表示する記述です。
文字の表示の仕方
キャンバスのコードに以下を追記します。ただし、「root.mainloop()」より前に記述する必要があります。
1 2 3 4 5 |
# 「root.mainloop()」より前に記述 # 文字を表示する label = tkinter.Label(root, text="文字を表示する", font=("Times New Roman", 35), bg="white") label.place(x=30, y=150) root.mainloop() |
ボタンの表示の仕方
ボタンの表示は以下の通りです。
こちらも「root.mainloop()」より前に記述する必要があります。
1 2 3 4 |
# 「root.mainloop()」より前に記述 # ボタンを表示する button = tkinter.Button(root, text="今日の天気", font=("Times New Roman", 36), command=today_weather, fg="skyblue") button.place(x=200, y=300) |
画像の表示の仕方
画像を表示する記述は以下の通りです。
他の項目と同じく「root.mainloop()」より前に記述します。
1 2 3 |
# 「root.mainloop()」より前に記述 img = tkinter.PhotoImage(file="img/sunny.png"), canvas.create_image(280, 100, image=img) |
太陽の画像を追加する例です。
Python tkinterライブラリで天気情報を表示する方法
天気を表示する実装例です。
今回はライブドアの天気情報を使用しています。
天気情報については「Slackbotで今日の天気を確認する〜Python・Node.jsでのBot開発〜」もお読みだくさい。
文字で天気を表示する方法
まずは文字で天気を表示する方法について説明します。
コードは以下の通りです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import tkinter import requests import json # 今日の天気を表示 def today_weather(): update_weather(0) # 明日の天気を表示 def yesterday_weather(): # ラベルにメッセージを設定 update_weather(1) # 今日の天気を表示 def update_weather(date_num): # ラベルにメッセージを設定 t = display_weather(date_num) label["text"] = t['dateLabel'] + 'の天気は「' + t['telop'] + '」です' label.update() # 天気情報を取得する def display_weather(date_num): # 天気のURL設定 city_numberには都市の番号を指定 url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=%s" % 130010 # URLを取得 response = requests.get(url) # URL取得結果のチェック response.raise_for_status() # 天気データを読み込む return json.loads(response.text)['forecasts'][date_num] # キャンバスの表示 root = tkinter.Tk() # タイトルの設定 root.title("お天気GUI") root.resizable(False, False) # キャンバスのサイズ設定 canvas = tkinter.Canvas(root, width=600, height=600) # キャンバスの表示 canvas.pack() # 今日の天気を文字で表示する label = tkinter.Label(root, text="ボタンをクリックしてください", font=("Times New Roman", 35), bg="white") label.place(x=30, y=150) # ボタンを表示する button = tkinter.Button(root, text="今日の天気", font=("Times New Roman", 36), command=today_weather, fg="skyblue") button2 = tkinter.Button(root, text="明日の天気", font=("Times New Roman", 36), command=yesterday_weather, fg="skyblue") button.place(x=200, y=300) button2.place(x=200, y=350) root.mainloop() |
上記例では「今日の天気」と「明日の天気」を表示できます。
画像を表示する方法
天気画像を表示する方法について説明します。
コードは以下の通りです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import tkinter import requests import json # 今日の天気を表示 def today_weather(): update_weather(0) # 明日の天気を表示 def yesterday_weather(): # ラベルにメッセージを設定 update_weather(1) # 今日の天気を表示 def update_weather(date_num): # ラベルにメッセージを設定 t = display_weather(date_num) if t['telop'] == '晴れ': weather_num = 0 elif t['telop'] == '晴時々曇': weather_num = 1 elif t['telop'] == '曇時々晴': weather_num = 2 elif t['telop'] == '雨': weather_num = 3 else: weather_num = 4 # 古い画像を削除 canvas.delete('all') # 画像を表示 canvas.create_image(280, 100, image=img_weather[weather_num]) label["text"] = t['dateLabel'] + 'の天気は「' + t['telop'] + '」です' label.update() # 天気情報を取得する def display_weather(date_num): # 天気のURL設定 city_numberには都市の番号を指定 url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=%s" % 130010 # URLを取得 response = requests.get(url) # URL取得結果のチェック response.raise_for_status() # 天気データを読み込む return json.loads(response.text)['forecasts'][date_num] # キャンバスの表示 root = tkinter.Tk() # タイトルの設定 root.title("お天気GUI") root.resizable(False, False) # キャンバスのサイズ設定 canvas = tkinter.Canvas(root, width=600, height=600) # キャンバスの表示 canvas.pack() # 文字を表示する label = tkinter.Label(root, text="ボタンをクリックしてください", font=("Times New Roman", 35), bg="white") label.place(x=30, y=150) # ボタンを表示する button = tkinter.Button(root, text="今日の天気", font=("Times New Roman", 36), command=today_weather, fg="skyblue") button2 = tkinter.Button(root, text="明日の天気", font=("Times New Roman", 36), command=yesterday_weather, fg="skyblue") button.place(x=200, y=300) button2.place(x=200, y=350) img_weather = [ tkinter.PhotoImage(file="img/sunny.png"), tkinter.PhotoImage(file="img/sunny_sometimes_cloudy.png"), tkinter.PhotoImage(file="img/cloudy_sometimes_sunny.png"), tkinter.PhotoImage(file="img/rainy.png"), tkinter.PhotoImage(file="img/cloudy.png") ] root.mainloop() |
以上、tkinterライブラリで天気をGUI表示する方法でした。
関連記事>>Slackbotで今日の天気を確認する〜Python・Node.jsでのBot開発〜
おすすめ記事>>Pythonistaの使い方まとめ
PythonまとめTOP>>Pythonプログラミングの始め方まとめ