ComboBox with two columns - python

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)

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()

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.

python gtk fill ListStore in TreeView

hey I'm new and I've a problem
I'm trying to create a TreeView with a ListStore. The ListStore should get a list from another class but it seems I can't fill my ListStore.
so here's the code
TreeView and ListStore:
labelDB = gtk.Label('Database')
self.storeDB = gtk.ListStore(str, str, int, str)
rentexDB = gtk.CellRendererText()
colDB1 = gtk.TreeViewColumn('Name', rentexDB)
colDB2 = gtk.TreeViewColumn('URL', rentexDB)
colDB3 = gtk.TreeViewColumn('Bitrate', rentexDB)
colDB4 = gtk.TreeViewColumn('Format', rentexDB)
self.fillStore()
pageDB = gtk.TreeView(model=self.storeDB)
pageDB.append_column(colDB1)
pageDB.append_column(colDB2)
pageDB.append_column(colDB3)
pageDB.append_column(colDB4)
showData.append_page(pageDB, labelDB)
and the fillStore:
def fillStore(self):
dataInStore = self.record.useDB()
if dataInStore is not None:
self.storeDB.clear()
for row in dataInStore:
#print row
self.storeDB.append(row)
return self.storeDB
when I print the row's I can see the List's I want to store in my ListStore
but the ListStore is always empty
I hope someone can help me, please
below is a demo app:
#!/usr/bin/env python3
from gi.repository import Gtk
class Demo:
def __init__(self):
self.window = Gtk.Window()
self.window.set_default_size(640, 480)
self.window.connect('delete-event', self.on_app_exit)
scrolledwindow = Gtk.ScrolledWindow()
self.window.add(scrolledwindow)
self.liststore = Gtk.ListStore(str, str, int, str)
# You need to use self.fillStore() instead.
self.liststore.append(['Debian', 'i386', 0, 'ok'])
self.liststore.append(['Fedora', 'amd64', 1, 'ok'])
self.liststore.append(['Ubuntu', 'i386', 2, 'ok'])
treeview = Gtk.TreeView(model=self.liststore)
scrolledwindow.add(treeview)
names = ('Name', 'URL', 'Bitrate', 'Format')
for i in range(4):
renderer_text = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(names[i], renderer_text, text=i)
treeview.append_column(column)
def fillStore(self):
pass
def run(self):
self.window.show_all()
Gtk.main()
def on_app_exit(self, widget, event=None):
Gtk.main_quit()
if __name__ == '__main__':
demo = Demo()
demo.run()

How can I create a GTK ComboBox with images in 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()

Categories

Resources