Search code examples
pythonpy2app

Is there a way to reload a python rumps script on click?


I'm new to python and I'm building a simple time zone app with rumps to make it a status bar app and py2app for packaging where I display a few different time zones and would like a refresh button to reload the time displayed.

I have read about using

import importlib

to reload modules but I'm not clear on how to use this method with the rumps library.

This is my current script:

import rumps
import datetime
import pytz
import os 
import sys
import importlib

class TimeZone(object):
    def __init__(self):
        super(TimeZone, self)
        self.config = {
            "app_name": "TimeZone",
            "ld_current_time" : "London " +  datetime.datetime.now(pytz.timezone('Europe/London')).strftime('%I:%M %p'),
            "ny_current_time" : "New York " + datetime.datetime.now(pytz.timezone('US/Eastern')).strftime('%I:%M %p') ,
            "la_current_time" : "Los Angeles " + datetime.datetime.now(pytz.timezone('US/Pacific')).strftime('%I:%M %p'),
         }
        self.app = rumps.App(self.config["app_name"])
        self.ld = rumps.MenuItem(title=self.config["ld_current_time"],callback='')
        self.ny = rumps.MenuItem(title=self.config["ny_current_time"],callback='')
        self.la = rumps.MenuItem(title=self.config["la_current_time"],callback='')
        self.set_up_menu()


    def set_up_menu(self):
        self.app.title = "⏳"
        self.app.menu = [self.ld,self.ny,self.la]


    @rumps.clicked('Refresh')
    def refresh(self):
        print('refresh')
        rumps.MenuItem.update(self.app.menu,1)
        #self.menu.update = [self.ld,self.ny,None,self.la,None]


    def run(self):
        self.app.run()

if __name__ == '__main__':
    app = TimeZone()
    app.run()

Any input would be greatly appreciated. Cheers!


Solution

  • I solved this by using the built-in os and sys python modules:

            @rumps.clicked('restart')
            def restart(self):
                print('restarted')
                os.execl(sys.executable, sys.executable, * sys.argv)