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.
Related
I'm just playing with Gtk currently, but was wondering if someone has an example code of a window that has a notebook in it, and with a click of a button or event, it opens a new tab with a Gtk (for example) entry in it and is accessible from further code.
I can't find any working code like that on the web or on this website.
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Simple Notebook Example")
self.set_border_width(3)
self.box = Gtk.VBox(spacing=6)
self.add(self.box)
self._add_tab_button = Gtk.Button(label="Add Tab")
self._add_tab_button.connect("clicked", self.add_tabs)
self.box.pack_start(self._add_tab_button, False, False, 5)
self.counter = 1
self.notebook = Gtk.Notebook()
self.box.pack_start(self.notebook, True, True, 5)
# add two tabs
self.add_tabs(None)
self.add_tabs(None)
def add_tabs(self, button):
page = Gtk.Box()
page.set_border_width(10)
page.add(Gtk.Label(label="Page %s content" % self.counter))
self.notebook.append_page(
page, Gtk.Label(label="Page %s" % self.counter))
page.show_all()
self.counter += 1
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
I'm pretty new to OOP and gtk programming, so sorry if the answer my question is really obvious, but I can't find a solution. I am trying to make a browser-like interface using the Gtk notebook. I wrote a method to add tabs, and it seems to work, becasue when I call it in the init, it works, and adds a new tab. Here the method is:
def create_page(self, button):
print("creating a new page")
print(self)
self.newpage = Gtk.Box()
self.newpage.set_border_width(50)
self.newpage.add(Gtk.Label.new("add notes here"))
self.notebook.append_page(self.newpage, Gtk.Label.new("new page"))
The reason the method has to have the button parameter is becasue I want it to be called by a button, and for that to happen, it has to have a button parameter.
When the button calls the parameter, the print statment works, and it prints its self <main.MyWindow object at 0x7efd64e52a80 (main+MyWindow at 0xe60270)>. It prints the exact same output as when I call it from the init.The problem is that it never actually adds the new notebook tab for some reason. Here my full code is:
import gi
# Since a system can have multiple versions
# of GTK + installed, we want to make
# sure that we are importing GTK + 3.
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title ="Stickies Hub")
#self.set_border_width(70)
# Create Notebook
self.notebook = Gtk.Notebook.new()
self.add(self.notebook)
#create buttons
self.new_tab = Gtk.Button(label=("button"))
self.new_tab.connect("clicked", self.create_page)
# Create pages
self.page1 = Gtk.Box()
self.page1.set_border_width(50)
self.page1.add(Gtk.Label.new("Welcome to Geeks for Geeks"))
self.notebook.append_page(self.page1, Gtk.Label.new("Click Here"))
self.page2 = Gtk.Box()
self.page2.set_border_width(50)
self.page2.add(Gtk.Label.new("A computer science portal for geeks"))
self.page2.add(self.new_tab)
self.notebook.append_page(self.page2, Gtk.Label.new("Click Here"))
self.create_page(self.new_tab)
self.create_page(self.new_tab)
def create_page(self, button):
print("creating a new page")
print(self)
self.newpage = Gtk.Box()
self.newpage.set_border_width(50)
self.newpage.add(Gtk.Label.new("new page"))
self.notebook.append_page(self.newpage, Gtk.Label.new("new page"))
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
# Display the window.
win.show_all()
# Start the GTK + processing loop
Gtk.main()
How can I add a new notebook tab from a button?
Thanks so much for help!
As jackw11111 said, the solution was to add self.show_all() at the end of create_page function. Thanks so much!
I made an answer so anyone with this same problem could easily find the answer.
Helloo, Here is my code. I think this will work for you.
from gi.repository import Gdk
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
notebook = Gtk.Notebook()
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(750, 500)
self.connect("destroy", Gtk.main_quit)
self.list_view()
def list_view(self):
self.table = Gtk.Table(n_rows=3, n_columns=3, homogeneous=True)
listbox = Gtk.ListBox()
self.add(self.table)
self.add(listbox)
self.two_d_array = {'Hello' : 'Hi', 'Example' : 'Merhaba'}
for i in self.two_d_array.keys():
## label yerine buton oluşturduk
items = Gtk.Button.new_with_label(i)
items.connect("button-press-event",self.button_clicked)
listbox.add(items)
self.table.attach(listbox,0,1,0,3)
self.add(self.notebook)
self.table.attach(self.notebook,1,3,0,3)
self.notebook.show_all()
self.page1 = Gtk.Box()
self.page1.set_border_width(10)
self.page1.add(Gtk.Label(label="Merhaba bu ilk sayfa."))
self.notebook.append_page(self.page1, Gtk.Label(label="Default Page"))
def context_menu(self):
menu = Gtk.Menu()
menu_item = Gtk.MenuItem("New Page")
menu.append(menu_item)
menu_item.connect("activate", self.on_click_popup)
menu.show_all()
return menu
## Buton sağ click ise context menu açtı
def button_clicked(self,listbox_widget,event):
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
menu = self.context_menu()
## Tıklanan objenin labelini print ediyor
print(listbox_widget.get_label())
self.labelmenu = listbox_widget.get_label()
menu.popup( None, None, None,None, event.button, event.get_time())
return True
def on_pop_menu(self, widget, event):
if event.button == 3:
widget.popup(None, None, None, None, event.button, event.time)
def on_click_popup(self, action):
## Yeni sayfa oluştur
self.new_page = Gtk.Box()
self.new_page.set_border_width(10)
self.new_page.add(Gtk.Label(label=self.two_d_array[self.labelmenu]))
self.notebook.append_page(self.new_page, Gtk.Label(label="New Page"))
self.close_button = Gtk.Button()
self.close_button.set_image(Gtk.Image(Gtk.STOCK_CLOSE,Gtk.IconSize))
self.close_button.connect('clicked')
self.close_button.show()
self.notebook.show_all()
window = MyWindow()
window.show_all()
Gtk.main()
So I want to have my small Python Gtk window have 2 switches. When one switch is ON, the other is turned OFF, and vice versa. I am not too sure how to control both switches. If anyone can lead me in the right direction, it'd be much appreciated.
#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class SwitcherWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Alt Switch Demo")
self.set_border_width(10)
hbox = Gtk.Box(spacing=10)
self.add(hbox)
switch1 = Gtk.Switch()
switch1.connect("notify::active", self.on_switch_activated)
switch1.set_active(True)
hbox.pack_start(switch1, True, True, 0)
switch2 = Gtk.Switch()
switch2.connect("notify::active", self.on_switch_activated)
switch2.set_active(False)
hbox.pack_start(switch2, True, True, 0)
if switch1.get_active():
switch2.set_active(False)
else:
switch2.set_active(True)
def on_switch_activated(self, switch, gparam):
builder = Gtk.Builder()
sw1 = builder.get_object("switch1")
sw2 = builder.get_object("switch2")
if switch.get_active():
state = "on"
sw2.set_active(False)
else:
state = "off"
print("Switch was turned", state)
win = SwitcherWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
I would bind the properties of both switches, inverted and sync'ed on creation:
#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
class SwitcherWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Alt Switch Demo")
self.set_border_width(10)
hbox = Gtk.Box(spacing=10)
self.add(hbox)
switch1 = Gtk.Switch()
switch1.set_active(True)
hbox.pack_start(switch1, True, True, 0)
switch2 = Gtk.Switch()
switch2.set_active(False)
hbox.pack_start(switch2, True, True, 0)
switch1.bind_property("active", switch2, "active", GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE | GObject.BindingFlags.INVERT_BOOLEAN)
win = SwitcherWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
The solution resides on this line of code:
switch1.bind_property("active", switch2, "active", GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE | GObject.BindingFlags.INVERT_BOOLEAN)
Here you bind the "active" property of switch1 and switch2 with the binding flags: bidirectional, sync on create and invert boolean
You can implement a similar logic to what I wrote:
#!/usr/bin/env python
class Switch:
_State = False
def __init__(self, StartingPosition=False):
self._State = StartingPosition
def SwitchON(self):
self._State = True
def SwitchOFF(self):
self._State = False
def Switch(self):
self._State = not self._State
def GetInfo(self):
print(self._State)
class Switcher:
def __init__(self, switchDependencyList=[]):
self.SwitchDependencyList = switchDependencyList
if len(self.SwitchDependencyList) == 0:
return None
if not len(self.SwitchDependencyList) % 2:
return None
def SwitchByIndex(self, Index):
for i, switch in enumerate(self.SwitchDependencyList):
if i == Index:
switch.SwitchON()
else:
switch.SwitchOFF()
def GetInfo(self):
for switch in self.SwitchDependencyList:
switch.GetInfo()
sw1 = Switch()
sw2 = Switch()
SwitcherModule = Switcher([sw1, sw2])
SwitcherModule.SwitchByIndex(1)
SwitcherModule.GetInfo()
No need for anything as complex as the prelisted answers. gtk already has a radiobutton widget that does it all for you. Only thing is that when it is initialised you have no buttons set, so you have to pick one to set.
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.
Can somebody provide a code snippet that can be used under Gtk3 to change the cursor to a hour glass using python..?
The following Window implementation does that:
import time
from gi.repository import Gdk, Gtk, GObject
class MainWindow(Gtk.Window):
"""Example window."""
def __init__(self):
"""Create new instance."""
super(MainWindow, self).__init__()
self.set_title('Test Windows')
box = Gtk.VBox()
label = Gtk.Label("Just a label....")
box.pack_start(label, True, True, 0)
button = Gtk.Button(" and a button")
box.pack_start(button, True, True, 0)
self.add(box)
self.connect("destroy", Gtk.main_quit)
self.show_all()
def set_watch(self):
"""Set the mouse to be a watch."""
watch = Gdk.Cursor(Gdk.CursorType.WATCH)
gdk_window = self.get_root_window()
gdk_window.set_cursor(watch)
def long_call(self):
"""Perform a long call."""
time.sleep(10) # your time consuming operation here
arrow = Gdk.Cursor(Gdk.CursorType.ARROW)
gdk_window = self.get_root_window()
gdk_window.set_cursor(arrow)
window = MainWindow()
window.set_watch()
GObject.idle_add(window.long_call)
Gtk.main()
It is important that you do set the pointer to be a watch OUTSIDE the idel_add call otherwise it won't be set.