问题
So I've been working on a little python application using rumps and I'd like to periodically update the title of the application in the status bar. There appears to be a function in rumps that should do what I'm looking for but I can't seem to get it to work, here's an adaption of some example code that shows the issue I'm running into:
import rumps
class AwesomeStatusBarApp(rumps.App):
def __init__(self):
super(AwesomeStatusBarApp, self).__init__("Awesome App")
self.menu = ["updating"]
@rumps.timer(1)
def sayhi(self, _):
super(AwesomeStatusBarApp, self).title(self,"Hi")
if __name__ == "__main__":
AwesomeStatusBarApp().run()
The super call in the init function works just fine, and the title function in the sayhi function should do exactly what I'm looking for, update the title and tell NSStatusBar to update it, however I'm failing with the following result:
2014-06-18 10:03:26.033 Python[29628:1107] : 'NoneType' object is not callable
And then a large traceback (Which I can provide, it just didn't format well).
I think that the error I'm running into may have something to do with the threading going on, however I'm at a loss of what to do. I tried shifting away from rumps, but I can't get NSStatusBar to work on its own, it always throws its own error. I'm looking to do something really simple, but it seems like I can never get it to work right, which is a pity.
Any help or advice is appreciated, thanks!
回答1:
There are at least two problems with your code:
The call to super() in
sayhi
is not necessaryIn the call to
.title()
insayhi
you shouldn't pass a "self" argument
I have no idea whether either of these is related to your problem without seeing the traceback.
回答2:
The problem isn't with rumps or PyObjC - just a few simple Python errors. You may want to read up on how classes work in Python.
Ronald's two points are correct about this line,
super(AwesomeStatusBarApp, self).title(self, "Hi")
There is no need to call the superclass implementation,
self.title(self, "Hi")
But this is still wrong since you never want to pass self
between methods in a class - that happens automatically,
self.title("Hi")
Still this is wrong as title
is a property so rewrite as,
self.title = "Hi"
Full code:
import rumps
class AwesomeStatusBarApp(rumps.App):
def __init__(self):
super(AwesomeStatusBarApp, self).__init__("Awesome App")
self.menu = ["updating"]
@rumps.timer(1)
def sayhi(self, _):
self.title = "Hi"
if __name__ == "__main__":
AwesomeStatusBarApp().run()
回答3:
You can update application title by setting title
property of class.
You can do self.title = "New Title"
in any method of class.
You can also do instance.title = "New Title"
. It updates itself immediately.
来源:https://stackoverflow.com/questions/24291386/rumps-updating-application-title