L'Isola di Niente
L'Isola di Niente » PyGObject Tips » Gdk Tips

Gdk Tips

スクリーンショットを保存
#!/usr/bin/env python3

from gi.repository import Gdk

rootwin = Gdk.get_default_root_window()
x, y, w, h = rootwin.get_geometry()
pixbuf = Gdk.pixbuf_get_from_window(rootwin, x, y, w, h)
pixbuf.savev("screenshot.png", "png", ["compression"], ["9"])

スクリーン情報を得る
#!/usr/bin/env python3

from gi.repository import Gdk

# Create GdkScreen
screen = Gdk.Screen.get_default()

# Screen Pixel Size
x = screen.get_width()
y = screen.get_height()
print("Screen @ {0}x{1} pixel".format(x, y))

# Screen mm Size
x = screen.get_width_mm()
y = screen.get_height_mm()
print("Screen @ {0}x{1} mm".format(x, y))

# WorkArea
n = screen.get_primary_monitor()
rect = screen.get_monitor_workarea(n)
print("WorkArea: {0}, {1}, {2}, {3}".format(rect.x, rect.y, rect.width, rect.height))

# Active Window Size
gdkwin = screen.get_active_window()
w = gdkwin.get_width()
h = gdkwin.get_height()
print("Active Window @ {0}x{1} pixel".format(w, h))

XPM 画像をソースに埋め込む
XPM 画像は GIMP 等で作成できるメタデータの画像形式です。
つまり文字列、配列内を Python の list としてコピペすればソースコードに埋め込み可能。
#! /usr/bin/env python3
 
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf
 
ICON = [
"32 32 3 1",
"   c None",
".  c #000000",
"+  c #FFFFFF",
"                                ",
"   ............                 ",
"   .++++++++++.                 ",
"   .++++++++++. ......          ",
"   .++++++++++. .++++.          ",
"   .++++++++++. .++++.          ",
"   .++++...+++. .++++.          ",
"   .++++....... .++++.          ",
"   .++++.       .++++.          ",
"   .++++.       .++++.          ",
"   .++++.       .++++. ......   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. ......   ",
"   .++++.       .++++.          ",
"   .++++.       .++++. ......   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. .++++.   ",
"   .++++.       .++++. .++++.   ",
"   .++++....... .++++. .++++.   ",
"   .++++...+++. .++++. .++++.   ",
"   .++++++++++. .++++. .++++.   ",
"   .++++++++++. .++++. .++++.   ",
"   .++++++++++. .++++. .++++.   ",
"   .++++++++++. .++++. .++++.   ",
"   ............ ...... ......   ",
"                                "]
 
 
class XpmWin(Gtk.Window):
    """
        XPM Data Read
    """
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("delete-event", Gtk.main_quit)
        # Icon as XPM data
        pixbuf = GdkPixbuf.Pixbuf.new_from_xpm_data(ICON)
        image = Gtk.Image.new_from_pixbuf(pixbuf)
        # ini
        self.add(image)
        self.show_all()
 
XpmWin()
Gtk.main()
Copyright(C) sasakima-nao All rights reserved 2002 --- 2017.