Page 1 of 1

[solved] gtk builder pango font choice not displayed

Posted: Tue Oct 24, 2017 8:14 pm
by lambcutlet
update, see post #5

hello, I need some help that has bugged me for a while, displaying text in a gui with a font and font size of my choosing.
I've simplified a gui below to help explain my issue with a text box and a button.
Each time I run this simple program on my raspberry pi the font and font size I have chosen within the python program does not transfer through to the gui. It doesn't matter if the program is started as user 'python test.py' or with sudo rights 'sudo python test.py' The font choice is always overridden.

in the program I have selected 'LiberationMono' as an example so both text lines should be of equal overall length.
In the text box - The text should be 'bold' size '10' and the length of both lines of text should be the same.
The button - The text should be 'bold' size '14' (when pressed it changes colour which does function)

Running the program on a raspberry pi the font choice is overridden to what seems to be a system default font and font size.
Running the same program on a Fedora system everything functions as programmed the font and size are correct.
the location of 'LiberationMono-Bold.ttf' is in the same location on both OS's '/usr/share/fonts/truetype/liberation/LiberationMono-Bold.ttf'

I have googled / searched these forums but to date I have no solution. Can anyone suggest a fix?

program name 'test.py'

Code: Select all

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject, Pango

GObject.threads_init()

class Handler:
	def __init__(self):
		self.builder = Gtk.Builder()
		self.builder.add_from_file("window.glade")
		self.builder.connect_signals(self)
		self.window = self.builder.get_object("window1")
		
		self.textbox_text = self.builder.get_object("textbox1")
		self.textbox_text.modify_font(Pango.FontDescription('LiberationMono Bold 10'))
		self.textbox_buffer = self.textbox_text.get_buffer()
		
		self.button = self.builder.get_object("button")
		self.button.modify_font(Pango.FontDescription('LiberationMono Bold 14'))
		self.button.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse("green"))
	
	# initial values
		self.T_ambient = 3
		self.T_rect = 543.3
		
		self.integer_id = GObject.timeout_add_seconds(1, self.display_values)	# timer in seconds
	
	# button control
	def button_toggled_cb(self, toggled):
		if self.button.get_active():
			self.button.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse("red"))
			self.button.set_label("On")
			self.brake_state = "on"
		else:
			self.button.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse("green"))
			self.button.set_label("Off")
			self.brake_state = "off"
		
	def onDeleteWindow(self, *args):
		Gtk.main_quit(*args)
			
	def display_values(self):
		self.T_ambient += 1.0
		string =  (u" ambient:   %6.2f\N{degree sign}C\n" % (self.T_ambient))
		string += (u" rectifier: %6.2f\N{degree sign}C\n" % (self.T_rect))
		
		self.print_stuff_onscreen(string)
		return True
	
	def print_stuff_onscreen(self, string):
		self.textbox_buffer.set_text(string)
		return
		
if __name__ == "__main__":
	try:
		app = Handler()
		app.window.show()
		Gtk.main()
	except:
		print("failed to run program.")
	finally:
		quit()
Configured using Glade and saved as 'window.glade'

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.10"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <signal name="delete-event" handler="onDeleteWindow" swapped="no"/>
    <child>
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_top">10</property>
        <property name="margin_bottom">10</property>
        <property name="row_spacing">3</property>
        <property name="column_spacing">6</property>
        <child>
          <object class="GtkScrolledWindow">
            <property name="width_request">200</property>
            <property name="height_request">50</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="window_placement">bottom-left</property>
            <property name="shadow_type">in</property>
            <child>
              <object class="GtkTextView" id="textbox1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
              </object>
            </child>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkToggleButton" id="button">
            <property name="label" translatable="yes">Off</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="toggled" handler="button_toggled_cb" swapped="no"/>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Re: gtk builder pango font choice not displayed

Posted: Tue Oct 24, 2017 9:43 pm
by PeterO
It's been a while since I looked into this, but I think you may have to venture into GTK3's use of CSS:
https://developer.gnome.org/gtk3/stable ... rview.html
PeterO

Re: gtk builder pango font choice not displayed

Posted: Wed Oct 25, 2017 8:23 pm
by lambcutlet
PeterO,
I'll have a look into the CCS stuff as an alternative method.
But what i don't understand is why it works on my Fedora system?
is it a debian based problem or just related to Jessie on the pi.

Re: gtk builder pango font choice not displayed

Posted: Wed Oct 25, 2017 9:37 pm
by PeterO
I've seen the same thing happen between Mint and Raspbian. Compile and run the same code on both and get different fonts in buttons etc.
I'm pretty sure I worked out is was a CSS issue, but I can;t remember if I fixed it or if I was just content that I had found the cause.

PeterO

[found the answer] Re: gtk builder pango font choice not displayed

Posted: Thu Oct 26, 2017 9:43 pm
by lambcutlet
found the answer in this location

Code: Select all

/home/*username*/.config/gtk-3.0/gtk.css
this file overrides my settings of 'Pango.FontDescription'.
for now i have just renamed the file to 'gtk.css.bak'
is there anything special about this file? do i need to keep it?

edit: the file must be present, if removed it returns after a reboot. Instead remove its contents to leave it blank.

Code: Select all

@define-color theme_base_color #ffffff;
@define-color theme_bg_color #edeceb;
@define-color theme_selected_bg_color #4d98f5;
@define-color theme_text_color #1a1a1a;
@define-color theme_fg_color #000000;
@define-color theme_selected_fg_color #ffffff;

* {
	font-family:	Piboto Light;
	font-size:	12pt;
	font-weight:	Normal;
	font-style:	Normal;
}

.scrollbar {
	-GtkScrollbar-has-forward-stepper: true;
	-GtkScrollbar-has-backward-stepper: true;
}

Re: [solved] gtk builder pango font choice not displayed

Posted: Thu Oct 26, 2017 10:54 pm
by gkreidl
Did you check if the file is recreated on each reboot (to the desktop).
I had to remove rasperrypi-ui-mods to permanently get rid of it.

Re: [solved] gtk builder pango font choice not displayed

Posted: Fri Oct 27, 2017 6:54 am
by lambcutlet
gkreidl,
thanks for the reply
I did not check after a reboot so I proceeded.
I had to chuckle when the annoying file returned.
second attempt at reboot but this time I left the file as 'gtk.css' but this time it was blank no content.
looks like this is the way to go
I have rebooted several times now and 'Pango.FontDescription' works.

Re: [solved] gtk builder pango font choice not displayed

Posted: Fri Oct 27, 2017 8:04 am
by PeterO
I think the right way to do it is to create a .css file for your application, but as I said earlier I don't remember actually doing it.
PeterO

Re: [solved] gtk builder pango font choice not displayed

Posted: Fri Oct 27, 2017 6:00 pm
by lambcutlet
PeterO,
is pango obsolete? or is it just an old fashioned way of changing fonts?
and is css the new preferred method.