Python Flask: getting an error when server shutdown is declined

I am a newbie at FLask. I am building an app which uses Flask as framework. the app and server is supposed to work only on localhost.

When exiting the app I also want to shutdown the server.

I found the nice way to stop the server here

I placed a button in the htmll layout header (so it’s there on all the pages) that uopn clicking shows a Tkinter dialog box. Now, before shutting down the server the app asks the user to confirm the action. No problems shutting down, all goes as expected. But answering “No” to the messagebox I created (Tkinter), makes that the second time I try to use the Exit button I get an error message.

here is my code.


from flask import Flask, render_template, url_for, request, url_for, redirect
from tkinter import filedialog, messagebox
from tkinter import *

app = Flask(__name__)

def dialog_yn(title="mesage Box", prompt="Make your choice"):
    result = messagebox.askquestion(title=title,message=prompt,icon='warning')
    return result

def index():
    return render_template('index.html')

@app.route('/shutdown', methods=['GET'])
def shutdown():
    answer = dialog_yn('Exit Configurator', 'Do you really want to quit the configurator?')
    if answer == 'yes':
        return 'Server shutting down...'
        return redirect(url_for('index'))

def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')

if __name__ == "__main__":

Upon annswering no everything seems to go back to the index page. the error actually triggers the second time I hit the exit button (submit button in a dummy form). Did I already say that?
the error I get is

  File "", line 24, in shutdown
    answer = dialog_yn('Exit Configurator', 'Do you really want to quit the configurator?')
  File "", line 13, in dialog_yn
    result = messagebox.askquestion(title=title,message=prompt,icon='warning')
  File "", line 99, in askquestion
    return _show(title, message, QUESTION, YESNO, **options)
  File "", line 72, in _show
    res = Message(**options).show()
  File "", line 40, in show
    w = Frame(self.master)
  File "", line 3119, in __init__
    Widget.__init__(self, master, 'frame', cnf, {}, extra)
  File "", line 2567, in __init__
RuntimeError: main thread is not in main loop

What am I doing wrong? As I said I am starting with Flask (and Tkinter for that matter), so go easy on me. thnx for the patience.


You need to destroy the Tk() instance after closing the messagebox:

def dialog_yn(title="mesage Box", prompt="Make your choice"):
    root = Tk()
    result = messagebox.askquestion(title=title, message=prompt, icon='warning')
    return result