Turning display with Tkinter GUI on and off on Raspberry Pi using python3 Turning display with Tkinter GUI on and off on Raspberry Pi using python3 tkinter tkinter

Turning display with Tkinter GUI on and off on Raspberry Pi using python3


You've fallen in to the standard trap for tkinter applications. You have an infinite loop that doesn't allow the tkinter GUI to update. Never use an infinite loop with tkinter applications. Only use mainloop()

If you want to mix reading GPIOs with tkinter you need to take one of the following approaches

  1. Use the .after tkinter method to periodically schedule a function call. That function reads from the GPIO pins and updates the GUI based on the GPIO state. root.after(1000,do_function) will call a function named do_function after 1000 milli-seconds. If your do_function also contains a call to the .after method, the function will schedule it self to be called again after a specified period.

  2. Use GPIO callbacks. This is much easier if you use the gpiozero library rather than the RPi.GPIO module since you have the button.when_pressed = do_function syntax available.

With either method, the function that is called will be where you add any code to change the GUI based on the state of the GPIO pin(s).


The Tkinter GUI was getting displayed after I interrupt the program since,root.mainloop() was placed at the end of the program.

Tkinter understanding mainloop This link helped me to understand the mainloop and to make necessary changes in the python sketch.

import sysimport osimport timefrom tkinter import *import tkinter as tkfrom tkinter.font import Fontimport RPi.GPIO as GPIO#pin descriptionsensor = 11GPIO.setwarnings(False)GPIO.setmode(GPIO.BOARD)GPIO.setup(sensor,GPIO.IN)print("Initializing PIR sensor....")time.sleep(12) #to warmup the sensorprint("PIR ready")print("")#initializing tkinter parametersroot = tk.Tk()text = tk.Text(root)font1 = Font(family = 'Helvetica', size = 30, weight = 'bold')font2 = Font(family = 'Helvetica', size = 20, weight = 'bold')font3 = Font(family = 'Helvetica', size = 15, weight = 'bold')explanation = """WELCOME TO MY GUI"""colin = tk.PhotoImage(file="background.gif")background=tk.Label(root,compound = tk.CENTER, text=explanation,font=font1, image=colin).pack()def exitProgram():    print("Exit button pressed")    GPIO.cleanup()    root.quit()exitButton =tk.Button(root, text = "Exit", font = font3, command = exitProgram, height = 1, width = 4, bd =1)exitButton.place(x= 350, y =435)root.attributes("-fullscreen",True)os.system("xset dpms force off")#keep the display turned off until any motion is detectedtry:    while True:        i = GPIO.input(sensor)        if i==1:#turn on the display when motion is detected           print("Human presence detected")           os.system("xset dpms force on")           root.update()           time.sleep(30)       else:           print("no human around")           os.system("xset dpms force off")           time.sleep(5)except KeyboardInterrupt:    GPIO.cleanup()