Python Forum
Picture changing triggered by GPIO
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Picture changing triggered by GPIO
#1
Hello,
I'm just started learning Python. Program will work under RPi.
General functionality is: when gpio input is grounded picture red picture should me displayed, when input is on open circuit to ground white picture should be displayed.

from Tkinter import *
from PIL import Image
from PIL import ImageTk
import os
import sys
import RPi.GPIO as GPIO


root = Tk()

#root.geometry("800x600")
#root.attributes('-type', 'dock')
#root.focus_force()

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
IP = 21
GPIO.setup(IP, GPIO.IN, pull_up_down=GPIO.PUD_UP)

root.geometry("800x600")
#root.attributes('-type', 'dock')
root.focus_force()

if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')
    os.environ.__setitem__('DISPLAY', ':0.0')
GPIO.setmode(GPIO.BCM)

global p1_rd
global p1_wh

p1_rd = ImageTk.PhotoImage(Image.open("1_rd.png"))
p1_wh = ImageTk.PhotoImage(Image.open("1_wh.png"))


im_1 = Label(image= p1_wh, borderwidth=0)
im_1.grid(row=0, column=0)

global trig1

trig1 = 1

def ChangeImage1(self):
    global trig1
    if GPIO.input(21) == 0:
        if GPIO.input(21) != trig1:
            im_1 = Label(image= p1_rd, borderwidth=0)
            im_1.grid(row=0, column=0)
            trig1 = 0
    else:
        if trig1 != 1:
            im_1 = Label(image= p1_wh, borderwidth=0)
            im_1.grid(row=0, column=0)
            trig1 = 1


ChangeImage1(None)
#root = Tkinter.Tk()
#state_det1(None)
root.mainloop()
GPIO.cleanup()
Program gives expected result only if gpio is triggered when program is starting up (once)
I tried to use while loop with extra function def. - only my program stuck.
I'm not sure that way with Tkinker is good one.

Can you help me with any direction when I should go?
Reply


Messages In This Thread
Picture changing triggered by GPIO - by q_nerk - Dec-13-2020, 07:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  openpyxl insert picture problem cools0607 2 1,399 May-03-2023, 06:48 AM
Last Post: cools0607
  function return boolean based on GPIO pin reading caslor 2 1,131 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 744 Jan-30-2023, 08:05 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 955 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,389 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,624 Aug-17-2021, 01:49 PM
Last Post: Daring_T
  Halting if command if gpio triggered event triggered knoxvilles_joker 7 3,159 Jun-21-2021, 01:27 AM
Last Post: knoxvilles_joker
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 3,861 Feb-19-2021, 06:29 AM
Last Post: LouF
  GPIO high if network IP has good ping duckredbeard 3 2,282 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  How to scrolling Picture in x axis kalihotname 1 2,208 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