Reputation: 1642
I have a GTK widget for selecting my printers and show them in a ComboBox widget.
How can I make the default printer entry bold or with red background?
I am not sure how to do this or whether it is possible at all.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
import os
import sys
import subprocess
class SystemPrinter(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Window Name")
box = Gtk.HBox()
self.add(box)
printers, default_printer = self.get_system_printers()
default_printer_entry = 0
printers_store = Gtk.ListStore(str, int)
for n, item in enumerate(printers.items()):
printers_store.append(item)
if item[0] == default_printer:
default_printer_entry = n
printers_combo = Gtk.ComboBox.new_with_model_and_entry(printers_store)
printers_combo.set_entry_text_column(0)
printers_combo.set_active(default_printer_entry)
box.pack_start(Gtk.Label("Printer", True, True, 0), False, False, 0)
box.pack_start(printers_combo, False, False, 0)
self.show_all()
def get_system_printers(self):
printers = {}
default_printer = ""
printers_raw = subprocess.check_output("lpstat -p -d", shell=True)
n = 0
for printer in printers_raw.split("\n"):
if "printer" in printer.split(" ")[0]:
printers[printer.split(" ")[1]] = n
n += 1
elif "system" in printer.split(" ")[0]:
default_printer = printer.split(" ")[3]
return printers, default_printer
def main(self):
Gtk.main()
if __name__ == '__main__':
s = SystemPrinter()
s.main()
edit: I want to highlight the an default entry of a combobox before I select it!
Upvotes: 1
Views: 1225
Reputation: 45
If you want to make text appear bold, you could try, for example, Gtk.Label.set_markup(<b>"this text will be bold</b>)
.
Upvotes: 1
Reputation: 5440
First off, changing background colors (or fonts for that matter) is not encouraged in modern Gtk versions, and most methods are either deprecated or don't work consistently. The idea is that anything 'appearance'-related should be in CSS definitions.
You can still change the font of widgets by calling the modify_font
method of the ComboBox
's entry widget. These are the steps involved:
Realize that a ComboBox
is a container widget which contains the GtkEntry you see on the screen. So 'find' the entry by calling get_child()
(which is an inherited method) on the ComboBox
.
Then call modify_font
on the entry widget. You need to provide the new font as created by eg. Pango's font_description()
.
You might be tempted to use the override_background()
method to change the color, but that doesn't seem to work reliably (and is deprecated).
Upvotes: 2