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

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

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

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

Python

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

Q&A

3回答

1641閲覧

python コードのエラーについて

BsaiuhuaNkt

総合スコア2

Tkinter

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

Python

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

0グッド

0クリップ

投稿2021/05/23 13:52

編集2022/01/12 10:55

コードと同じファイルに画像があるのに画像のようなエラーが表記されます。
原因は何でしょうか?
ちなみに下がコードです。

python

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

エラーは画像の通りです。
イメージ説明

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

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

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

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

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

y_waiwai

2021/05/23 14:00

このままではコードが読めないので、質問を編集し、<code>ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
BsaiuhuaNkt

2021/05/23 14:07

すみません。 コーデの中に貼りなおしました。
guest

回答3

0

カレントディレクトリーの概念を理解していない場合は、ファイル名は全てフルパスで書きましょう。

投稿2021/05/23 14:44

otn

総合スコア84423

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

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

0

pythonはカレントディレクトリ(起動したときのディレクトリ)を見ます。
ソースコードの置いてあるディレクトリではありません。

修正するには以下のように行います。

まず以下を実行してください。

python

1import os 2os.getcwd()

これで表示されるディレクトリにneko_bg.pngを置いてください。
そのあとでコードを再度実行してください。

投稿2021/05/23 14:14

ppaul

総合スコア24666

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

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

BsaiuhuaNkt

2021/05/24 04:07

ご指摘の通り実行してみると'C:Users\tarak'と表示されたので????>windows(C:)>ユーザー>tarakのファイルに移動させましたがまた同じエラーが表示されます。これはどこが違っているのでしょうか?
ppaul

2021/05/24 08:36

エクスプローラーで、移動させたあとのneko_bg.pngを右クリックしてプロパティをクリックしてください。そして全般タブをクリックして、場所をみてください。 そこに書かれているパスは、C:Users\tarak\neko_bg.png になっていますか?
BsaiuhuaNkt

2021/05/24 11:47

C:¥User¥tarakとなっています。 ちなみにソースコードをこのパスと同じ場所と元の場所で両方試しましたが、どちらもダメでした。
BsaiuhuaNkt

2021/05/25 01:47

"C:\Users\tarak\neko_bg.png"と表示されます。
guest

0

neko_bg.pngが見つからないと言ってます

投稿2021/05/23 13:54

jamjam3

総合スコア165

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

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

BsaiuhuaNkt

2021/05/23 13:58

このコードと同じファイルに画像があるのにこのようなエラーが表示されます 原因はいったい何でしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問