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

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

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

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

Q&A

解決済

1回答

324閲覧

ボタンでJPG画像を切り替えたい

kumasan2019

総合スコア12

Tkinter

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

0グッド

0クリップ

投稿2019/05/25 11:21

前提・実現したいこと

Python tkinter上でjpg画像をボタンで切り替えたい。

宜しくお願い致します。

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

下記のcodeでは切り替えできません。 

該当のソースコード

Python tkinter

1 2下記は動作しないcode 3 4# -*- coding:utf-8 -*- 5 6import tkinter as tk 7from PIL import Image, ImageTk 8import sys 9import random 10 11root = tk.Tk() 12root.title('jpg切り替え') 13root.geometry('400x400') 14 15btn1=tk.Button(root, text='Next') 16btn1.place(x=130,y=350) 17 18# ウィジェットがクリックされたときのイベントを定義 19def callback(event): 20 ran = random.randrange(1, 11) 21 photo=str(ran)+'.jpg' 22 # print(photo) 23 24 img = ImageTk.PhotoImage(Image.open(photo)) 25 panel = tk.Label(root, image = img) 26 panel.pack()#side = "bottom", fill = "both", expand = "yes") 27 panel.place(x=5,y=5) 28 29btn1.bind("<Button>", callback) 30 31 32### 試したこと 33 34このcodeでは、run(F5)するとfileも読まれてjpgも表示されます。 35もう一度runするとjpgが乱数に従って表示されます。 36 37以下はjpgの切替は出来ていますが、ボタンでの切り替えが出来ていません。 38 39import tkinter as tk 40from PIL import Image, ImageTk 41import sys 42import random 43 44root = tk.Tk() 45root.title('jpg切り替え') 46root.geometry('400x400') 47 48btn1=tk.Button(root,text='Next') #commandはまだ 49btn1.place(x=130,y=350) 50 51ran = random.randrange(1, 11) 52 53photo=str(ran)+'.jpg' 54 55img = ImageTk.PhotoImage(Image.open(photo)) 56panel = tk.Label(root, image = img) 57#panel.pack()#side = "bottom", fill = "both", expand = "yes") 58panel.place(x=5,y=5) 59 60def destroy(): 61 global root 62 root.destroy() 63 64#Command Button2 65btn2=tk.Button(root, text='End', command=destroy) 66btn2.place(x=230, y=350) 67 68root.mainloop() 69 70 71 72### 補足情報(FW/ツールのバージョンなど) 73 74・Python 3.7.3 を使用

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

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

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

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

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

guest

回答1

0

自己解決

解決してませんがclose致します。 有難うございました。

投稿2019/05/25 22:08

kumasan2019

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問