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

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

新規登録して質問してみよう
ただいま回答率
85.48%
文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

Python

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

Q&A

1回答

159閲覧

クイズプログラムでのエラー

imbat_horta

総合スコア0

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

Python

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

0グッド

0クリップ

投稿2020/07/07 07:20

前提・実現したいこと

自分で作ろうとしているクイズ形式のプログラムなのですが
実行すると以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "kime4.py", line 11, in <module> img = ImageTk.PhotoImage(file='hasira.png') File "C:\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 89, in __init__ image = _get_image_from_kw(kw) return Image.open(source) File "C:\Anaconda3\lib\site-packages\PIL\Image.py", line 2809, in open fp = builtins.open(filename, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'hasira.png' Exception ignored in: <function PhotoImage.__del__ at 0x00000204BAC8D438> Traceback (most recent call last): File "C:\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 118, in __del__ name = self.__photo.name AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

該当のソースコード

import tkinter as tk from PIL import Image, ImageTk from tkinter import PhotoImage def raise_frame(frame): frame.tkraise() root = tk.Tk() root.title("テスト") img = ImageTk.PhotoImage(file='hasira.png') f1 = tk.Frame(root,width=680, height=480, bg="white") label= tk.Label(f1, image=img).pack() label1=tk.Label(f1, text="", font=("",30)).pack() button1=tk.Button(f1, text="スタート", font=("",30), command=lambda:raise_frame(f2)).pack() f2 = tk.Frame(root,width=680, height=480, bg="black") label= tk.Label(f2, image=img).pack() Label1 = tk.Label(f2, text="女性ですか?男性ですか? (1:男性、2:女性)", font=("",30), bg="white") Label1.pack() #ボタン配置 button = tk.Button(f2, text="男性", font=("", 24),command=lambda:raise_frame(f3)).pack(padx=200, side="left") button1 = tk.Button(f2, text="女性", font=("", 24),command=lambda:raise_frame(f3)).pack(padx=200, side= 'right') f3 = tk.Frame(root,width=680, height=480, bg="skyblue") label2= tk.Label(f3, image=img).pack() Label3 = tk.Label(f3, text="傷がありますか?", font=("",30), bg="white") Label3.pack() #ボタン配置 button = tk.Button(f3, text="ある", font=("", 24),command=lambda:raise_frame(f4)).pack(padx=200, side="left") button1 = tk.Button(f3, text="ない", font=("", 24),command=lambda:raise_frame(f4)).pack(padx=200, side= 'right') gyoumei = ImageTk.PhotoImage(file = "gyoumei.png") f4 = tk.Frame(root,width=680, height=480, bg="red") label5 = tk.Label(f4, text="", font=("",30)) label= tk.Label(f4, image=gyoumei).pack() label5.pack() button3 = tk.Button(f4, text='戻る', font=("",30), command=lambda:raise_frame(f1)).pack() #f3 = tk.Frame(root,width=680, height=480, bg="red") #button3 = tk.Button(f3, text='戻る', font=("",30), command=lambda:raise_frame(f1)).pack(fill = "both", padx=100, pady=200, expand=True) for frame in (f1, f2, f3, f4): frame.grid(row=0, column=0, sticky='news') raise_frame(f1) root.mainloop()

試したこと

Googleなどで調べてもいまいちわからなかったのでなぜこうなってしまうのかこれを付け加えるとできるなどアドバイスがあればお願いします!

補足情報(FW/ツールのバージョンなど)

プログラムはpythonで使っているソフトはビジュアルスタジオコードです。

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

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

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

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

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

guest

回答1

0

原因はFileNotFoundError: [Errno 2] No such file or directory: 'hasira.png'です。
カレントディレクトリに画像ファイルを配置してください。一般的にはkime4.pyと同じ場所でよいです。

以下のようなコードでファイルの存在確認ができます。

Python

1import os 2 3img_path = 'hasira.png' 4print( img_path, os.path.exists(img_path)) 5img_path = os.path.join( os.getcwd(), 'hasira.png') # getcwdはカレントディレクトリパスを返す 6print(img_path, os.path.exists( img_path)) 7

投稿2020/07/07 07:29

編集2020/07/07 08:13
can110

総合スコア38266

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

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

imbat_horta

2020/07/07 07:45

回答ありがとうございます。 申し訳ないのですが kime4.pyと同じところにhasira.pngのファイルを入れて実行してみましたがエラー文が変わりませんでした。 Traceback (most recent call last): File "kime4.py", line 11, in <module> img = ImageTk.PhotoImage(file='hasira.png') File "C:\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 89, in __init__ image = _get_image_from_kw(kw) File "C:\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 58, in _get_image_from_kw return Image.open(source) File "C:\Anaconda3\lib\site-packages\PIL\Image.py", line 2809, in open fp = builtins.open(filename, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'hasira.png' Exception ignored in: <function PhotoImage.__del__ at 0x00000201FB4FD438> Traceback (most recent call last): File "C:\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 118, in __del__ name = self.__photo.name AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo' ソースコードの方では label= tk.Label(f1, image=img).pack() label1=tk.Label(f1, text="", font=("",30)).pack() button1=tk.Button(f1, text="スタート", font=("",30), command=lambda:raise_frame(f2)).pack() labelやbutton1などに赤い波線が引かれていました この赤い波線はエラー文と関係とかありますか?
can110

2020/07/07 08:16

回答に追記したコードで、ファイルが存在するか確認ください。 VSCodeは使っていないので推測ですが、VSCode上からコードを実行すると、 実行.pyファイルのある場所とカレントディレクトリが異なる場合がありえます。 コマンドプロンプト上から「python kime4.py」と実行してみてください。 とりあえず赤い波線はとりあえず関係ありません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問