Qt Designer Custom ttf font with Python - python

Both this question Add a custom font in Qt and this link mentioning the ability to add fonts through c++. Is this possible through python and the Qt Designer framework? I have attempted adding the ttf to resources and compiling the qrc file. Then in the stylesheet defining the font.
When launching the app nothing happens. Is there something else that needs to be done? The example discusses loading binary files.

You need to also add it to the QFontDatabase:
First add the font file “yourfont” to the resource editor (which you have done) and then to the fontdatabase
QFontDatabase fontDatabase;
fontDatabase.addApplicationFont(":/fonts/yourfont");
Now you can use yourfont with setFont for instance on a QLabel.

Since there is no mention in the answers/comments about QtDesigner, neither here nor in the linked posts, this is what I found about QtDesigner standalone (5.6) - about a font that is not installed in the system:
I've used a free font called AutobusBold.ttf. First, path to this file needs to end up in your qrc file, mostly for the sake of Python - you can use Resource Browser/Edit Resources (the pencil) for this; you will end up with something like this in the .qrc file:
<file>myfont/AutobusBold.ttf</file>
If you open the .ttf file with fontview.exe on Windows, you'll see its name is Autobus Bold - with a space.
Then you need to close the .ui file in QtDesigner, then go to Settings/Additional Fonts... in QtDesigner (if an .ui file is open, then this option is greyed out); there click the + ("Add font file"), find the .ttf font file, and add it by selecting it and hitting Open. From now on, this font will appear whenever the Font dialog is opened in QtDesigner, mixed with the system fonts.
Then, reopen your .ui file, find the element you want to apply this font to and select it, in its styleSheet option click the three dots, and from the resulting dialog window, click Add Font - you'll get the Font dialog, with all system fonts - but also the custom font will be there; select it, and you'll get a .qss stanza like this: font: 75 8pt "Autobus Bold";
That first 75 is probably font-weight, you can also delete it, it will work the same; the 8pt is font-size, and "Autobus Bold" is font-family. Notice here that the font name in font-family is quoted, as it has a space in the name - without the quotes, everything fails!
Then, if you write an extern .qss file, you can rewrite the font: stanza as separate font-family (with quoted font name!) and font-size stanzas - and it should work the same.
Of course, if you want to transfer all this to Python, then you need to additionally addApplicationFont as described in the answer by #GPPK.

Related

Managing font installation/removal on Windows using python

After some researching I found out that I can install a truetype font for the current user by just making a new string value in HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts with NAME set to the font's name and DATA set to the font's path. Removing a font is also similar, first you remove the string value for the specified font and then delete the font file itself.
I tried to do it manually and it does work (both installing and removing a font). But recreating everything in python lands me into a problem. I'm able to install font using the above method but when I try to remove the font then windows tells me that the font file is currently being used by "System". My program is able to remove the registry value but fails to delete the font file.
My helper registry editor functions can be found at -> https://github.com/nk521/phonts/blob/07a4d6de5165d5c19861c2f28708c60d52f33df9/core/helpers/win.py
This function installs a font -> https://github.com/nk521/phonts/blob/07a4d6de5165d5c19861c2f28708c60d52f33df9/core/win.py#L15
This function deletes a font -> https://github.com/nk521/phonts/blob/07a4d6de5165d5c19861c2f28708c60d52f33df9/core/win.py#L35
This line will generate a PermissionError -> https://github.com/nk521/phonts/blob/07a4d6de5165d5c19861c2f28708c60d52f33df9/core/win.py#L41
On Windows, GDI may lock font files that have been used. You might not be able to delete the font file until that user session has ended. If you delete the registry entry, then the font won't get loaded into GDI in the next session. Try deleting the reg entry, then log off and back on, that might unlock the file; or if it has locked it in a different session, you might need to reboot.

How do I configure Python IDLE's text font?

I've searched all over and can't seem to find a solution. Python's IDLE just looks terrible on my laptop (Lenovo Yoga 2 Pro) with a 3200x1800 display running 8.1
I've attached a screenshot so you can see what I'm talking about. Has anyone figured out how to configure this? Thanks for the help!
Add a file like %USERPROFILE%\.idlerc\config-main.cfg
Add the following lines:
[EditorWindow]
font-size = 14
font = monaco
Or pick your favorite font and size.
I presume 'terrible looking' applies to the old Courier (typewriter) font. IDLE's tk Text windows (Shell, Editor, and Output) default to using 10 point 'TkFixedFont'. On Windows, (and only on Windows), that resolves to 'Courier'. I believe that this is or at least used to be standard on Windows.
The best solution is to select Options on the top menu bar and then Configure IDLE. One is then presented with a dialog with the Fonts/Tabs tab selected and the current font selected in the Base Editor Font box and the current size next to Size. There is an example box showing some text in the current font and size. Change either the font or size and the example is updated. Select OK or Apply and current text windows are updated.
Choices other than default are written to config-main.cfg in directory %USERPROFILE%/.idlerc/. The directory and file are created if necessary.
I personally use the fixed-pitch Lucida Console. I occasionally use variable-pitch Lucida Sans Unicode when using strings with non-Latin characters. One can tell from the example in the box whether a font is fixed or variable in character width by whether characters line up neatly in columns or not. (One of my goals is to add examples from several other scripts so one can also see the unicode coverage offered by a font.)

Is it possible to use pango markup text on a Gtk+ 3 toolbutton label using Glade and PyDev?

I have put together a Gtk+ interface in Glade and part of the UI is a tool palette with several toolbuttons using utf-8 characters as labels. They work fine in the default font, but I would like to change font details using pango markup. This is straightforward when dealing with a label as such, as one can apply
label.set_markup(pangoMarkupString)
but the label in a toolbutton can not, as far as I can tell, be addressed directly in this way. A naive
button.label.set_markup(pangoMarkupString)
naturally doesn't work and returns an error saying that toolbuttons do not have the label property. Is there any way to use pango parsed text in a toolbutton, and what depth of python trickery would be required at the application end?
To save any others from hours of fruitless head-scratching and searching, and to open the eyes of other newbies to the powers of Gtk+ 3 and Glade, I present the solution I found.
Right click on your tool palette in the outliner and select edit
Choose the hierarchy tab in the editor
Select your button in the hierarchy outliner
Under Label properties choose widget rather than text
Click on the selector icon at the right of the widget entry box
Add a new widget
Leave the tool palette editor
Select label1, the new widget you created in the outliner
You can now edit its id, label text and attributes
I don't need it yet, but I wonder how to do this with a regular button...

Change wxpython program's default title and icon in dialog box

I want to change the title of my program from its default "Python" to lets say "MyProgram". Also, if I choose "About" from this menu, the dialog box comes up with a picture of the Python logo. How can I change that image?
Pictures for reference:
http://i.imgur.com/iop5f0q.jpg
http://i.imgur.com/XjagYBA.jpg
I'm pretty sure you have to create an app bundle with py2app to get that changed. Otherwise it just defaults to Python.
See the following answer:
Changing WxPython app Mac menu bar title?
According to that, you will have to also make sure that the plist file is filled out correctly.

writing a font viewer - getting font properties, loading ttf dynamically

I'm trying to write a font viewer for TrueType / OpenType fonts with VB6 / VB5 code (under Windows).
it is surprisingly difficult:
1) in VB / winAPI, i did not find how to extract the font's name, or font properties in general.
2) i can install the font (using AddFontResource API function), but then have to uninstall it. However, while (AddFontResource" expects a pathname, removing the font requires the font's name which is unknown to me.
is there a way to use an non-installed font ttf) ?
is there a way to extract a font's properties using vb6 ?
(I can write the program in wxPython but i know even less about fonts in python than with VB)
You could use the FreeType library.
It indeed is. I have faced the same problem myself (see my question). I ended up writing my own parser though because I needed to detect if the font was corrupt or not. There is a AddFontMemResourceEx function which:
When the function succeeds, the caller of this function can free the memory pointed to by pbFont because the system has made its own copy of the memory. To remove the fonts that were installed, call RemoveFontMemResourceEx. However, when the process goes away, the system will unload the fonts even if the process did not call RemoveFontMemResource.
Also, you can use the Font and Text Functions to get the font metrics.

Categories