jaredks / rumps

Ridiculously Uncomplicated macOS Python Statusbar apps
BSD 3-Clause "New" or "Revised" License
3.06k stars 177 forks source link

Have a onoff button clicked by default #189

Closed AmberSahdev closed 1 year ago

AmberSahdev commented 1 year ago

Hi First off, thanks for creating this ridiculously uncomplicated library.

I wanted to ask if there's a way to have onoff buttons clicked by default upon application launch.

I'd have to set the sender.state to True but I dont see a way to access the sender object outside the function that enters upon clicking the button.

Thanks

jaredks commented 1 year ago

There are a few ways to do this. I think this is cleanest:

import rumps

app = rumps.App('test')

@rumps.clicked('Action')
def on_action_click(menuitem):
    menuitem.state = not menuitem.state

@rumps.events.before_start.register
def before_start():
    app.menu['Action'].state = 1

app.run()

But you can also create the MenuItem manually like so:

import rumps

app = rumps.App('test')
action = rumps.MenuItem('Action')
action.state = 1
@action.set_callback
def on_action_click(menuitem):
    menuitem.state = not menuitem.state

app.menu.add(action)
app.run()
AmberSahdev commented 1 year ago

Thanks