Hardware

Raspberry Pi: Gui & GPIO

GUI & GPIO

This Section will trait some examples about the interaction of the Raspberry Pi board from the Python GUIs and its electronic surrendering environment :

#!/usr/bin/env python3
#gui2.py

from tkinter import *
from tkinter import font
import tkinter as tk
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.OUT)
GPIO.output(40, GPIO.LOW)

win = tk.Tk()

myFont = font.Font(family = 'Helvetica', size = 36, weight = 'bold')
font.families()

def ledON():
    print("LED button pressed")
        if GPIO.input(40) :
            GPIO.output(40,GPIO.LOW)
            ledButton["text"] = "LED ON"
        else:
            GPIO.output(40,GPIO.HIGH)
            ledButton["text"] = "LED OFF"

def exitProgram():
      print("Exit Button pressed")
      GPIO.cleanup()
      win.quit()
win.title("First GUI")
win.geometry('800x480')

exitButton  = Button(win, text = "Exit", font = myFont, command = exitProgram, height =2 , width = 6)
exitButton.pack(side = BOTTOM)

ledButton = Button(win, text = "LED ON", font = myFont, command = ledON, height = 2, width =8 )
ledButton.pack()

mainloop()

Result :

chmod +x gui2.py

in the execution, every time we press the button, it toggles form “LED OFF” and “LED ON”.

Code Source :

Leave a Reply

Your email address will not be published. Required fields are marked *