在Python中突出显示选择框

时间:2011-09-08 17:33:15

标签: python pygtk selection xlib

我正在尝试重建桌面“突出显示以选择”功能的功能,以便我可以在自己的应用中使用它。当我说“突出显示以选择”时,我指的是在桌面上单击并拖动时显示的选择框(原生于所有主流操作系统)。

我已经工作了几个小时试图重新创建它,根本找不到方法。我已经尝试过PyGTK,Xlib用于python,以及其他一些奇怪的黑客攻击。所有这些都有自己的问题,不允许我继续前进。

我通常不会提出直接的示例代码而不提供某种起点,但在这个项目中我甚至不知道从哪里开始。你会怎么做?

以下是要求:

  • 必须在根窗口上绘制(或“显示”为根的透明层)
  • 必须返回选择的坐标(x,y,高度宽度)

更新:忘记了一些细节。

  • 我正在使用Ubuntu 10.10
  • 我有双显示器(不过,我认为这不重要)
  • 我不介意下载任何必要的额外库

1 个答案:

答案 0 :(得分:0)

我不知道这是否是您正在寻找的,但如果您在模块中创建了另一个窗口,并在释放拖动时让代码显示该怎么办?你可以获取光标的当前位置,并让它在那里绘制窗口。

This should help you get the mouse position on the root window.

所以,你的代码可能看起来有点像这样(这是未经测试的代码!)我只是展示了__ init __内部的相关部分。

def __init__(self):

    ...
    #Some of your code here.
    ...

    win = gtk.Window(gtk.WINDOW_TOPLEVEL)

    #Note that I am creating a popup window separately.
    popwin = gtk.Window(gtk.WINDOW_POPUP)

    #I am setting "decorated" to False, so it will have no titlebar or window controls.
    #Be sure to compensate for this by having another means of closing it.
    popwin.set_decorated(False)

    def ShowPopup():
        #You may need to put additional arguments in above if this is to be an event. 
        #For sake of example, I'm leaving this open ended.

        #Get the cursor position.            
        rootwin = widget.get_screen().get_root_window()
        curx, cury, mods = rootwin.get_pointer()

        #Set the popup window position.
        popwin.move(curx, cury)
        popwin.show()

    def HidePopup():
        #This is just an example for how to hide the popup when you're done with it.
        popwin.hide()

    ...
    #More of your code here.
    ...

    #Of course, here is the code showing your program's main window automatically.
    win.show()

一种非常简单的方法,但它应该给出你想要的外观。