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

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

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

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

Python

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

Q&A

1回答

1224閲覧

ダイヤログで選択した画像を表示する

chara2100

総合スコア1

Tkinter

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

Python

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

0グッド

1クリップ

投稿2021/01/15 03:02

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
tkinterで選択した画像ファイルを表示したいです
■■な機能を実装中に以下のエラーメッセージが発生しました。

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

エラーメッセージ

該当のソースコード

class TkinterClass: global file_name def __init__(self): global root root = tk.Tk() root.geometry("1000x700") #一つ目の商品 #seleniumドライバーのパス選択 button = tk.Button(root, text='seleniumドライバーを開く', font=('', 12), width=20, height=1, bg='#999999', activebackground="#aaaaaa") button.bind('<ButtonPress>', self.file_dialog) button.place(x=200, y=5) self.file_name = tk.StringVar() self.file_name.set('未選択です') label = tk.Label(textvariable=self.file_name, font=('', 12)) label.place(x=250, y=40) #プロファイルパス選択 button = tk.Button(root, text='プロファイルパスを開く', font=('', 12), width=20, height=1, bg='#999999', activebackground="#aaaaaa") button.bind('<ButtonPress>', self.profile_dialog) button.place(x=600, y=5) self.profile_name = tk.StringVar() self.profile_name.set('未選択です') label = tk.Label(textvariable=self.profile_name, font=('', 12)) label.place(x=650, y=40) #一枚目の画像パス button = tk.Button(root, text='一枚目の画像を開く', font=('', 12), width=20, height=1, bg='#999999', activebackground="#aaaaaa") button.bind('<ButtonPress>', self.img_file_dialog) button.place(x=10, y=75) self.img_file_name = tk.StringVar() self.img_file_name.set('未選択です') label = tk.Label(textvariable=self.img_file_name, font=('', 12)) label.place(x=50, y=110) canvas = tk.Canvas( root, # 親要素をメインウィンドウに設定 width=500, # 幅を設定 height=500 # 高さを設定 #relief=tk.RIDGE # 枠線を表示 # 枠線の幅を設定 ) canvas.place(x=0, y=0) # メインウィンドウ上に配置 #PILでjpgを使用 img_1 = Image.open(open(img1)) img_1.thumbnail((500, 500), Image.ANTIALIAS) img_1 = ImageTk.PhotoImage(img_1) # 表示するイメージを用意 canvas.create_image( # キャンバス上にイメージを配置 0, # x座標 0, # y座標 image=img_1, # 配置するイメージオブジェクトを指定 tag="illust", # タグで引数を追加する。 anchor=tk.NW # 配置の起点となる位置を左上隅に指定 ) #二枚目の画像パス button = tk.Button(root, text='二枚目の画像を開く', font=('', 12), width=20, height=1, bg='#999999', activebackground="#aaaaaa") button.bind('<ButtonPress>', self.img2_file_dialog) button.place(x=280, y=75) self.img2_file_name = tk.StringVar() self.img2_file_name.set('未選択です') label = tk.Label(textvariable=self.img2_file_name, font=('', 12)) label.place(x=320, y=110) #三枚目の画像パス button = tk.Button(root, text='三枚目の画像を開く', font=('', 12), width=20, height=1, bg='#999999', activebackground="#aaaaaa") button.bind('<ButtonPress>', self.img3_file_dialog) button.place(x=550, y=75) self.img3_file_name = tk.StringVar() self.img3_file_name.set('未選択です') label = tk.Label(textvariable=self.img3_file_name, font=('', 12)) label.place(x=600, y=110) #三枚目の画像パス button = tk.Button(root, text='四枚目の画像を開く', font=('', 12), width=20, height=1, bg='#999999', activebackground="#aaaaaa") button.bind('<ButtonPress>', self.img4_file_dialog) button.place(x=800, y=75) self.img4_file_name = tk.StringVar() self.img4_file_name.set('未選択です') label = tk.Label(textvariable=self.img4_file_name, font=('', 12)) label.place(x=850, y=110) #タイトル文 Static1 = tk.Label(text=u'▼ タイトル文 ▼') Static1.place(x=10, y=300) Entry1 = tk.Entry(width=50) Entry1.insert(tk.END, u'') Entry1.place(x=15, y=325) # タイトルの値を取得 def button_entry(event): global Entry1_value global Entry1_value1 Entry1_value = Entry1.get() Entry1_value1 = Entry1_value print(Entry1_value1) #取得ボタン Button1 = tk.Button(text=u'ok', width=8) Button1.bind("<Button-1>", button_entry) # ボタンが押されたときに実行される関数をバインド Button1.place(x=320, y=325) #商品説明文 Static2 = tk.Label(text=u'▼ 商品説明文 ▼') Static2.place(x=10, y=350) Entry2 = tk.Entry(width=50) Entry2.insert(tk.END, u"") Entry2.place(x=15, y=375) # 説明文の値を取得 def button_entry2(event): global Entry2_value global Entry2_value2 Entry2_value = Entry2.get() Entry2_value2 = Entry2_value print(Entry2_value2) #取得ボタン Button2 = tk.Button(text=u'ok', width=8) Button2.bind("<Button-1>", button_entry2) # ボタンが押されたときに実行される関数をバインド Button2.place(x=320, y=375) #カテゴリーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー def category1(evebt): global category_1 global category_2 global category_3 category_1 = cb1.get() category_2 = cb2.get() category_3 = cb3.get() print(category_1) print(category_2) print(category_3) cb1 = ttk.Combobox(root, state='readonly') Static5 = tk.Label(text=u'▼ カテゴリー ▼') Static5.place(x=10, y=405) def file_dialog(self, event): global file_name global browser_main fTyp = [("", "*")] iDir = os.path.abspath(os.path.dirname(__file__)) file_name = tk.filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir) if len(file_name) == 0: self.file_name.set('選択をキャンセルしました') else: self.file_name.set("'" + file_name + "'") browser_main = file_name print(browser_main) def profile_dialog(self, event): global profile_name global profile_main fTyp = [("", "*")] iDir = "C:\Users" profile_name = tk.filedialog.askdirectory(initialdir=iDir) if len(profile_name) == 0: self.profile_name.set('選択をキャンセルしました') else: self.profile_name.set(profile_name) profile_main = profile_name print(profile_main) def img_file_dialog(self, event): global img_file_name fTyp = [("", "*")] iDir = os.path.abspath(os.path.dirname(__file__)) img_file_name = tk.filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir) if len(img_file_name) == 0: self.img_file_name.set('選択をキャンセルしました') else: self.img_file_name.set(" ") img1 = img_file_name def img2_file_dialog(self, event): global img2_file_name global img2 fTyp = [("", "*")] iDir = os.path.abspath(os.path.dirname(__file__)) img2_file_name = tk.filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir) if len(img2_file_name) == 0: self.img2_file_name.set('選択をキャンセルしました') else: self.img2_file_name.set(" ") img2 = img2_file_name def img3_file_dialog(self, event): global img3_file_name global img3 fTyp = [("", "*")] iDir = os.path.abspath(os.path.dirname(__file__)) img3_file_name = tk.filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir) if len(img3_file_name) == 0: self.img3_file_name.set('選択をキャンセルしました') else: self.img3_file_name.set(" ") img3 = img3_file_name def img4_file_dialog(self, event): global img4_file_name global img4 fTyp = [("", "*")] iDir = os.path.abspath(os.path.dirname(__file__)) img4_file_name = tk.filedialog.askopenfilename(filetypes=fTyp, initialdir=iDir) if len(img4_file_name) == 0: self.img4_file_name.set('選択をキャンセルしました') else: self.img4_file_name.set(" ") img4 = img4_file_name if __name__ == '__main__': TkinterClass()

試したこと

ここに問題に対して試したことを記載してください。

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

Marusoftware

2021/01/21 15:24

どんな問題が発生しているのか書いていただけると幸いです。 どうやら、PILを使って、画像を読み込むところまではできているようなので...
guest

回答1

0

えっと、なんとなくですが、pil.openで開いたファイルのオブジェクトはどこかに保存しておかないといけなかった気がします。
pil.openで開いた後、その上にimagetk.photoimageを上書きしてしまっているので...
変数を分けるといいかもしれません。

投稿2021/01/21 15:26

Marusoftware

総合スコア189

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問