tags:

views:

293

answers:

1

Hi all,

I wanna show a gtk.Window under a gtk.widget.

But I don't know how to retrieve the gtk.widget's coordinates for my gtk.window.

Anyone knows ?

Thanks.

A: 

You can use the "window" attribute of the gtk.Widget to get the gtk.gdk.Window associated with it. Then look at the get_origin() method to get the screen coordinates.

These coordinates are for the top-level window, I believe (I could be wrong about that, but my code below seems to support that). You can use the get_allocation() method to get the coordinates of a widget relative to its parent.

I got the idea from here. Be warned though: some window managers ignore any initial settings for window position. You might want to look at this post for more info, but I haven't personally checked it out.

Were you intending to create another top-level window? Or a popup window?

#!/usr/bin/env python
import sys

import pygtk
import gtk

class Base:
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.connect("destroy", self.on_destroy)

        self.box = gtk.VButtonBox()

        self.buttons = [
            gtk.Button("Test 1"),
            gtk.Button("Test 2"),
            gtk.Button("Test 3")
        ]

        for button in self.buttons:
            self.box.add(button)
            button.connect("clicked", self.show_coords)
            button.show()

        self.window.add(self.box)

        self.box.show()
        self.window.show()

    def show_coords(self, widget, data=None):
        print "Window coords:"
        print self.window.get_window().get_origin()
        print "Button coords:"
        print widget.get_allocation()

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

    def main(self):
        gtk.main()

if __name__ == "__main__":
    base = Base()
    base.main()
detly
thanks, but i doesn't work.In fact, i want to place the gtk.window, under a gnomeapplet.Applet", but the "gnomeapplet.Applet" doesn't have the get_window() method. So i don't know how to do.
darius
Hmm. I've really only worked with the more common GTK controls, so I'm not sure about the inner workings of applets. Is there a particular application or applet that illustrates this behaviour already?
detly
yes, it's the clock applet : http://gnome-panel.sourcearchive.com/documentation/2.30.0/files.html I tried like the in the calendar-window.c to use the set_type_hint() with the gtk.gdk.WINDOW_TYPE_HINT_DOCK constant. But a part of my window is hidden by the panel.
darius
If it is a gtk.Widget subclass it will have a get_window() method. So perhaps what you have is not a gtk.Widget. If it's an AppletInfo object, you should have a get_position method. If your set_type_hint is not working too, you can perhaps use modify the gravity.
Ali A