I am working on this project where I have Raspberry Pi 3 with full Raspbian (with GUI) connected to a 7" screen. I created a Python 3 script:

// gui.py

import tkinter as tk

root = tk.Tk()
root.title("My Dash")

I then created a shell script to run this file:

// test.sh

python3 gui.py

Finally I created a PHP file to run this shell script:

// run.php

    shell_exec("sudo /bin/bash test.sh");

All these files reside in /var/www/html with www-data set as both the user and user-group. However, when you run the PHP file nothing happens.

I've tried redirecting errors to a log file and it gives me the following error:

(tkinter.TclError: couldn't connect to display ":0.0")

If I run the Python file directly in the terminal, it displays the GUI successfully.

Can TKinter scripts only be run from the terminal and not from a webpage? If so, is there some kind of workaround? I'm trying to develop a web application where you can fire off TKinter windows from buttons on the site.


To the best of my knowledge you cannot display GUI screens created with tkinter on a web page. (Irrelevant but for completeness - You can display a web page in Tkinter though.)

Some of the functionality (widgets) of tkinter has been 'HTML-y-fied' by a package called tk_html_widgets available on GitHub here. I've never tried this but it may simplify conversion.

If this does not give you the richness of your application screens, there are a few possible ways around this:

  1. Create the screen using php etc and display it as a web page
  2. Embed the Python program in HTML to create the screen.
  3. Use the inbuilt web server for Python and Flask
  4. Go full blown web development using Django

Other web development frameworks are available (e.g. web2py / Pyramid etc) for Python.

  • I dont want to open tkinter into html. I want a button on my page to run python file that has tkinter code.. As a separate window and not inside html page – Muhammad Zeeshan Akram Dec 4 '20 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.