#!/usr/bin/env python # example layout.py import gtk import whrandom class LayoutExample: def OnWindowDeleteEvent(self, widget, event): # return false so that window will be destroyed return gtk.FALSE def OnWindowDestroy(self, widget, *data): # exit main loop gtk.mainquit() def OnButtonClicked(self, button): # move the button # Note: it probably isn't necessary to freeze the layout widget here, # but better safe than sorry self.layout.freeze() self.layout.move(button, whrandom.randint(0,500), whrandom.randint(0,500)) self.layout.thaw() def __init__(self): # create the top level window window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL) window.set_title("Layout Example") window.set_default_size(300, 300) window.connect("delete-event", self.OnWindowDeleteEvent) window.connect("destroy", self.OnWindowDestroy) # create the table and pack into the window table = gtk.GtkTable(2, 2, gtk.FALSE) window.add(table) # create the layout widget and pack into the table self.layout = gtk.GtkLayout(None, None) self.layout.set_size(600, 600) table.attach(self.layout, 0, 1, 0, 1, gtk.FILL|gtk.EXPAND, gtk.FILL|gtk.EXPAND, 0, 0) # create the scrollbars and pack into the table vScrollbar = gtk.GtkVScrollbar(None) table.attach(vScrollbar, 1, 2, 0, 1, gtk.FILL|gtk.SHRINK, gtk.FILL|gtk.SHRINK, 0, 0) hScrollbar = gtk.GtkHScrollbar(None) table.attach(hScrollbar, 0, 1, 1, 2, gtk.FILL|gtk.SHRINK, gtk.FILL|gtk.SHRINK, 0, 0) # tell the scrollbars to use the layout widget's adjustments vAdjust = self.layout.get_vadjustment() vScrollbar.set_adjustment(vAdjust) hAdjust = self.layout.get_hadjustment() hScrollbar.set_adjustment(hAdjust) # create 3 buttons and put them into the layout widget button = gtk.GtkButton("Press Me") button.connect("clicked", self.OnButtonClicked) self.layout.put(button, 0, 0) button = gtk.GtkButton("Press Me") button.connect("clicked", self.OnButtonClicked) self.layout.put(button, 100, 0) button = gtk.GtkButton("Press Me") button.connect("clicked", self.OnButtonClicked) self.layout.put(button, 200, 0) # show all the widgets window.show_all() def main(): # enter the main loop gtk.mainloop() return 0 if __name__ == "__main__": LayoutExample() main()