pythonを用いてカレンダーの作成をしています。
https://qiita.com/eito_2/items/cc0d462c8d0ce04d7a89
を参考に作成したカレンダーに、日付ボタンをクリックして新しいウィンドウを表示するプログラムを作成したいです。新しく生成したウィンドウにはその日の天気を選択するボタンを作成したいと考えているのですが、そのボタンに画像を貼り付けることができません。
下の画像の雨、雪、曇りの文字の部分をその天気を示す画像にしたいのですが、「image "pyimage1" doesn't exist」というエラーメッセージが表示されます。画像は同じディレクトリ内においてあります。
以下にコードを載せるので何が問題なのか教えていただきたいです。
python
1from cgitb import text 2from curses.textpad import Textbox 3from datetime import datetime, date 4from email.mime import image 5from logging import root 6from dateutil.relativedelta import relativedelta 7import calendar 8import jpholiday 9import tkinter as tk 10from numpy import imag 11 12WEEKDAY_COLOR = (None, "red", *(["black"] * 5), "blue") 13A_MONTH = relativedelta(months=1) 14image_weather = [] 15 16 17def button_click(): #日付がクリックされた時にこの関数をcommandで指定して日記用のウィンドウを開きたい 18 19 window = tk.Tk() 20 window.title("diary") 21 window.geometry("500x500") 22 23 image_weather.append(tk.PhotoImage(file="/Users/koyo/Desktop/koyo/py_program/太陽.png")) 24 image_weather.append(tk.PhotoImage(file="/Users/koyo/Desktop/koyo/py_program/雨.png")) 25 image_weather.append(tk.PhotoImage(file="/Users/koyo/Desktop/koyo/py_program/雲.png")) 26 image_weather.append(tk.PhotoImage(file="/Users/koyo/Desktop/koyo/py_program/雪.png")) 27 28 label_for_wather = tk.Label(window, text="天気") 29 label_for_wather.pack() 30 31 frame_for_weather = tk.Label(window) #天気のボタンを配置するためのframe 32 frame_for_weather.pack() 33 button_sunny = tk.Button(frame_for_weather, text="雨", image=image_weather[0]) 34 button_rainy = tk.Button(frame_for_weather, text="雨", image=image_weather[1]) 35 button_cloudy = tk.Button(frame_for_weather, text="曇り", image=image_weather[2]) 36 button_snowy = tk.Button(frame_for_weather, text="雪", image=image_weather[3]) 37 button_sunny.pack(side=tk.LEFT) 38 button_rainy.pack(side=tk.LEFT) 39 button_cloudy.pack(side=tk.LEFT) 40 button_snowy.pack(side=tk.LEFT) 41 42 textbox_for_diary = tk.Text(window, font=("",20), height=500, width=500, bg="skyblue") #日記入力用のテキストボックスを作成 43 textbox_for_diary.pack() 44 45 window.mainloop() 46 47class TkCalendar(tk.Frame): 48 49 def __init__(self, master=None, *args, **kwargs): 50 super().__init__(master, *args, **kwargs) 51 self.pack() 52 today = datetime.today() 53 self.date = date(today.year, today.month, 1) 54 self.calendar = calendar.Calendar() 55 self.calendar.setfirstweekday(6) 56 57 def build(self): 58 self.build_date() 59 self.build_weekdays() 60 self.build_days() 61 62 def build_date(self, font=("", 20)): 63 frame = tk.Frame(self) 64 frame.pack(pady=5) 65 # 先月ボタン 66 prev = tk.Button(frame, text="<", font=font, command=self.prev_month) 67 prev.pack(side=tk.LEFT) 68 # 年月表示 69 self.year = tk.Label(frame, text=self.date.year, font=font) 70 self.year.pack(side=tk.LEFT) 71 slash = tk.Label(frame, text="/", font=font) 72 slash.pack(side="left") 73 self.month = tk.Label(frame, text=self.date.month, font=font) 74 self.month.pack(side=tk.LEFT) 75 # 翌月ボタン 76 next = tk.Button(frame, text=">", font=font, command=self.next_month) 77 next.pack(side=tk.LEFT) 78 79 def build_weekdays(self): 80 frame = tk.Frame(self) 81 frame.pack(pady=5) 82 for column, weekday in enumerate("日月火水木金土", 1): 83 widget = tk.Button(frame, text=weekday, fg=WEEKDAY_COLOR[column], 84 height=2, width=4, relief="flat") 85 widget.grid(column=column, row=1, padx=10, pady=5) 86 87 def build_days(self): 88 self.days = tk.Frame(self) 89 self.days.pack() 90 self.update_days() 91 92 def update_days(self): 93 for day in self.days.winfo_children(): 94 day.destroy() 95 year, month = self.date.year, self.date.month 96 weeks = self.calendar.monthdayscalendar(year, month) 97 for row, week in enumerate(weeks, 1): 98 for column, day in enumerate(week, 1): 99 if day == 0: 100 continue 101 color = WEEKDAY_COLOR[column] 102 if jpholiday.is_holiday(date(year, month, day)): 103 color = "red" 104 widget = tk.Button(self.days, text=day, fg=color, 105 height=2, width=4, relief="flat",command=button_click) #このボタンが日付を表示している。commandを指定して日にちがクリックされた時に新たなウィンドウを開く 106 widget.grid(column=column, row=row, padx=10, pady=5) 107 108 def update(self): 109 self.year["text"] = self.date.year 110 self.month["text"] = self.date.month 111 self.update_days() 112 113 def prev_month(self): 114 self.date -= A_MONTH 115 self.update() 116 117 def next_month(self): 118 self.date += A_MONTH 119 self.update() 120 121 122def main(): 123 root = tk.Tk() 124 root.title("calendar") 125 root.geometry("600x440") 126 tkcalendar = TkCalendar(root) 127 tkcalendar.build() 128 root.mainloop() 129 130if __name__ == "__main__": 131 main()
参考にしたQiitaのサンプルプログラムからの変更点はdef update_days内のwidgetボタンのcommandです。
よろしければ回答のほどよろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/03/07 07:56