如何将独立的Window放置在屏幕上的确切位置? [英] How to position an independent Window in an exact location on the screen?

查看:59
本文介绍了如何将独立的Window放置在屏幕上的确切位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上正在开发一个相对复杂的GTK + 2应用程序.我的应用程序显然有一个主窗口;那么我需要打开一个新的独立"窗口.

I'm actually working on a relatively complex GTK+2 application. My application obviously have a main window; then I need to open a new "independent" Window.

我需要将飞行中"窗口放置在屏幕的* 精确位置*中,恰好位于小部件的顶点(一个DrawingArea).我需要将新窗口放置在屏幕的精确位置,恰好在小部件(gtk.DrawingArea)的顶点处.

I need to place the "flying" window in a *precise position* of the screen, exactly at the vertices of a widget (a DrawingArea). I need to place the new window in a precise position of the screen, exactly at the vertices of a widget (a gtk.DrawingArea).

所以我想到了以下算法:

So I thought the following algorithm:

  1. 我获得DrawingArea的顶点坐标(相对于父窗口);

然后,我将相对坐标转换为屏幕上的绝对坐标

Then, I convert relative coordinates to get absolute coordinates on-screen;

最后,我可以简单地将窗口移动到屏幕上的所需位置,即gtk.DrawingArea的顶点上.是吗?

Finally, I can simply move my window to desired position on screen, that is, on the vertex of gtk.DrawingArea. Is it right?

不幸的是,我无法将此算法转换为代码.p.s.我正在使用 Python 2.7 Gtk + 2.24 ;尽管如此,也欢迎使用C/C ++示例.

Unfortunately, I can not translate this algorithm into code. p.s. I'm working with Python 2.7 and Gtk+2.24; despite to this, C/C++ examples are welcome too.

推荐答案

我不确定这是否是您想要的,试试看:

I'm not sure if this is what you want, has a try:

#!/usr/bin/env python3


from gi.repository import Gtk


class Demo:

    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_title('Demo')
        self.window.set_default_size(300, 300)
        self.window.set_border_width(5)
        self.window.connect('delete-event', self.on_app_exit)

        hbox = Gtk.Box()
        hbox.set_halign(Gtk.Align.CENTER)
        hbox.set_valign(Gtk.Align.CENTER)
        self.window.add(hbox)

        da = Gtk.DrawingArea()
        da.set_size_request(100, 100)
        hbox.pack_start(da, False, False, 5)

        button = Gtk.Button('Show')
        button.connect('clicked', self.on_button_clicked, da)
        hbox.pack_start(button, False, False, 5)

        self.second_win = Gtk.Window()
        self.second_win.set_title('flying window')
        # You may also want to remove window decoration.
        #self.second_win.set_decorated(False)

        label = Gtk.Label('second window')
        self.second_win.add(label)


    def run(self):
        self.window.show_all()
        Gtk.main()

    def on_app_exit(self, widget, event=None):
        Gtk.main_quit()

    def on_button_clicked(self, button, da):
        allocation = da.get_allocation()
        self.second_win.set_default_size(allocation.width, 
                allocation.height)
        pos = self.window.get_position()
        self.second_win.move(pos[0] + allocation.x, pos[1] + allocation.y)
        self.second_win.show_all()


if __name__ == '__main__':
    demo = Demo()
    demo.run()

和屏幕截图:

这篇关于如何将独立的Window放置在屏幕上的确切位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆