You can implement your OTF
font using @font-face like:
@font-face {
font-family: GraublauWeb;
src: url("path/GraublauWeb.otf") format("opentype");
}
@font-face {
font-family: GraublauWeb;
font-weight: bold;
src: url("path/GraublauWebBold.otf") format("opentype");
}
// Edit: OTF now works in most browsers, see comments
However if you want to support a wide variety of browsers i would recommend you to switch to WOFF
and TTF
font types. WOFF
type is implemented by every major desktop browser, while the TTF
type is a fallback for older Safari, Android and iOS browsers. If your font is a free font, you could convert your font using for example a transfonter.
@font-face {
font-family: GraublauWeb;
src: url("path/GraublauWebBold.woff") format("woff"), url("path/GraublauWebBold.ttf") format("truetype");
}
If you want to support nearly every browser that is still out there (not necessary anymore IMHO), you should add some more font-types like:
@font-face {
font-family: GraublauWeb;
src: url("webfont.eot"); /* IE9 Compat Modes */
src: url("webfont.eot?#iefix") format("embedded-opentype"), /* IE6-IE8 */
url("webfont.woff") format("woff"), /* Modern Browsers */
url("webfont.ttf") format("truetype"), /* Safari, Android, iOS */
url("webfont.svg#svgFontName") format("svg"); /* Legacy iOS */
}
You can read more about why all these types are implemented and their hacks here. To get a detailed view of which file-types are supported by which browsers, see:
@font-face Browser Support
EOT Browser Support
WOFF Browser Support
TTF Browser Support
SVG-Fonts Browser Support
There's a much better way than using .config()
to change your application font, especially if your goal is to change the font for a whole group of widgets (or all widgets).
One of the really great features of Tk is the notion of "named fonts". The beauty of named fonts is, if you update the font, all widgets that use that font will automatically get updated. So, configure your widgets once to use these custom fonts, then changing the attributes is trivial.
Here's a quick example:
# python 2 imports
# import Tkinter as tk
# import tkFont
# python 3 imports
import tkinter as tk
import tkinter.font as tkFont
class App:
def __init__(self):
root=tk.Tk()
# create a custom font
self.customFont = tkFont.Font(family="Helvetica", size=12)
# create a couple widgets that use that font
buttonframe = tk.Frame()
label = tk.Label(root, text="Hello, world", font=self.customFont)
text = tk.Text(root, width=20, height=2, font=self.customFont)
buttonframe.pack(side="top", fill="x")
label.pack()
text.pack()
text.insert("end","press +/- buttons to change\nfont size")
# create buttons to adjust the font
bigger = tk.Button(root, text="+", command=self.OnBigger)
smaller = tk.Button(root, text="-", command=self.OnSmaller)
bigger.pack(in_=buttonframe, side="left")
smaller.pack(in_=buttonframe, side="left")
root.mainloop()
def OnBigger(self):
'''Make the font 2 points bigger'''
size = self.customFont['size']
self.customFont.configure(size=size+2)
def OnSmaller(self):
'''Make the font 2 points smaller'''
size = self.customFont['size']
self.customFont.configure(size=size-2)
app=App()
If you don't like that approach, or if you want to base your custom font on the default font, or if you're just changing one or two fonts to denote state, you can use font.actual
to get the actual size of a font for a given widget. For example:
import Tkinter as tk
import tkFont
root = tk.Tk()
label = tk.Label(root, text="Hello, world")
font = tkFont.Font(font=label['font'])
print font.actual()
When I run the above I get the following output:
{'family': 'Lucida Grande',
'weight': 'normal',
'slant': 'roman',
'overstrike': False,
'underline': False,
'size': 13}
Best Answer
One way is to have a hidden picture box and setup the font specs of that picture box the way you want.
Then use the TextHeight and TextWidth methods of the PictureBox to take your measurements. The Units will be in whatever scalemode the Picture Box is set to.
If you are printing directly to the printer or form then just set your font FIRST then take your measurements.
To center it
Substitute displayarea with whatever object you are using.
Based on your updated answer note that the hidden picture box suggestion isn't used to print. It is only get text measurement. However you are printing directly to the form so you just need to use the code example above.