Deepin Linux Desktop, 截图工具剖析(三) python - namedtuple


Deepin的截图工具,程序都入口是screenshot.py,但是整个程序都主体窗口还是我们的main.py,class DeepinScreenshot(object),是整个程序都主窗口。

本人私自给这个文件作了如下修改,至于解析参数都那一块,就暂时不做分析啦,留做以后都小菜。

我给这个类定义了一个函数main,

    def main(self):
        print "This is the whole window"
        gtk.main()
    
if __name__ == "__main__":
    t = DeepinScreenshot()
    print "In this API"
    t.main()
    #MainScreenshot() #垃圾代码
或者直接如下
if __name__ == "__main__":
    DeepinScreenshot()


本节都重点还是class DeepinScreenshot(object) 中__init__ 函数中的一个子函数,getScreenshotWindowInfo(), 话说,这个小程序用到都知识真不少,不过大家如果细心学习,通过这一个程序,我们就能对Python掌握很多都零碎知识,多分析几个程序就可以写我们自己都程序啦。回到正题

getScreenshotWindowInfo()函数在window.py中:

from  collections import namedtuple

...
...

def getScreenshotWindowInfo():
    ''' return (x, y, width, height) '''
    coordInfo = namedtuple('coord', 'x y width height')
    screenshotWindowInfo = []
    screenshotWindowInfo.append(coordInfo(0, 0, screenWidth, screenHeight))
    for eachWindow in filterWindow():
        (x, y, width, height) = getWindowCoord(eachWindow)
        screenshotWindowInfo.append(coordInfo(*convertCoord(x, y, width, height)))
    return screenshotWindowInfo

if __name__ == "__main__":

    print getScreenshotWindowInfo()

执行结果如下:(源码会在下节给出,毕竟是从人家那里扣扣减减的,还有很多module需要分析,学习)

Xlib.protocol.request.QueryExtension
[coord(x=0, y=0, width=1280, height=800), coord(x=65, y=-2, width=1215, height=802), coord(x=340, y=215, width=722, height=460), coord(x=65, y=-2, width=1215, height=802), coord(x=0, y=0, width=-100, height=-100), coord(x=0, y=0, width=-100, height=-100), coord(x=0, y=0, width=-100, height=-100), coord(x=0, y=0, width=-100, height=-100), coord(x=0, y=-26, width=1280, height=50), coord(x=0, y=-2, width=65, height=802)]
Script terminated.

import collections

location = []
coord = collections.namedtuple('coordination', 'x, y, height, width')
print 'Type of coord:', type(coord)

coord1 = coord(x=1, y=1, height=249, width=249)
print coord1
coord2 = coord(x=2, y=2, height=349, width=349)
print coord2
coord3 = coord(0, 8, 9, 5) #测试不加参数会不会正确,至少证明整形没错啊
print coord3
for coor in [coord1, coord2, coord3]:
    print 'x=%d, y=%d, height=%d, width=%d' % coor

运行结果如下:

Type of coord:
coordination(x=1, y=1, height=249, width=249)
coordination(x=2, y=2, height=349, width=349)
coordination(x=0, y=8, height=9, width=5)
x=1, y=1, height=249, width=249
x=2, y=2, height=349, width=349
x=0, y=8, height=9, width=5

评论关闭