pythontkinterraspberry-pitkinter-canvaskiosk

Tkinter/Canvas-based kiosk-like program for Raspberry Pi


I would like run a Python program on a Raspberry Pi (or, for that matter, any Unix/Linux-based computer) that effectively turns the entire screen into a canvas and allows me to draw text and graphics objects on it in real time. I ideally want this to also automatically conceal everything else on the desktop and eliminate the window frame and task bar, similar to playing a video in full screen mode (with ESC to exit).

My research so far suggests that Tkinter/Canvas would be the simplest solution. But while I have found examples online that accomplish part of what I describe above, I haven't been able to put the pieces together in a form that does it all. It doesn't help that I have no prior experience with Tkinter.

If someone can point me to a minimal working example of the setup described, I would greatly appreciate it.


Solution

  • Minimal example. It create fullscreen window, without borders, always on top
    so you can't toggle to other window, ie. to console to kill program.

    You can close it by pressing ESC but I add function to close after 5 second if ESC will not work :)

    It draws red oval on full screen.

    #!/usr/bin/env python3
    
    import tkinter as tk
    
    # --- functions ---
    
    def on_escape(event=None):
        print("escaped")
        root.destroy()
    
    # --- main ---
    
    root = tk.Tk()
    
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    
    # --- fullscreen ---
    
    #root.overrideredirect(True)  # sometimes it is needed to toggle fullscreen
                                  # but then window doesn't get events from system
    #root.overrideredirect(False) # so you have to set it back
    
    root.attributes("-fullscreen", True) # run fullscreen
    root.wm_attributes("-topmost", True) # keep on top
    #root.focus_set() # set focus on window
    
    # --- closing methods ---
    
    # close window with key `ESC`
    root.bind("<Escape>", on_escape)
    
    # close window after 5s if `ESC` will not work
    root.after(5000, root.destroy) 
    
    # --- canvas ---
    
    canvas = tk.Canvas(root)
    canvas.pack(fill='both', expand=True)
    
    canvas.create_oval((0, 0, screen_width, screen_height), fill='red', outline='')
    
    # --- start ---
    
    root.mainloop()