【问题标题】:Highlight Select Box in Python在 Python 中突出显示选择框
【发布时间】:2011-09-08 17:33:14
【问题描述】:

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

我已经工作了好几个小时试图重新创建它,但根本找不到方法。我已经尝试过 PyGTK、Xlib for python 和其他一些奇怪的 hack。所有这些都有自己的问题,无法让我继续前进。

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

以下是要求:

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

更新:忘记了一些细节。

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

【问题讨论】:

  • 你能用 pygtk 处理 onclick/release 处理程序吗?你能在屏幕上画一个盒子吗?如果是这样,您还需要什么?
  • 问题中最难的部分是在根窗口上绘制一个框 - 而不是在子窗口/小部件上。我没有使用 pygtk 在根窗口上成功绘制任何东西。

标签: python pygtk selection xlib


【解决方案1】:

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

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()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2012-12-15
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多