<div dir="ltr"><div><div>[I have posted this same question on stackoverflow, <a href="http://stackoverflow.com/questions/37979079/update-gtk-menu-from-web-flask-app">http://stackoverflow.com/questions/37979079/update-gtk-menu-from-web-flask-app</a>, with no responses; i hope to be more lucky here]<br><br></div>Addition: I suppose this problem is something related to the app context, but I can't imagine how to make this work.<br><br></div>Hi all<br><br>I am trying to create an Ubuntu indicator, that launches a Flask app in a separate thread (using multiprocessing.Process), and from an action in the Flask app, add an item to the indicator menu (in this simplified code, just when the root page is loaded, a menu item with the current datetime should be created). The problem is that although I get no errors, the menu is not created. I am using GLib.idle_add as recommended in other places without success. This is the code:<br><br>#!/usr/bin/env python<br># -*- coding: UTF-8 -*-<br><br>import argparse<br>from datetime import datetime<br>from flask import Flask<br>from multiprocessing import Process<br>import signal<br><br>import gi<br>gi.require_version('Gtk', '3.0')<br>gi.require_version('AppIndicator3', '0.1')<br>from gi.repository import Gtk, GLib, AppIndicator3<br><br><br>APP_ID = "test"<br>APP_ICON = "test"<br><br>class GtkFlaskIndicatorTest(object):<br><br>    def __init__(self, debug):<br><br>        self.indicator = AppIndicator3.Indicator.new(APP_ID, APP_ICON, AppIndicator3.IndicatorCategory.APPLICATION_STATUS)<br>        self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)<br><br>        GLib.threads_init()<br><br>        self.app = GtkFlaskApp(self, debug)<br>        self.app.start_app()<br><br>        self.menu = None<br>        self.quit_menu_item = None<br>        self.build_menu()<br><br>        Gtk.main()<br><br>    def build_menu(self):<br><br>        self.menu = Gtk.Menu()<br><br>        self.quit_menu_item = Gtk.MenuItem(u"Quit")<br>        self.quit_menu_item.connect('activate', self.quit)<br>        self.menu.append(self.quit_menu_item)<br><br>        self.menu.show_all()<br>        self.indicator.set_menu(self.menu)<br><br>    def add_menu_item(self, text):<br>        print "add_menu_item: " + text<br>        GLib.idle_add(self.add_menu_item_cb, text)<br><br>    def add_menu_item_cb(self, text):<br>        print "add_menu_item_cb: " + text<br>        item = Gtk.MenuItem(text)<br>        self.menu.append(item)<br>        self.menu.show_all()<br><br>    def quit_control_c(self, signal, frame):<br>        self.quit(None)<br><br>    def quit(self, source):<br>        self.app.stop_app()<br>        Gtk.main_quit()<br><br><br>class GtkFlaskApp(object):<br><br>    def __init__(self, indicator, debug):<br>        self.indicator = indicator<br>        self.debug = debug<br>        self.app = Flask("GtkFlaskFlaskApp")<br>        self.app.add_url_rule('/', 'test', self.test)<br><br>    def test(self):<br>        text = str(datetime.now())<br>        self.indicator.add_menu_item(text)<br>        return text<br><br>    def start_app(self):<br>        self.app_server = Process(target=self.start_app_thread)<br>        self.app_server.start()<br><br>    def start_app_thread(self):<br>        self.app.run(port=8888, debug=self.debug, use_reloader=False)<br><br><br>if __name__ == "__main__":<br><br>    signal.signal(signal.SIGINT, signal.SIG_DFL)<br><br>    parser = argparse.ArgumentParser()<br>    parser.add_argument('--debug', action='store_true')<br>    args = parser.parse_args()<br><br>    GtkFlaskIndicatorTest(args.debug)<br></div>