在Python中高亮选择框
我想要在自己的应用程序里重建桌面上“拖动选择”的功能。所谓“拖动选择”,就是在桌面上点击并拖动时出现的选择框,这个功能在所有主流操作系统中都有。
我已经花了好几个小时尝试去实现这个功能,但就是找不到合适的方法。我试过PyGTK、Python的Xlib,还有一些其他奇怪的解决办法,但它们都有各自的问题,让我无法继续下去。
我通常不会直接要求别人给我示例代码,而是会提供一些起点,但在这个项目里,我连从哪里开始都不知道。你会怎么做呢?
这是我的需求:
- 必须在根窗口上绘制(或者在一个“看起来像”根窗口的透明层上)
- 必须返回选择框的坐标(x, y, 高度, 宽度)
更新: 忘记了一些细节。
- 我正在使用Ubuntu 10.10
- 我有双显示器(不过,我觉得这应该没什么影响)
- 我不介意下载任何必要的额外库
1 个回答
0
我不知道这是不是你想要的,但你可以试试在你的模块里创建一个新窗口,然后在你松开鼠标拖动的时候显示这个窗口。你可以获取鼠标当前的位置,然后在那个地方绘制窗口。
所以,你的代码可能看起来像这样(这段代码没有经过测试!)我只展示了__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()
这是一种非常简单的方法,但应该能达到你想要的效果。