質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Visual Studio Code

Visual Studio Codeとは、Microsoft社が開発したマルチプラットフォーム対応のテキストエディタです。Visual Studioファミリーの一員でもあります。拡張性とカスタマイズ性が高く、テキストエディタでありながら、IDEと遜色ない機能を備えることができます。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

button

HTMLで用いる<button>タグです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

5394閲覧

 pythonのtkinterでの画像表示について

skye

総合スコア1

Visual Studio Code

Visual Studio Codeとは、Microsoft社が開発したマルチプラットフォーム対応のテキストエディタです。Visual Studioファミリーの一員でもあります。拡張性とカスタマイズ性が高く、テキストエディタでありながら、IDEと遜色ない機能を備えることができます。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

button

HTMLで用いる<button>タグです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

1グッド

0クリップ

投稿2022/03/06 21:22

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です。
よろしければ回答のほどよろしくお願いします。

naisu👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

tk.PhotoImagemaster キーワードで root ウィンドウを指定して下さい。

python

1image_weather.append(tk.PhotoImage(file="/Users/koyo/Desktop/koyo/py_program/太陽.png", master=window)) 2image_weather.append(tk.PhotoImage(file="/Users/koyo/Desktop/koyo/py_program/雨.png", master=window)) 3image_weather.append(tk.PhotoImage(file="/Users/koyo/Desktop/koyo/py_program/雲.png", master=window)) 4image_weather.append(tk.PhotoImage(file="/Users/koyo/Desktop/koyo/py_program/雪.png", master=window))

投稿2022/03/07 00:24

melian

総合スコア19712

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

skye

2022/03/07 07:56

ありがとうございます。解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問