0

I am trying to create a program then when started, shows a gif and a button that says: "Turn off system". When clicked, I want the gif to change to another gif and the button to be replaced with a button that says "Turn on system".

I made exactly this but when I click the button, the first GIF remains on the screen. You can however see the second GIF popping in and out when clicking rapidly.

EDIT* Due to a formatting issue I had to redefine my code, the def main(): you see in my code should not exist.

Here is my code:

    import tkinter as tk
    from PIL import Image, ImageTk
    from tkinter import *
    from tkinter import ttk

    root = tk.Tk()
    root.title('Camera System')

def update(ind):

    frame = stgImg[ind]
    ind += 1
    if ind == frameCnt:
        ind = 0
    label.configure(image=frame)
    root.after(100, update, ind)

def SystemOn():
    
    stgImg = PhotoImage(file=(filename1.gif))
    label.configure(image=stgImg)
    label.image = stgImg

    global b3
    b3=tk.Button(root,text="Turn Off System",command=lambda:SystemOff())
    b3.pack()
    b1.pack_forget()
    b2.pack_forget()

def SystemOff():

    stgImg = PhotoImage(file=(filename2.gif))
    label.configure(image=stgImg)
    label.image = stgImg

    global b2
    b2=tk.Button(root,text="Turn On System",command=lambda:SystemOn())
    b2.pack()
    b1.pack_forget()
    b3.pack_forget()

def main():

    frameCnt = 12
    root.geometry('1010x740+200+200')
    stgImg = [PhotoImage(file=(filename1.gif),
    format = 'gif -index %i' %(i)) for i in range(frameCnt)]

    label=ttk.Label(root)
    label.pack()
    root.after(0, update, 0)

    global b1
    b1=tk.Button(root,text="Turn Off System",command=lambda:SystemOff())
    b1.pack()

    root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34

1 Answers1

0

Here is a small program that achieves the affects described.

I've set up filedialog to load the two images and defined a button with message.

Two functions work together to replace the image and text.

import tkinter as tk
from tkinter import filedialog as fido

master = tk.Tk()

imageon = fido.askopenfilename(title ="Pick a Picture")
on = tk.PhotoImage(file = imageon)

imageoff = fido.askopenfilename(title ="Pick a Picture")
off = tk.PhotoImage(file = imageoff)

def turnOn():
    button.config(image = on, text = "Turn on system", command = turnOff)

def turnOff():
    button.config(image = off, text = "Turn off system", command = turnOn)

button = tk.Button(master, image = off, compound = "top")
button.grid(sticky = tk.NSEW)
turnOff()

master.mainloop()
Derek
  • 1,916
  • 2
  • 5
  • 15
  • Thanks, would this program also work with 2 moving gifs? I see that I can load in a GIF but its not going to move cause it acts like an image. – RonWonkers Oct 14 '21 at 12:54
  • You can play gif animations in tkinter. Here's a link. https://stackoverflow.com/questions/43770847/play-an-animated-gif-in-python-with-tkinter – Derek Oct 14 '21 at 12:57
  • Thanks you for your info :) – RonWonkers Oct 14 '21 at 18:18