質問するログイン新規登録

質問編集履歴

1

誤字

2024/07/28 09:34

投稿

hokuto456
hokuto456

スコア1

title CHANGED
File without changes
body CHANGED
@@ -21,42 +21,8 @@
21
21
  #tkinterというパッケージに含まれるfiledialogをインポート
22
22
  from PIL import Image,ImageTK
23
23
 
24
- beforeimg=None
25
24
 
26
- def ButtonClick():
27
25
 
28
- global beforeimg,pimg
29
- #グローバル変数でないと処理終了後に画像が表示されなくなる
30
-
31
-
32
- #askopenfilenameはファイル名選択画面を表示させるための関数
33
- filename=filedialog.askopenfilename(filetypes=[("画像ファイル","*jpg;*png;")])
34
-
35
- if filename:
36
- if beforeimg is not None:
37
- canvas.delete(beforeimg)
38
-
39
- #image形式でfilenameのファイルをimgに読み込み
40
- img=Image.open(filename)
41
- #表示するためにPhotoImageに変換
42
- pimg=ImageTK.PhotoImage(image=img)
43
- #キャンパスに配置
44
- beforeimg=canvas.create_img(0,0, anchor=tk.NW,image=pimg)
45
-
46
-
47
-
48
- root=tk.Tk()
49
- root.geometry("1024x1024")
50
-
51
- canvas=tk.Canvas(root,width=1024,height=768,bg="white")
52
- canvas.place(x=0,y=0)
53
-
54
- openbutton=tk.Button(root,text="開く",font=("Helvetica",14),command=ButtonClick)
55
- openbutton.place(x=30,y=900)
56
-
57
- root.mainloop()
58
- ```
59
-
60
26
  ### 試したこと・調べたこと
61
27
  - [x] teratailやGoogle等で検索した
62
28
  - [ ] ソースコードを自分なりに変更した