How can I create a GTK ComboBox with images in Python? - python

How can I create a ComboBox that displays a list of entries, each containing some text and an icon?
I'm using Python and GTK3 with GObject introspection.

Here's an example of how to do that, inspired by this answer for C.
from gi.repository import Gtk
from gi.repository import GdkPixbuf
store = Gtk.ListStore(str, GdkPixbuf.Pixbuf)
pb = GdkPixbuf.Pixbuf.new_from_file_at_size("picture.png", 32, 32)
store.append(["Test", pb])
combo = Gtk.ComboBox.new_with_model(store)
renderer = Gtk.CellRendererText()
combo.pack_start(renderer, True)
combo.add_attribute(renderer, "text", 0)
renderer = Gtk.CellRendererPixbuf()
combo.pack_start(renderer, False)
combo.add_attribute(renderer, "pixbuf", 1)
window = Gtk.Window()
window.add(combo)
window.show_all()
window.connect('delete-event', lambda w, e: Gtk.main_quit())
Gtk.main()

The same example in GTK2, inspired by your code:
import pygtk
pygtk.require('2.0')
import gtk
import gtk.gdk
import gobject
import gc
store = gtk.ListStore(str, gtk.gdk.Pixbuf)
pb = gtk.gdk.pixbuf_new_from_file("picture.png")
store.append(["Test", pb])
combo = gtk.ComboBox(store)
renderer = gtk.CellRendererText()
combo.pack_start(renderer, True)
combo.add_attribute(renderer, "text", 0)
renderer = gtk.CellRendererPixbuf()
combo.pack_start(renderer, False)
combo.add_attribute(renderer, "pixbuf", 1)
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.add(combo)
window.show_all()
window.connect('delete-event', lambda w, e: gtk.main_quit())
gtk.main()

Related

Different color for odd and even rows in a PyGObject TreeView [duplicate]

I'm new in python and Gtk+3 development. I'm trying to create a Gui with Gtk+3 which contains a Gtk.TreeView. I'm trying to show the odd records of the TreeView in different color but i always fail. I searched all over the internet and StackOverFlow but i didn't find something helpful.
I'm using Python 3.4.3. Can someone help?
#!/usr/bin/env python3
# -*- coding: ISO-8859-1 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
xim = [("Oranges", 5), ("Apples", 3), ("Bananas", 1), ("Tomatoes", 4), ("Cucumber", 1), ("potatoes", 10),
("apricot", 100)]
window = Gtk.Window()
window.connect("destroy", lambda q: Gtk.main_quit())
liststore = Gtk.ListStore(str, int)
for i in range(len(xim)):
liststore.append(xim[i])
treeview = Gtk.TreeView(model=liststore)
window.add(treeview)
treeviewcolumn = Gtk.TreeViewColumn("Item")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 0)
treeviewcolumn = Gtk.TreeViewColumn("Quantity")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 1)
css_provider = Gtk.CssProvider()
css = """
GtkTreeView row:nth-child(odd){background-color: #ccc}
"""
css_provider.load_from_data(css.encode())
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
window.show_all()
Gtk.main()
I know it should be better to use CSS to change the style of widgets, but there's another solution that works.
Add another column in the ListStore with the background color value and add the attribute "background" to both CellRendererText. In this way, every row will recover the background property from the ListStore. You should use a different color for each row.
This is your code modified with my technique:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
xim = [("Oranges", 5), ("Apples", 3), ("Bananas", 1), ("Tomatoes", 4), ("Cucumber", 1), ("potatoes", 10),
("apricot", 100)]
window = Gtk.Window()
window.connect("destroy", lambda q: Gtk.main_quit())
liststore = Gtk.ListStore(str, int, str)
for i in range(len(xim)):
if i % 2 == 0:
background_color = "#fff"
else:
background_color = "#bbb"
liststore.append(xim[i] + (background_color,))
treeview = Gtk.TreeView(model=liststore)
window.add(treeview)
treeviewcolumn = Gtk.TreeViewColumn("Item")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 0)
treeviewcolumn.add_attribute(cellrenderertext, "background", 2)
treeviewcolumn = Gtk.TreeViewColumn("Quantity")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 1)
treeviewcolumn.add_attribute(cellrenderertext, "background", 2)
window.show_all()
Gtk.main()

PyGi: How to use a GTKListBox with a GTKListStore?

I am very new to GTK and Gnome app development, so apologies for my naiveté. (My development language is Python). I would like to use a ListBox to display some data, and the individual row views will be quite complicated (i.e. composed of multiple different widgets). As a result I would prefer not to use a TreeView, because that will require a bunch of custom drawing/event handling. I noticed that ListBox has a bind_model method, but it appears I can't use it to bind a ListStore model, even thought ListStore implements the ListModel interface. Does anybody know how to accomplish this?
A simple exampe:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GObject
import sys
class Item(GObject.GObject):
text = GObject.property(type = str)
def __init__(self):
GObject.GObject.__init__(self)
class GUI:
def __init__(self):
item1 = Item()
item1.text = "Hello"
item2 = Item()
item2.text = "World"
liststore = Gio.ListStore()
liststore.append(item1)
liststore.append(item2)
listbox=Gtk.ListBox()
listbox.bind_model(liststore, self.create_widget_func)
window = Gtk.Window()
window.add(listbox)
window.connect("destroy", self.on_window_destroy)
window.show_all()
def create_widget_func(self,item):
label=Gtk.Label(item.text)
return label
def on_window_destroy(self, window):
Gtk.main_quit()
def main():
app = GUI()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
This is condensed code from my open source accounting program.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys
class GUI :
def __init__(self):
listbox = Gtk.ListBox()
employee_name_label = Gtk.Label("Henry", xalign=1)
combo = Gtk.ComboBoxText()
combo.set_property("can-focus", True)
for name in ["bar", "foo", "python"]:
combo.append('0', name)
list_box_row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=5)
list_box_row.add(hbox)
switch = Gtk.Switch()
switch.props.valign = Gtk.Align.CENTER
project_time_label = Gtk.Label("0:00:00", xalign=1 )
project_time_label.set_property('width-chars', 8)
hbox.pack_start(employee_name_label, True, False, 5)
hbox.pack_end(project_time_label, False, False, 5)
hbox.pack_end(switch, False, False, 5)
hbox.pack_end(combo, False, False, 5)
listbox.add(list_box_row)
window = Gtk.Window()
window.add(listbox)
window.connect("destroy", self.on_window_destroy)
window.show_all()
def on_window_destroy(self, window):
Gtk.main_quit()
def main():
app = GUI()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
It may not answer your question exactly, but it does work and it shows a way to use ListBox. ListBox is a very good choice for complicated setups. In my case I was doing so much operations every second that it crashed Treeviews.

Python 3 and Gtk+3 - issue with TreeView and alternate colors of rows

I'm new in python and Gtk+3 development. I'm trying to create a Gui with Gtk+3 which contains a Gtk.TreeView. I'm trying to show the odd records of the TreeView in different color but i always fail. I searched all over the internet and StackOverFlow but i didn't find something helpful.
I'm using Python 3.4.3. Can someone help?
#!/usr/bin/env python3
# -*- coding: ISO-8859-1 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
xim = [("Oranges", 5), ("Apples", 3), ("Bananas", 1), ("Tomatoes", 4), ("Cucumber", 1), ("potatoes", 10),
("apricot", 100)]
window = Gtk.Window()
window.connect("destroy", lambda q: Gtk.main_quit())
liststore = Gtk.ListStore(str, int)
for i in range(len(xim)):
liststore.append(xim[i])
treeview = Gtk.TreeView(model=liststore)
window.add(treeview)
treeviewcolumn = Gtk.TreeViewColumn("Item")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 0)
treeviewcolumn = Gtk.TreeViewColumn("Quantity")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 1)
css_provider = Gtk.CssProvider()
css = """
GtkTreeView row:nth-child(odd){background-color: #ccc}
"""
css_provider.load_from_data(css.encode())
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
window.show_all()
Gtk.main()
I know it should be better to use CSS to change the style of widgets, but there's another solution that works.
Add another column in the ListStore with the background color value and add the attribute "background" to both CellRendererText. In this way, every row will recover the background property from the ListStore. You should use a different color for each row.
This is your code modified with my technique:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
xim = [("Oranges", 5), ("Apples", 3), ("Bananas", 1), ("Tomatoes", 4), ("Cucumber", 1), ("potatoes", 10),
("apricot", 100)]
window = Gtk.Window()
window.connect("destroy", lambda q: Gtk.main_quit())
liststore = Gtk.ListStore(str, int, str)
for i in range(len(xim)):
if i % 2 == 0:
background_color = "#fff"
else:
background_color = "#bbb"
liststore.append(xim[i] + (background_color,))
treeview = Gtk.TreeView(model=liststore)
window.add(treeview)
treeviewcolumn = Gtk.TreeViewColumn("Item")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 0)
treeviewcolumn.add_attribute(cellrenderertext, "background", 2)
treeviewcolumn = Gtk.TreeViewColumn("Quantity")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 1)
treeviewcolumn.add_attribute(cellrenderertext, "background", 2)
window.show_all()
Gtk.main()

Autosize Python Gtk3 TreeViewColumn but make them resizable

I try to change the width of the columns. When I set them to autosize the having all a good size, but I cannot make them smaller.
Then I set the columns to fixed size but now the horizontal scrollbar no longer appears.
So maybe it is possible to start with autosize column width but also give the possibility to change the width later?
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
window.set_size_request(400, 200)
mainbox = Gtk.VBox()
window.add(mainbox)
scrolled_window = Gtk.ScrolledWindow()
mainbox.pack_start(scrolled_window, True, True, 0)
transactions_liststore = Gtk.ListStore(str, str, str, str, str, str)
for n in range(30):
transactions_liststore.append(["A", "2016-10-10", "Ulrich Farmer", "99,99 EUR", "A short Text", "A longer Text with much much more more content"])
treeview = Gtk.TreeView(Gtk.TreeModelSort(transactions_liststore))
scrolled_window.add(treeview)
for n, name in enumerate(["Type", "Date", "Name", "Value", "Info1", "Info2"]):
cell = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(name, cell, text=n)
if True:
column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
column.set_fixed_width(50)
column.set_min_width(50)
column.set_expand(True)
column.set_resizable(True)
column.set_reorderable(True)
treeview.append_column(column)
window.show_all()
Gtk.main()
edit:
found on https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeViewColumn.html#Gtk.TreeViewColumn.set_resizable
If resizable is True and sizing mode of the column is
Gtk.TreeViewColumnSizing.AUTOSIZE, then the sizing mode is changed to
Gtk.TreeViewColumnSizing.GROW_ONLY.
Try this code :
if True:
column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
#column.set_fixed_width(50)
Edit:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys
class GUI:
def __init__(self):
window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
window.set_size_request(400, 200)
mainbox = Gtk.VBox()
window.add(mainbox)
window.connect('destroy', self.on_window_destroy)
scrolled_window = Gtk.ScrolledWindow()
mainbox.pack_start(scrolled_window, True, True, 0)
transactions_liststore = Gtk.ListStore(str, str, str, str, str, str)
for n in range(30):
transactions_liststore.append(["A", "2016-10-10", "Ulrich Farmer", "99,99 EUR", "A longer Text with much much more more content", "A short Text"])
treeview = Gtk.TreeView(Gtk.TreeModelSort(transactions_liststore))
scrolled_window.add(treeview)
for n, name in enumerate(["Type", "Date", "Name", "Value", "Info1", "Info2"]):
cell = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(name, cell, text=n)
column.set_min_width(50)
column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
column.set_resizable(True)
column.set_reorderable(True)
treeview.append_column(column)
window.show_all()
def on_window_destroy(self, window):
Gtk.main_quit()
def main():
app = GUI()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
You still cannot resize the last column but it works to resize the other columns. Hope this helps.

ComboBox with two columns

I'm trying to create a GtkComboBox with two columns. I picked up some examples and I'm trying to adapt but without success. Only the items that would be the second column are shown.
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class ComboBox(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_title("ComboBox")
self.set_default_size(150, -1)
self.connect("destroy", Gtk.main_quit)
slist = Gtk.ListStore(str, str)
slist.append(['01', 'Ferro'])
slist.append(['07', 'Uranio'])
slist.append(['08', 'Cobalto'])
combobox = Gtk.ComboBox()
combobox.set_model(slist)
combobox.set_active(0)
combobox.set_wrap_width(2)
self.add(combobox)
cellrenderertext = Gtk.CellRendererText()
combobox.pack_start(cellrenderertext, True)
combobox.add_attribute(cellrenderertext, "text", 1)
window = ComboBox()
window.show_all()
Gtk.main()
I want create a GtkComboBox like this:
There must be a CellRendererText for each column to be displayed.
cell1 = Gtk.CellRendererText()
cell2 = Gtk.CellRendererText()
combobox.pack_start(cell1, True)
combobox.pack_start(cell2, True)
combobox.add_attribute(cell1, "text", 0)
combobox.add_attribute(cell2, "text", 1)

Categories

Resources