How can I change the simple_table_code from below, to have the possibility of modifying the number of rows of a Gtk.Grid
(for example by clicking one of the buttons)?
I suppose I must remove grid section from _init__
, but I would like to leave the there buttons.
When I try to do it, the grid doesn't show at all. Also, Is there any more effective way to obtain cell borders beside the method I used?
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
class MyTable(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Table")
grid = Gtk.Grid(column_spacing=1,row_spacing=1)
grid.override_background_color(Gtk.StateFlags.NORMAL,Gdk.RGBA(0.0,0.0,0.0,0.5))
self.add(grid)
for i in range (5):
butt=Gtk.Button(label=i)
butt.connect("clicked",self.on_button_clicked)
grid.attach(butt,i,1,1,1)
for i in range (5):
for y in range(10):
a=Gtk.Label(label=i)
a.override_background_color(Gtk.StateFlags.NORMAL,Gdk.RGBA(0.95,0.95,0.95,1.0))
grid.attach(a,i,y+1,1,1)
win = MyTable()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
The hack that you are doing seems to be fine, I don't know what is going to do your program, but you could also consider using a Gtk.DrawingArea
.
To add the row with a button take a look to this:
from gi.repository import Gtk, Gdk
class MyTable(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Table")
main_box=Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
button_add_rows=Gtk.Button(label="Add Rows")
button_add_rows.connect("clicked", self.add_row)
main_box.add(button_add_rows)
self.grid = Gtk.Grid(column_spacing=1,row_spacing=1)
self.grid.override_background_color(Gtk.StateFlags.NORMAL,Gdk.RGBA(0.0,0.0,0.0,0.5))
main_box.add(self.grid)
self.number_of_columns=5
self.number_of_rows=10
self.add(main_box)
for column in range(self.number_of_columns):
button=Gtk.Button(label=column)
self.grid.attach(button,column,1,1,1)
for column in range(self.number_of_columns):
for row in range(self.number_of_rows):
a=Gtk.Label()
a.override_background_color(Gtk.StateFlags.NORMAL,Gdk.RGBA(0.95,0.95,0.95,1.0))
self.grid.attach(a,column, row+1,1,1)
def add_row(self, button, data=None):
for column in range(self.number_of_columns):
for row in range(self.number_of_rows+1, self.number_of_rows+2):
a=Gtk.Label()
a.override_background_color(Gtk.StateFlags.NORMAL,Gdk.RGBA(0.95,0.95,0.95,1.0))
self.grid.attach(a,column, row+1,1,1)
self.grid.show_all()
self.number_of_rows+=1
table_window = MyTable()
table_window.connect("delete-event", Gtk.main_quit)
table_window.show_all()
Gtk.main()
You can notice that I replaced some Gtk.Label(label=column)
width
Gtk.Label()
. Actually the with of the cells is already given by the first row of buttons, and you was inserting "invisible" text to the cells since the font is white and the background too. If the user uses black text the table will be filled with numbers. I think that using a Gtk.DrawingArea
would be better.
Also, keep in mind that when you create a Gtk widget, if you want to see it you need to show it. In this case I used self.grid.show_all()
on the method add_row