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

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

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

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

Python

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

Q&A

解決済

2回答

15825閲覧

python tkinterでボタンを押すと表示された画像が別の画像に切り替わるプログラムの作成

python_man

総合スコア13

Tkinter

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

Python

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

0グッド

1クリップ

投稿2018/10/02 16:31

tkinterを用いてGUI上でボタンと画像Aがあり、ボタンを押すと画像Aから画像Bに切り替わるようなプログラムの作成を行いたいと思っています。
tkinterを使い始めたばかりでそこまで理解ができていないのですが、ボタンがあるGUI上に画像を表示させることができないです。 
以下のコードに付け加えて教えていただけるとありがたいです。

python

1コード 2import sys 3import tkinter as tk 4 5 6root = tk.Tk() 7 8# ウインドウのタイトルを定義する 9root.title(u'タイトル') 10 11# ここでウインドウサイズを定義する 12root.geometry('1000x600') 13 14# Buttonを設置してみる 15Button1 = tk.Button(text=u'画像切り替え') 16Button1.pack() 17 18root.mainloop()

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

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

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

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

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

guest

回答2

0

ベストアンサー

ファイル名をフルパスで指定すれば動きます。
イメージ説明

参考:tkinterのlabelに貼り付けた画像を消す方法。

import sys import tkinter root = tkinter.Tk() # タイトル root.title('タイトル') # ウインドウサイズ root.geometry('250x250') # 画像の取得 img1 = tkinter.PhotoImage(file = r'C:\workspace\tmp\pic\test001.png') img2 = tkinter.PhotoImage(file = r'C:\workspace\tmp\pic\test002.png') # 画像切替 def push1(): label1.configure(image=img1) label2.configure(image='') def push2(): label1.configure(image='') label2.configure(image=img2) # ボタン Button1 = tkinter.Button(text = '画像1' , command = push1) Button1.grid(row = 1, column = 0) Button2 = tkinter.Button(text = '画像2' , command = push2) Button2.grid(row = 1, column = 1) # ラベル label1 = tkinter.Label(root,image=img1) label1.grid(row = 0, column = 0) label2 = tkinter.Label(root) label2.grid(row = 0, column = 0) # ループ root.mainloop()

投稿2018/10/02 18:35

opyon

総合スコア1009

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

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

python_man

2018/10/09 16:34

回答ありがとうございました。 とてもシンプルで分かりやすいプログラムでいした!
guest

0

試してみた。なかなか面白いですね。

python

1import tkinter as tk 2from PIL import Image, ImageTk 3 4 5class Frame(tk.Frame): 6 img1=None 7 img2=None 8 def __init__(self, master=None, title=None, width=100, height=100): 9 tk.Frame.__init__(self, master, height=height, width=width) 10 if title is not None: 11 self.master.title(title) 12 def photoImage(f): 13 img = Image.open(f) 14 if img.mode == 'P': 15 img = img.convert('RGB') 16 img = img.resize((80,80)) 17 photo = ImageTk.PhotoImage(img) 18 return photo 19 20 # 画像の取得 21 self.img1 = photoImage(r'../resource/heart_16.png') 22 self.img2 = photoImage(r'../resource/map-marker_16.png') 23 24 self.set_button() 25 self.set_label() 26 27 def set_button(self): 28 # ボタン 29 quit=tk.Button(self, text='quit', bg='gray', command=self.master.destroy) 30 quit.grid(row=0, column=0) 31 32 btn1 = tk.Button(self, text='画像1', command=lambda: self.push(self.img1)) 33 btn1.grid(row=0, column=1) 34 btn2 = tk.Button(self, text='画像2', command=lambda: self.push(self.img2)) 35 btn2.grid(row=0, column=2) 36 37 def set_label(self): 38 # ラベル 39 l1 = tk.Label(self, image=self.img1, bg="linen") 40 l1.image = self.img1 41 l1.grid(row=1, column=1) 42 self.label1 = l1 43 44 # 画像切替 45 def push(self, img): 46 self.label1.configure(image=img) 47 48if __name__ == "__main__": 49 50 frm = Frame(title='test', width=200, height=200) 51 frm.pack() 52 53 # ループ 54 frm.mainloop()

投稿2018/10/03 07:14

t_obara

総合スコア5488

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問