use the system monospace font in gtk textview - python

I would like to have a GtkTextView in my (Python) program which shows text with the system monospace font. I found many ways which use an expicit font family name and size. However, I would like to use the system specified monospace font (e.g. from the ubuntu font preferences panel).
My program should be able to run on Windows as well as Linux without modifications, chosing automatically the right font.
to clarify, this is not what I want:
fontdesc = pango.FontDescription("Courier 18")
textview.modify_font(fontdesc)

You can just use "monospace 18" as your font and it will use the system monospaced font.

[available since 3.16]
set_monospace()
GTK3+ Doc
https://developer.gnome.org/gtk3/stable/GtkTextView.html#gtk-text-view-set-monospace
gtk_text_view_set_monospace ()
void
gtk_text_view_set_monospace (GtkTextView *text_view, gboolean monospace);
Sets the “monospace” property, which indicates that the text view should use monospace fonts.
Parameters
text_view a GtkTextView
monospace TRUE to request monospace styling

Related

Pyqt5: How to format text without using QFont?

Well as the title already tells, I want to simply format a text without using QFont(). At the moment I'm using it like that:
font = QFont()
font.setBold(True)
label = QLabel()
label.setFont(font)
label.setText("Hello World!")
So far so good. But if I want to have a certain part in a text of a label bold, it gets quite annoying, because I have to create an extra QLabel and use setBold() and put this part into the right position. Is there a way (e.g. markdown) to bold a certain part of a text of a label?
Like that:
label = QLabel()
label.setText("**Hello** World!")
Qt uses a subset of HTML for rich text. This is also the default setting. Try:
label.setText("<b>Hello</b> World!")
The label text format is controlled by the textFormat property. The default is Auto, for possible values see https://doc.qt.io/qt-5/qt.html#TextFormat-enum.
If you use a recent version of Qt (at least 5.14) you can also use Markdown as you suggested:
label.setTextFormat(Qt.MarkdownText)
label.setText("**Hello** World!")
Reference: https://doc.qt.io/qt-5/richtext-html-subset.html

Python Wand: how to get the text bolded?

How to generate bolded text using python wand library? I cant get it to work.
http://docs.wand-py.org/en/0.4.1/wand/drawing.html - regarding documentation styles that are supported:
'undefined;
'normal'
'italic'
'oblique'
'any'
there is no bold style?
sample usage i wanted to do a footer with a date that I get from contents file:
with Image(width=150, height=25,) as img:
draw.font_family = 'MS Reference Sans Serif'
draw.font_size = 14.0
draw.push()
draw.font_style = 'italics'
metrics = draw.get_font_metrics(img, contents['date'], multiline=False)
draw.text(int((img.width - metrics.text_width)/2), int((metrics.text_height)), contents['date'])
draw.pop()
draw(img)
img.save(filename='./temp/footer.png')
Maybe there is some way to make it bold in some quick way? Would really appreciate any help.
"bold" is not a font_style, it's a font_weight: http://docs.wand-py.org/en/0.4.1/wand/drawing.html#wand.drawing.Drawing.font_weight
And the integer values it takes are the values fonts typically use, where 400 is "normal" and 700 is "bold". (These are also used in CSS. See e.g. https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Common_weight_name_mapping.)
Although mercator offered a correct answer to the title question, this answer focusing on the nested question:
Maybe there is some way to make it bold in some quick way?
Try using the bold variant typeface directly.
Either specific the bold-font file provided by Microsoft ...
draw.font = 'refsanb.ttf`
... or use the font style with a "Bold" suffix.
draw.font_family = 'MS Reference Sans Serif Bold'

How can I get the existing font colour (CLI) in Python?

I know how to change the font colour of output in Python using this (for example)
print('\033[94m' + text)
However, the rest of the text will then stay that colour, even after the app has closed. I could set it to a certain colour, but the user may have changed the background colour and that would be a problem,
print('\x1b\x63') # Esc c
This will reset the terminal (RIS).

wxpython TextCtrl color specific part of the text

I'm using wxpython to create GUI with python, now, is there a way to color specific part of the text?
Example (using Tkinter):
http://i.stack.imgur.com/fGAVA.png
Here is my Print function:
def Print(self, String):
''' Print Received Data '''
self.text_serverLog.AppendText(datetime.now().strftime("[%H:%M:%S] " + String + "\n"))
I found the function SetForegroundColour but sadly its color the whole TextCtrl.
I'm using Win7, python 2.7, wxpython.
Thanks.
You can use the wx.TextCtrl in RichText mode by setting its style flag to wx.TE_RICH or wx.TE_RICH2. You can also use the aforementioned RichTextCtrl or the StyledTextCtrl (1 or 2). There's also FancyText, but I think that's more of a helper for drawing text than a real control the user can edit. Regardless, all of these widgets have examples in the wxPython demo package, which you can find at the wxPython website's download page.

How can I change the font size in GTK?

Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markup on a label, with something silly like:
lbl.set_markup("<span font_desc='Tahoma 5.4'>%s</span>" % text)
This 1) requires me to set the font , 2) seems like a lot of overhead (having to parse the markup), and 3) would make it annoying to change the font size of buttons and such. Is there a better way?
If you want to change font overall in your app(s), I'd leave this job to gtkrc (then becomes a google question, and "gtkrc font" query brings us to this ubuntu forums link which has the following snippet of the the gtkrc file):
style "font"
{
font_name = "Corbel 8"
}
widget_class "*" style "font"
gtk-font-name = "Corbel 8"
(replace the font with the one you/user need)
Then the user will get consistent experience and will be able to change the settings easily without need for them to poke in the code and without you needing to handle the overhead of maintaining your personal configuration-related code. I understand you can make this setting more specific if you have a more precise definition for the widget_class.
YMMV for different platforms, but AFAIK this file is always present at some location if GTK is being used, and allows to the user to be in charge of presentation details.
In C, you can do:
gtk_widget_modify_font(lbl, pango_font_description_from_string("Tahoma 5.4"));
In PyGTK, I believe it's something like:
pangoFont = pango.FontDescription("Tahoma 5.4")
lbl.modify_font(pangoFont)

Categories