0

Considering this code:

import pygtk
import gtk

class TheBoard:
    def delete_event(self, widget, event, data=None):
        return False # confirms delete events

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self, grid=4):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Game Board")
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(10)

        self.table = gtk.Table(grid, grid, True)
        self.spots = [gtk.Label('unk')] * (grid*grid)
        for index, spot in enumerate(self.spots):
            spot.set_text("%d" % index)
            x = index / grid
            y = index % grid
            spot.show()
            self.table.attach(spot, x, x+1, y, y+1, xoptions=gtk.FILL, yoptions=gtk.FILL, xpadding=0, ypadding=0)
            print "(%d, %d)" % (x, y)
            print "Attaching %s as (%d, %d, %d, %d)" % (spot.get_text(), x, x+1, y, y+1)

        self.window.add(self.table)
        self.table.show()
        self.window.show()

    def main(self):
        gtk.main()

if __name__ == '__main__':
   gb = TheBoard()
   gb.main()

And this output:

main.py:30: GtkWarning: IA__gtk_table_attach: assertion 'child->parent == NULL' failed
    self.table.attach(spot, x, x+1, y, y+1, xoptions=gtk.FILL, yoptions=gtk.FILL, xpadding=0, ypadding=0)
(0, 0)
Attaching 0 as (0, 1, 0, 1)
(0, 1)
Attaching 1 as (0, 1, 1, 2)
(0, 2)
Attaching 2 as (0, 1, 2, 3)
(0, 3)
Attaching 3 as (0, 1, 3, 4)
(1, 0)
Attaching 4 as (1, 2, 0, 1)
(1, 1)
Attaching 5 as (1, 2, 1, 2)
(1, 2)
Attaching 6 as (1, 2, 2, 3)
(1, 3)
Attaching 7 as (1, 2, 3, 4)
(2, 0)
Attaching 8 as (2, 3, 0, 1)
(2, 1)
Attaching 9 as (2, 3, 1, 2)
(2, 2)
Attaching 10 as (2, 3, 2, 3)
(2, 3)
Attaching 11 as (2, 3, 3, 4)
(3, 0)
Attaching 12 as (3, 4, 0, 1)
(3, 1)
Attaching 13 as (3, 4, 1, 2)
(3, 2)
Attaching 14 as (3, 4, 2, 3)
(3, 3)
Attaching 15 as (3, 4, 3, 4)

And the rendered board: The Game Board

What am I doing wrong with either creating the gtk.Label or gtk.Table objects? I think I am messing up the gtk.Table.attach() function. Maybe I'm a bit dense at the moment, but I was having some difficulty figuring out the proper arguments for the function call. I modeled my code to look like this tutorial, but my code doesn't work. It runs, but shows a warning, and it looks like the last gtk.Label overwrites the priors.

I've looked at a lot of articles, but all the gtk.Table tutorials show code explicitly lists each item which is attached, instead of having something dynamic, which is what I'm trying to do. I want to create the items in a loop, so the board can be of any size (making it a square to make it easy).

Any thoughts? Where did I make my mistake?

Richard Żak
  • 814
  • 1
  • 11
  • 21
  • 1
    Don't use pygtk and Gtk2. It will be obsolete in a few more years. [Here](http://python-gtk-3-tutorial.readthedocs.io/en/latest/layout.html#id1) is a Gtk3 tutorial using GtkGrid. – theGtknerd Feb 27 '18 at 12:01
  • `[gtk.Label('unk')] * (grid*grid)` creates a list that contains one and the same Label instance multiple times. Then gtk complains because you're trying to add the same label in multiple places in the GUI. – Aran-Fey Feb 28 '18 at 05:24
  • Possible duplicate of [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Aran-Fey Feb 28 '18 at 05:24

0 Answers0