#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0

    Opening GIF Image from Python TKinter Buttons


    I have already made a post on how to open a JPG file but I now plan to use GIF image files. Can someone show me a sample code example on how to open GIF image files whenever you click buttons on TKinter GUI in Python? Thanks.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Code:
    try:
        # Python2
        import Tkinter as tk
    except ImportError:
        # Python3
        import tkinter as tk
        
    def action():
        root.title("Button with an image")
    
    root = tk.Tk()
    root.title("Click on the image to activate button")
    
    # pick a GIF image you have in the working directory
    # or give full path
    image1 = tk.PhotoImage(file='Farm.gif')
    
    # create a button to display the image
    button = tk.Button(root, image=image1, command=action)
    button.grid(row=1, column=1)
    
    root.mainloop()
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo