label=tk.Label
=>
label=tk.Label()
fpath=filelog.askopenfilename
=>
fpath=filelog.askopenfilename()
前提・実現したいこと
Pythonで写真を表示するアプリを作ろうとしたところ次のエラーが発生しました。どうすれば正しく動作しますか?
発生している問題・エラーメッセージ
Traceback (most recent call last): File "/home/fuyofuyopiroq/photo/index.py", line 22, in <module> label.pack() TypeError: pack_configure() missing 1 required positional argument: 'self'
該当のソースコード
Python
1import tkinter as tk 2import tkinter.filedialog as filelog 3import PIL.Image 4import PIL.ImageTk 5root=tk.Tk() 6root.geometry("500x500") 7root.configure(bg="black") 8def openphoto(path): 9 newimage=PIL.Image.open(path).resize((350,350)) 10 imagedata=PIL.ImageTk.PhotoImage(newimage) 11 label.configure(image=imagedata) 12 label.image=imagedata 13def openfile(): 14 fpath=filelog.askopenfilename 15 if fpath: 16 openphoto(fpath) 17 18label=tk.Label 19button=tk.Button(text="Open file") 20button["command"]=openfile 21button.pack() 22label.pack() 23tk.mainloop()
試したこと
20行目を
Python
1button["command"]=openfile()
に変えると、次のエラーが表示されました。
Traceback (most recent call last): File "/home/xxx/.local/lib/python3.9/site-packages/PIL/Image.py", line 3231, in open fp.seek(0) AttributeError: 'function' object has no attribute 'seek' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/xxx/photo/index.py", line 20, in <module> button["command"]=openfile() File "/home/xxx/photo/index.py", line 16, in openfile openphoto(fpath) File "/home/xxx/photo/index.py", line 9, in openphoto newimage=PIL.Image.open(path).resize((350,350))#open file and resize file(350x350) File "/home/xxx/.local/lib/python3.9/site-packages/PIL/Image.py", line 3233, in open fp = io.BytesIO(fp.read()) AttributeError: 'function' object has no attribute 'read'
補足情報(FW/ツールのバージョンなど)
Python3.9
Debian11.5(Chromebook Crostini)
回答1件
あなたの回答
tips
プレビュー