import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, Gdk
import os


def clicked(widget):
    popover = Gtk.Popover.new()
    popover.set_relative_to(widget)
    button = Gtk.Button.new_with_label("Click me again")
    button.connect('clicked', clicked_again)
    popover.add(button)
    popover.show_all()
        


def clicked_again(widget):
    popover = Gtk.Popover.new()
    popover.set_relative_to(widget)
    button = Gtk.Button.new_with_label("I'm misplaced")
    button.connect('clicked', clicked_again)
    rect = Gdk.Rectangle()
    rect.x = 0
    rect.y = 0
    rect.width = rect.height = 1
    popover.set_pointing_to(rect)
    popover.add(button)
    popover.show_all()


scrolled = Gtk.ScrolledWindow()
scrolled.show()
flowbox = Gtk.FlowBox()
flowbox.set_selection_mode(Gtk.SelectionMode.NONE)
flowbox.set_homogeneous(True)
flowbox.set_max_children_per_line(1000)
flowbox.show()
scrolled.add(flowbox)

for i in range(0, 100):
    button = Gtk.Button.new_with_label("click me")
    button.connect('clicked', clicked)
    flowbox.add(button)

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.add(scrolled)
win.resize(800, 800)
win.show_all()
Gtk.main()
