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

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

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

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

Q&A

解決済

1回答

6927閲覧

【Python】atomで「_tkinter.TclError: couldn't open .png: no such file or directory」と表示された場合の対処法

Azm-yk

総合スコア10

Python

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

0グッド

0クリップ

投稿2020/01/28 18:12

編集2020/01/28 18:35

デスクトップ上に保存しているフォルダ内にある「.py」ファイルをatomで実行しようとしたところ、「_tkinter.TclError: couldn't open .png: no such file or directory」という表示が出ました。
「実行しようとしているファイル」と「開くことができないと言われているファイル」は、同じフォルダ内に配置しているのですが、何が原因なのでしょうか。
また、対処法についてもあわせてご教示ください。

利用環境は、Mac(ver. 10.13.2),python3,atom(atom-runnerインストール済)

参考としてプログラムを以下に載せます

python

1import tkinter 2import random 3 4index = 0 5timer = 0 6score = 0 7tsugi = 0 8 9cursor_x = 0 10cursor_y = 0 11mouse_x = 0 12mouse_y = 0 13mouse_c = 0 14 15def mouse_move(e): 16 global mouse_x, mouse_y 17 mouse_x = e.x 18 mouse_y = e.y 19 20def mouse_press(e): 21 global mouse_c 22 mouse_c = 1 23 24neko = [] 25check = [] 26for i in range(10): 27 neko.append([0,0,0,0,0,0,0,0]) 28 check.append([0,0,0,0,0,0,0,0]) 29 30def draw_neko(): 31 cvs.delete("NEKO") 32 for y in range(10): 33 for x in range(8): 34 if neko[y][x] > 0: 35 cvs.create_image(x*72+60, y*72+60, image=img_neko[neko[y][x]], tag="NEKO") 36 37def check_neko(): 38 for y in range(10): 39 for x in range(8): 40 check[y][x] = neko[y][x] 41 42 for y in range(1, 9): 43 for x in range(8): 44 if check[y][x] > 0: 45 if check[y-1][x] == check[y][x] and check[y+1][x] == check[y][x]: 46 neko[y-1][x] = 7 47 neko[y][x] = 7 48 neko[y+1][x] = 7 49 50 for y in range(10): 51 for x in range(1, 7): 52 if check[y][x] > 0: 53 if check[y][x-1] == check[y][x] and check[y][x+1] == check[y][x]: 54 neko[y][x-1] = 7 55 neko[y][x] = 7 56 neko[y][x+1] = 7 57 58 for y in range(1, 9): 59 for x in range(1, 7): 60 if check[y][x] > 0: 61 if check[y-1][x-1] == check[y][x] and check[y+1][x+1] == check[y][x]: 62 neko[y-1][x-1] = 7 63 neko[y][x] = 7 64 neko[y+1][x+1] = 7 65 if check[y-1][x+1] == check[y][x] and check[y+1][x-1] == check[y][x]: 66 neko[y-1][x+1] = 7 67 neko[y][x] = 7 68 neko[y+1][x-1] = 7 69 70def sweep_neko(): 71 num = 0 72 for y in range(10): 73 for x in range(8): 74 if neko[y][x] == 7: 75 neko[y][x] = 0 76 num = num + 1 77 return num 78 79def drop_neko(): 80 flg = False 81 for y in range(8, -1, -1): 82 for x in range(8): 83 if neko[y][x] != 0 and neko[y+1][x] == 0: 84 neko[y+1][x] = neko[y][x] 85 neko[y][x] = 0 86 flg = True 87 return flg 88 89def over_neko(): 90 for x in range(8): 91 if neko[0][x] > 0: 92 return True 93 return False 94 95def set_neko(): 96 for x in range(8): 97 neko[0][x] = random.randint(0, 6) 98 99def draw_txt(txt, x, y, siz, col, tg): 100 fnt = ("Times New Roman", siz, "bold") 101 cvs.create_text(x+2, y+2, text=txt, fill="black", font=fnt, tag=tg) 102 cvs.create_text(x, y, text=txt, fill=col, font=fnt, tag=tg) 103 104def game_main(): 105 global index, timer, score, tsugi 106 global cursor_x, cursor_y, mouse_c 107 if index == 0: # タイトルロゴ 108 draw_txt("ねこねこ", 312, 240, 100, "violet", "TITLE") 109 draw_txt("Click to start.", 312, 560, 50, "orange", "TITLE") 110 index = 1 111 mouse_c = 0 112 elif index == 1: # タイトル画面 スタート待ち 113 if mouse_c == 1: 114 for y in range(10): 115 for x in range(8): 116 neko[y][x] = 0 117 mouse_c = 0 118 score = 0 119 tsugi = 0 120 cursor_x = 0 121 cursor_y = 0 122 set_neko() 123 draw_neko() 124 cvs.delete("TITLE") 125 index = 2 126 elif index == 2: # 落下 127 if drop_neko() == False: 128 index = 3 129 draw_neko() 130 elif index == 3: # 揃ったか 131 check_neko() 132 draw_neko() 133 index = 4 134 elif index == 4: # 揃ったネコがあれば消す 135 sc = sweep_neko() 136 score = score + sc*10 137 if sc > 0: 138 index = 2 139 else: 140 if over_neko() == False: 141 tsugi = random.randint(1,6) 142 index = 5 143 else: 144 index = 6 145 timer = 0 146 draw_neko() 147 elif index == 5: #マウス入力を待つ 148 if 24 <= mouse_x and mouse_x < 24+72*8 and 24 <= mouse_y and mouse_y < 24+72*10: 149 cursor_x = int((mouse_x-24)/72) 150 cursor_y = int((mouse_y-24)/72) 151 if mouse_c == 1: 152 mouse_c = 0 153 set_neko() 154 neko[cursor_y][cursor_x] = tsugi 155 tsugi = 0 156 index = 2 157 cvs.delete("CURSOR") 158 cvs.create_image(cursor_x*72+60, cursor_y*72+60, image=cursor, tag="CURSOR") 159 draw_neko() 160 elif index == 6: # ゲームオーバー 161 timer = timer + 1 162 if timer == 1: 163 draw_txt("GAME OVER", 312, 348, 60, "red", "OVER") 164 if timer ==50: 165 cvs.delete("OVER") 166 index = 0 167 cvs.delete("INFO") 168 draw_txt("SCORE "+str(score), 160, 60, 32, "blue", "INFO") 169 if tsugi > 0: 170 cvs.create_image(752, 128, image=img_neko[tsugi], tag="INFO") 171 root.after(100, game_main) 172 173root = tkinter.Tk() 174root.title("落ち物バズル「ねこねこ」") 175root.resizable(False, False) 176root.bind("<Motion>", mouse_move) 177root.bind("<ButtonPress>", mouse_press) 178cvs = tkinter.Canvas(root, width=912, height=768) 179cvs.pack() 180 181bg = tkinter.PhotoImage(file="neko_bg.png") 182cursor = tkinter.PhotoImage(file="neko_cursor.png") 183img_neko = [ 184 None, 185 tkinter.PhotoImage(file="neko1.png"), 186 tkinter.PhotoImage(file="neko2.png"), 187 tkinter.PhotoImage(file="neko3.png"), 188 tkinter.PhotoImage(file="neko4.png"), 189 tkinter.PhotoImage(file="neko5.png"), 190 tkinter.PhotoImage(file="neko6.png"), 191 tkinter.PhotoImage(file="neko_niku.png") 192 ] 193 194cvs.create_image(456, 384, image=bg) 195game_main() 196root.mainloop() 197

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

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

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

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

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

Azm-yk

2020/01/28 23:30 編集

ちなみにIDLEでは実行できました。誤字では無いかと思います。
guest

回答1

0

ベストアンサー

とにもかくにも、ファイルが存在しない、といってるんですから、
本当に存在してるのか、ファイル名があってるのかよくチェックするしかないです

よくあるのは全角文字が混じってるとか見えない不当な文字が入ってるとか。。

投稿2020/01/28 21:37

y_waiwai

総合スコア87774

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

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

Azm-yk

2020/01/28 23:33

回答ありがとうございます。おそらくファイルを配置場所が問題かと思いますので、引き続き確認してみます。
y_waiwai

2020/01/28 23:39

カレントディレクトリがどこか、コードで出力するようにして確認してみては。
Azm-yk

2020/01/30 19:59

解決しました。atomでプロジェクトを開く際に、実行するファイルを直接選択していましたが、一つ上のフォルダをプロジェクトとして選択し、そのプロジェクトとして選択されたフォルダの中から当該ファイルを開いて実行すると問題なく動作しました。 親切にありがとうございました。今後ともよろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問