Comments (5)
Thanks for explaining Bold and Oblique, Cheerless Dreamer.
I did notice (in regards to your EDIT) that NSColor appears different in dark mode if you don't specify the alpha (the opacity). By default, the alpha is set to 0.5 for the rumps module.
Change the following:
color = NSColor.redColor()
to:
color = NSColor.colorWithCalibratedRed_green_blue_alpha_(1, 0, 0, 1)
And now, when you go into Dark Mode, it is completely red.
from rumps.
How would one bold the font here? Or italicize? I grep'd NSFont in Terminal and can't find anything useful.
The font used in the sample (Monaco) doesn't actually have any options apart from the regular weight, at least on my machine. You can use the following line as a test instead:
font = NSFont.fontWithName_size_("Helvetica", 18.0)
… and for a bold and italic version:
font = NSFont.fontWithName_size_("Helvetica Bold Oblique", 18.0)
You can get the correct name by using the 'Font Book' application.
EDIT: Note that using Dark Mode alters the appearance somewhat, see attached screenshot for an example. The red in the sample code appears pink under Dark Mode.
from rumps.
It can de done by using NSAttributedString which let's you define strings with attributes such as font, background color, foreground color, underline, etc. To see all options check the docs. The MenuItem class rumps provides is a wrapper for AppKit's NSMenuItem which has a attributedTitle property, rumps does not provide a way to set it so you'll need to access MenuItem's _menuitem internal variable and use some PyObjC. Here's a sample:
import rumps
from AppKit import NSAttributedString
from PyObjCTools.Conversion import propertyListFromPythonCollection
from Cocoa import (NSFont, NSFontAttributeName,
NSColor, NSForegroundColorAttributeName)
if __name__ == "__main__":
font = NSFont.fontWithName_size_("Monaco", 18.0)
color = NSColor.redColor()
"""
Dictionary of attributes
propertyListFromPythonCollection is used to create NSDictionary from a python dict
conversionHelper=lambda x: x is used because font and color are alreaady cocoa objects
and do not need to be converted
"""
attributes = propertyListFromPythonCollection({
NSFontAttributeName: font,
NSForegroundColorAttributeName: color}
, conversionHelper=lambda x: x)
string = NSAttributedString.alloc().initWithString_attributes_("Foo", attributes)
menu_item = rumps.MenuItem("")
menu_item._menuitem.setAttributedTitle_(string)
menu = [menu_item]
app = rumps.App("Sample", menu=menu)
app.run()
from rumps.
Diego, thanks for the sample code! Super useful.
from rumps.
How would one bold the font here? Or italicize? I grep'd NSFont in Terminal and can't find anything useful.
from rumps.
Related Issues (20)
- Is Rumps Python 3 compatible? HOT 1
- When using the alert function
- Access parent menu title from sub menu
- Basic test app rumps.clicked not working HOT 2
- Have a onoff button clicked by default HOT 2
- [help] How can I update string shows up on the to bar every minute HOT 2
- Can we apply color in the title string?
- rumps.alert or rumps.Window will halt rumps.timer(x) HOT 1
- Menu item key combination
- rumps App gets hidden if title is too long HOT 2
- Mac M1 issue HOT 3
- AppKit depreciated - ImportError: cannot import name 'NSApplication' from 'AppKit' HOT 2
- [accidentally opened issue] HOT 1
- Add functionality for hover bubbles (NSPopover?)
- Make app title display content split to two lines. HOT 2
- Custom handler for KeyboardInterrupt HOT 2
- How to create icon for the menu that changes white/black HOT 2
- Does anyone know how to fix a "(no such file), '/System/Library/Carbon.framework/Carbon' (no such file, not in dyld cache)" error? HOT 5
- App title occasionally positioned badly, making it unreadable
- Cannot get basic app working on OSX HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rumps.