Home>
How can I use Tkinter in Flask?
Specifically, the Tkinter code:
from Tkinter import *
from ScrolledText import *
root = Tk ()
t = ScrolledText ()
t.pack ()
root.mainloop ()

and Flask code:
from flask import Flask, request
app = Flask (__ name__)
@ app.route ("/")
def index ():
    return app.send_static_file ("index.html")
@ app.route ("/hoge&quot ;, methods = ["GET"])
def hoge ():
    hoge = request.args ["hoge"]
    return hoge
app.run (debug = True)

I want to write the hoge I got with Flask to Tkinter's ScrolledText.
I examined multithreading and subprocesses, but I didn't understand.
  • Answer # 1

    Excuse me,
    In short, you want to exchange information between the client application and the web application.
    There are various ways to pass information between processes.
    Generally, I think that I use a database, but if the above two applications are running on the same terminal and writing is always only on the Web application side, it can also be a file.
    There are other things such as shared memory delivery, message pipes, and message queues.
    If you need to update the screen at the event (timing) received by request in Flask, there are two possible ways.
    (1) The content of the database and file is rewritten on the Flask side, and the Tkinter application side goes to retrieve the information at regular intervals. Update the screen if it has been updated.
    (2) The Flask side sends an event to the Tkinter app at the timing of receiving the request, and at that timing, the Tkinter app updates the content with the received information.
    I think that the classic and simple one is ①, but if that doesn't work, I think it's easy to implement interprocess communication using multiprocessing.
    Interprocess communication with multiprocessing module
    End of reference.