create a gtk.window under a gtk.widget
I wanna show a gtk.Window under a gtk.widge开发者_JAVA百科t.
But I don't know how to retrieve the gtk.widget's coordinates for my gtk.window.
Anyone knows ?
Thanks.
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()
精彩评论