Python Forum
Picture changing triggered by GPIO
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Picture changing triggered by GPIO
#3
You should use gpiozero, which has already event driven callbacks and it's own background thread.

Example-Code:
import os
from tkinter import Button, Label, Tk

from gpiozero import Button as GPIO_Button
from gpiozero.pins.mock import MockFactory
from PIL import Image
from PIL.ImageTk import PhotoImage

# There is Button from tkinter already imported
# renaming Button to GPIO_Button
# MockFactory is to test without GPIO (real Hardware)


if not os.environ.get("DISPLAY", ""):
    print("no display found. Using :0.0")
    os.environ["DISPLAY"] = ":0.0"


def set_red_image():
    """
    Callback function for gpio Button
    """
    label["image"] = red_img


def set_white_image():
    """
    Callback function for gpio Button
    """
    label["image"] = white_img


def drive_low():
    """
    This function is for testing without GPIO.
    """
    print("Setting GPIO to low state")
    gpio_pin = mock_factory.pins[pin]
    gpio_pin.drive_low()
    # call drive high after 2 seconds
    root.after(2000, drive_high)


def drive_high():
    """
    This function is for testing without GPIO.
    """
    print("Setting GPIO to high state")
    gpio_pin = mock_factory.pins[pin]
    gpio_pin.drive_high()
    # call drive_low after one second
    root.after(1000, drive_low)


if __name__ == "__main__":
    pin = 21

    # for testing without gpio
    mock_factory = MockFactory()
    # end

    button = GPIO_Button(pin, pin_factory=mock_factory)
    # button = GPIO_Button(pin) # <- with real gpio

    # the library has an own background thread,
    # which handles incomming events like positive edge or negative edge
    # https://gpiozero.readthedocs.io/en/stable/recipes.html#button
    button.when_pressed = set_red_image
    button.when_released = set_white_image
    # when_pressed calls set_red_image
    # when_released calls set_white_image

    root = Tk()
    root.geometry("800x600")

    # placeholder images
    # Hint: PhotoImage requires the master (root).
    #       Don't create instances of PhotoImage
    #       before root was created
    white_img = PhotoImage(
        Image.new(mode="RGB", size=(200, 200), color=(255, 255, 255))
    )
    red_img = PhotoImage(Image.new(mode="RGB", size=(200, 200), color=(255, 0, 0)))

    # creating the label once is enough
    # you can change the label afterwards it was created
    label = Label(root, image=white_img, borderwidth=0)
    label.grid(row=0, column=0)

    # button to close the app
    Button(root, text="Close", command=root.destroy).grid(row=1, column=0)

    root.focus_force()

    # this calls the test function to test without gpio
    # you don't need it with gpio
    # and you can remove drive_high and drive_low
    root.after(1000, drive_low)
    root.mainloop()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Picture changing triggered by GPIO - by q_nerk - Dec-13-2020, 07:18 PM
RE: Picture changing triggered by GPIO - by DeaD_EyE - Dec-14-2020, 03:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  openpyxl insert picture problem cools0607 2 1,563 May-03-2023, 06:48 AM
Last Post: cools0607
  function return boolean based on GPIO pin reading caslor 2 1,189 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 798 Jan-30-2023, 08:05 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 996 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,529 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  How would I use Watchdog to get triggered when DVD is inserted? Daring_T 12 4,753 Aug-17-2021, 01:49 PM
Last Post: Daring_T
  Halting if command if gpio triggered event triggered knoxvilles_joker 7 3,242 Jun-21-2021, 01:27 AM
Last Post: knoxvilles_joker
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 3,932 Feb-19-2021, 06:29 AM
Last Post: LouF
  GPIO high if network IP has good ping duckredbeard 3 2,346 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  How to scrolling Picture in x axis kalihotname 1 2,257 Jun-16-2020, 12:18 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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