Python Forum
Simple Button click on image file to create action?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Button click on image file to create action?
#4
Here's one with built in tkinter GUI package (I wrote using python 3.7.2)
Get the image here: https://developers.google.com/speed/webp...y/4.sm.jpg
convert to 4.sm.png using gimp export as 4.sm.png (create images directory below script, and save there)

you will need to install pillow (which contains PIL), use:
pip install pillow
# Note requires python 3, written with 3.7.2
# download image here: https://developers.google.com/speed/webp/gallery/4.sm.jpg
# convert to png with gimp
# create images directory in script path
import tkinter as tk
import os
from pathlib import Path
import PIL

class ImageClick:
    def __init__(self, parent=None):
        # Make sure path is known (assures path set to this script directory)
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        # set image path
        imagepath = Path('./images')
        photo = imagepath / '4.sm.png'
        if not parent:
            self.parent = tk.Tk()
        else:
            self.parent=parent
        photo=tk.PhotoImage(file=photo)
        btn = tk.Button(self.parent)
        btn.config(image=photo, width="320", height="241", activebackground="black", bg="black", bd=0, command=self.clicked)
        btn.pack(fill=tk.BOTH)
        parent.mainloop()

    def clicked(self):
        print('You clicked me')

if __name__ == '__main__':
    root = tk.Tk()
    ImageClick(root)
    root.mainloop()
Looks like:
   

when clicked (5 times):
Output:
You clicked me You clicked me You clicked me You clicked me You clicked me
Reply


Messages In This Thread
RE: Simple Button click on image file to create action? - by Larz60+ - Mar-27-2019, 02:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 874 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Figure Gets Larger Every time I click a show button joshuagreineder 2 1,335 Aug-11-2022, 06:25 AM
Last Post: chinky
  simple tkinter question function call not opening image gr3yali3n 5 3,463 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Why does the image for the button not appear? finndude 4 2,117 Oct-21-2021, 06:41 PM
Last Post: deanhystad
  looking for scripts that do simple image display Skaperen 10 4,290 Sep-13-2021, 05:35 PM
Last Post: FullOfHelp
  [Tkinter] image inside button rwahdan 4 7,121 Jul-12-2021, 08:49 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,660 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  [Tkinter] Modify Class on Button Click KDog 4 3,984 May-11-2021, 08:43 PM
Last Post: KDog
  How to create a simple GUI GRS26 7 3,660 Mar-27-2021, 02:38 PM
Last Post: FernandoCabral
  tkinter button image Nick_tkinter 4 4,084 Mar-04-2021, 11:33 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020