lambcutlet
Posts: 14
Joined: Mon Aug 18, 2014 7:56 pm

[solved] gtk builder pango font choice not displayed

Tue Oct 24, 2017 8:14 pm

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>
Last edited by lambcutlet on Thu Oct 26, 2017 10:13 pm, edited 3 times in total.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: gtk builder pango font choice not displayed

Tue Oct 24, 2017 9:43 pm

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

lambcutlet
Posts: 14
Joined: Mon Aug 18, 2014 7:56 pm

Re: gtk builder pango font choice not displayed

Wed Oct 25, 2017 8:23 pm

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.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: gtk builder pango font choice not displayed

Wed Oct 25, 2017 9:37 pm

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

lambcutlet
Posts: 14
Joined: Mon Aug 18, 2014 7:56 pm

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

Thu Oct 26, 2017 9:43 pm

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;
}
Last edited by lambcutlet on Fri Oct 27, 2017 7:08 am, edited 1 time in total.

gkreidl
Posts: 6326
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

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

Thu Oct 26, 2017 10:54 pm

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.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

lambcutlet
Posts: 14
Joined: Mon Aug 18, 2014 7:56 pm

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

Fri Oct 27, 2017 6:54 am

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.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

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

Fri Oct 27, 2017 8:04 am

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

lambcutlet
Posts: 14
Joined: Mon Aug 18, 2014 7:56 pm

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

Fri Oct 27, 2017 6:00 pm

PeterO,
is pango obsolete? or is it just an old fashioned way of changing fonts?
and is css the new preferred method.

Return to “Python”