Search code examples
pygtkgtk3

Dynamic data in GtkGrid


Im using Python GTK+ 3. I cant add data to GtkGrid in button signal (nothing happened when i call attach method in signal). Here is the code:

# -*- coding: utf-8 -*-
from gi.repository import Gtk, GObject, Gdk
import sqlite3

class MainWindow(Gtk.Window):

def __init__(self):
    Gtk.Window.__init__(self, title="1")
    self.fullscreen()
    MainBox = Gtk.Box()
    MainBox.set_orientation(Gtk.Orientation.VERTICAL) 
    self.add(MainBox)
    self.table = Gtk.Grid()
    self.table.set_column_spacing(10)
    MainBox.pack_start(self.table, False, True, 0)
    btn_add = Gtk.Button("ADD VALS")
    btn_add.connect("clicked", self.add_kopd)
    MainBox.pack_start(btn_add, False, True, 0)
    self.table.attach(Gtk.Label("1"), 0,0,1,1)
    self.table.attach(Gtk.Label("2"), 1,0,1,1)
    self.table.attach(Gtk.Label("3"), 2,0,1,1)

def add_kopd(self, widget):
    self.table.attach(Gtk.Label("4"), 0,1,1,1)
    self.table.attach(Gtk.Label("5"), 1,1,1,1)
    self.table.attach(Gtk.Label("6"), 2,1,1,1)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Solution

  • You need to set the new widgets to show. In GTK, nothing will be shown by default. Modify your add_kopd method to this:

    def add_kopd(self, widget):
        self.table.attach(Gtk.Label("4"), 0,1,1,1)
        self.table.attach(Gtk.Label("5"), 1,1,1,1)
        self.table.attach(Gtk.Label("6"), 2,1,1,1)
        self.show_all()
    

    Or, if you don't want to call show_all()

    def add_kopd(self, widget):
        window, attachment = self.table.attach(Gtk.Label("4"), 0,1,1,1)
        attachment.show()
        ...