Update image of a button depending on its previous image
<p>I am trying to program a start/pause button in Tkinter (Pyhton) but the following code doesn't work:</p>

<pre><code>def startpause():
 if startpause_button.cget('image')=='start_image':
  startpause_button.config(image=pause_image)
 else:
  startpause_button.config(image=start_image)
return

start_image=ImageTk.PhotoImage(file='start.png')
pause_image=ImageTk.PhotoImage(file='pause.png')

startpause_button=ttk.Button(frame,image=start_image,command = startpause)
</code></pre>

<p>I understand the issue is about cget (which return ('pyimage3'), ) and what it returns but I don't know what to put as a value to check ("('pyimage3')," doesn't work).</p>

<p>Do you have any idea about this issue?</p>

<p>Thank you.</p>

<p><code>startpause_button.cget('image')</code> and <code>'start_image'</code> are two different things, <code>.cget('image')</code> returns the name of the image in a list e.g. <code>('pyimage1',)</code>. This means to compare them you need to take it out of the list with <code>[0]</code> and make sure both variables are strings with <code>str()</code> because <code>'pyimage1'</code> and <code>pyimage1</code> are also two different things</p>

<pre><code>import tkinter.ttk
from tkinter import Tk, PhotoImage

def startpause():
    global start_image
    if str(startpause_button.cget('image')[0])==str(start_image):
        startpause_button.config(image=pause_image)
    else:
        startpause_button.config(image=start_image)
    #return # i dont think you need this

global start_image
start_image=PhotoImage(file='start.gif')
pause_image=PhotoImage(file='pause.gif')

startpause_button=tkinter.ttk.Button(frame,image=start_image,command = startpause)
</code></pre>

<p>This does work and I've tested it, hope this helps you! :)</p>

python image tkinter