[FIXED] Is it possible to get a python program that uses tkinter to run in a web page using flask?

Issue

I have a Python program that I want to use with my web server. To do that, we need to transform it into a web environment. I read that it can be done with flask. The challenge is that the program is a GUI made with tkinter. When I run the following program with cmd:flask run I get a local host (http://127.0.0.1:5000/). When I copy the address and type it into a web browser, the web window shows an error Internal Server Error, but a new window with a check button and a label opens on my computer. The program works. What I need is to put that window inside a web window with a checkbutton and a label. This will allow you to finally put it on your server. (I have hundreds of widgets that forced me to use place.method and I don’t want to code everything from scratch in another language). Is there anyway to do that?

From Flask Import Flask
From tkinter import *
app = Flask(__name__)

@app.route("/")
Def Halo():

    Root = Tk()

    out = Label(root, text="0", bg="red")

    Default out_result():
        out.configure(text="Button pressed")

    button1 = Checkbutton(root, command=out_result)

    Button 1. Location (x=20, y=20)
    out.place(x=50, y=20)

    root.mainloop()
 

Solution

No, you cannot run tkinter programs via a flask app. Tkinter applications require a physical display, or an emulated physical display. It’s not literaly impossible — I know there is at least one website that lets you run tkinter code in a browser — but I’m sure it would be much easier to rewrite your code than to try to run it in a browser.

TL;DR: while not literally impossible, it will not work out of the box.

Answered By – Bryan Oakley

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published