[solved] gtk builder pango font choice not displayed
Posted: 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'
Configured using Glade and saved as 'window.glade'
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()
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>