I'm trying to make a little GUI script using tk. After computation on the basis of user input, I generate a simple string that I want to print using a listbox, but it was suffering from alignment problems.
I tried printing the output in the console at the same time to check whether this was a formatting error:
for loop :
string = foo(x)
listbox.insert(END, string)
print string
The problem is that the console is using a fixed width font but the listbox is using a variable width font. In a variable width font, characters like "i" (lowercase I) and "l" (lowercase L) take up less horizontal space that characters like "M" and "0".
If you want characters to line up in a listbox like they do in the console, you need to use a fixed width font. You can configure the font used by the listbox via the font attribute.
Tkinter provides several default fonts, the default fixed-width font is named "TkFixedFont". This default font will be approximately the same vertical height as the default variable width font that is used by other widgets. The exact font that is chosen may be different on different platforms, but is typically a variant of courier.
For example:
import Tkinter as tk
root = tk.Tk()
listbox = tk.Listbox(root, font="TkFixedFont")
If you wish to be explicit about the font family and size, you can provide that as a string, tuple, or as a font object. For example, picking a courier font of size 18 could be specified as font="Courier 18".
listbox = tk.Listbox(root, font="Courier 18")
For more information on fonts, see the TkDocs tutorial on fonts, colors and images and the section Widget Styling on effbot.
Related
I am trying to write code in Motif to change a dialog warning box to resize size it if the box is not wide enough. The width and height is always being set by the calling classes and its not always wide enough for the message being displayed and the end of the line is truncated off. Instead of fixing everywhere to use auto sizing (i.e. width is 0 or not set at all) they want to figure out what the pixel width size is for a character in the dialog. They can then multiple the longest line X pixels width to get the lines length in pixels. Then we would see if the dialog declared width needs to be reset to stop the truncation. Only dialogs that are too short will be changed (dialogs too wide are not to be changed).
However; I can't find any example on how get the character width in pixels anywhere. I remember years ago I was on a project where they created some type of widget, inserted a character into it, and then did a XtGetValues to get the width and height so I think it can be done. So does anyone know how to do this?
That was a long time ago, but if memory serves, Xt doesn't have any specific support for fonts, it relies on plain libx11. You will need to call XQueryFont or XLoadQueryFont to get the XFontStruct describing your font, then grovel through the per_char array to find the extents of individual glyphs.
Today I switched the font of my application from one of the standard system fonts to Sinkin in the form of an otf file, I've added to my resource.qrc.
The following code sets the default font of the application object.
int id = QFontDatabase::addApplicationFont(":/fonts/font.otf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
qapp.setFont(family);
QFont font = qapp.font();
font.setPointSize(opt_font_size);
qapp.setFont(font);
Now it seems, that the font is placed a bit too high in all elements. That means buttons, labels, input fields and tabs. Or Qt does not realize, that it has to adjust the size of the elements to fit the new font.
Changing the font size via setPointSize does not fix it. Everything just gets smaller or bigger.
Any ideas where I can dig into?
I use CreateTextLayout and CreateTextFormat to draw text with DirectWrite (C++), the text is mixed Hebrew/English, is there a way to use a different font/font size for the Latin and Hebrew characters?
Thanks.
When you create the IDWriteTextFormat using CreateTextFormat, you can pass the name of the font family in the first parameter and you can change the size of the font in sixth parameter.
You can get the CreateTextFormat parameters from MSDN.
Here is a list of Microsoft Windows font families, you will find ones in Latin and Hebrew there.
I ended up calling IDWriteFont::HasCharacter to build text ranges inside which all characters use the same font (the hebrew or the latin one).
If that text range uses the hebrew font, I call textLayout->SetFontSize on it to increase the font size.
This amounts to anticipating the font callback DirectWrite will do, which is automatic and cannot be customized.
I'm working on my edit box that I'm creating. I need to find a font that draw letter at same size. I'm using directx
I'm guessing that by "find a font that draw letter at same size" you mean a font where all of the letters are the same size. This is called a Monospaced or Fixed Width Font. There should be several on most systems, the one that you should be able to count on always existing on Windows systems is "Courier New".
It is observed in some fonts sat Verdana, that the rendering for bold and normal text does not occupy the same width for a given text.
In my application i am making use of one such font ,and there is UI with list and highlighted item.
for highlighted item the font is same with bold attribute ,because of above mentioned font issue the text appears to move(enlarge and occupy more space )horizontally when the list is scrolled.
I can use bold font with one less in font size which seem ok but not perfect
Is there any corrective measure to resolve this?
What you describe is the default attribute of most proportional fonts. If having the string width increased seems ugly, try using other visual methods to draw attention to the selected item, such as changing the background color.
You could make the list items wide enough to accommodate size increase when it becomes bold.
If the highlight has to be bold, most likely your only option is using a proportional font (doesn't need to be courier, you may find some sans-serif proportional fonts acceptable).