PyGobject(八十)Gtk.Clipboard

Gtk.Clipboard

Gtk.Clipboard剪貼板
這裏寫圖片描述

Methods

方法修飾詞 方法名及參數
static get (selection)
static get_default (display)
static get_for_display (display, selection)
clear ()
get_display ()
get_owner ()
request_contents (target, callback, *user_data)
request_image (callback, *user_data)
request_rich_text (buffer, callback, *user_data)
request_targets (callback, *user_data)
request_text (callback, *user_data)
request_uris (callback, *user_data)
set_can_store (targets)
set_image (pixbuf)
set_text (text, len)
store ()
wait_for_contents (target)
wait_for_image ()
wait_for_rich_text (buffer)
wait_for_targets ()
wait_for_text ()
wait_for_uris ()
wait_is_image_available ()
wait_is_rich_text_available (buffer)
wait_is_target_available (target)
wait_is_text_available ()
wait_is_uris_available ()

Virtual Methods

Properties

Name Type Flags Short Description

Signals

Name Short Description
owner-change The ::owner-change signal is emitted when GTK+ receives an event that indicates that the ownership of the selection associated with clipboard has changed.

例子

不知道怎麼樣才能粘貼系統中剪切的圖片。如用QQ剪切的圖片
這裏寫圖片描述
代碼:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/16
# section 128
TITLE = "Clipboard"
DESCRIPTION = """
The Gtk.Clipboard object represents a clipboard of data shared between different processes
or between different widgets in the same process. Each clipboard is identified by a name encoded
as a Gdk.Atom. (Conversion to and from strings can be done with Gdk.Atom.intern() and Gdk.Atom.name().)
The default clipboard corresponds to the “CLIPBOARD” atom; another commonly used clipboard is the “PRIMARY” clipboard,
which, in X, traditionally contains the currently selected text.
"""
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk

items = ["edit-cut", "edit-paste", "edit-copy"]


class ClipboardWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Clipboard Example")

        grid = Gtk.Grid()

        # self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        atom = Gdk.atom_intern('CLIPBOARD', True)
        self.clipboard = Gtk.Clipboard.get(atom)
        # None:  all available targets should be saved.
        self.clipboard.set_can_store(None)
        self.entry = Gtk.Entry()

        button_copy_text = Gtk.Button("Copy Text")
        button_paste_text = Gtk.Button("Paste Text")
        grid.attach(self.entry, 0, 0, 1, 1)
        grid.attach(button_copy_text, 1, 0, 1, 1)
        grid.attach(button_paste_text, 2, 0, 1, 1)
        button_copy_text.connect("clicked", self.copy_text)
        button_paste_text.connect("clicked", self.paste_text)

        for i, item in enumerate(items):
            image = Gtk.Image.new_from_icon_name(item, Gtk.IconSize.MENU)
            button_copy_image = Gtk.Button("Copy Image")
            grid.attach(image, 0, i + 1, 1, 1)
            grid.attach(button_copy_image, 1, i + 1, 1, 1)
            button_copy_image.connect("clicked", self.copy_image, image)

        self.image = Gtk.Image()
        button_paste_image = Gtk.Button("Paste Image")
        grid.attach(self.image, 0, 4, 1, 1)
        grid.attach(button_paste_image, 1, 4, 2, 1)
        button_paste_image.connect("clicked", self.paste_image)

        self.add(grid)

    def copy_text(self, widget):
        self.clipboard.set_text(self.entry.get_text(), -1)

    def paste_text(self, widget):
        selection = self.clipboard.wait_for_contents(Gdk.atom_intern('UTF8_STRING', True))
        print(selection.get_text())
        text = self.clipboard.wait_for_text()
        if text is not None:
            self.entry.set_text(text)
        else:
            print("No text on the clipboard.")

    def copy_image(self, widget, image):
        image_type = image.get_storage_type()
        if image_type == Gtk.ImageType.PIXBUF:
            self.clipboard.set_image(image.get_pixbuf())
        elif image_type == Gtk.ImageType.ICON_NAME:
            name, size = image.get_icon_name()
            pixbuf = Gtk.IconTheme.get_default().load_icon(name, size, 0)
            self.clipboard.set_image(pixbuf)
        else:
            print("No image has been pasted yet.")

        # clear the image
        self.image.set_from_pixbuf(None)

    def paste_image(self, widget):
        # print(self.clipboard.wait_for_targets())
        selection = self.clipboard.wait_for_contents(Gdk.atom_intern('image/png', True))
        pixbuf = selection.get_pixbuf()
        # pixbuf = self.clipboard.wait_for_image()

        if pixbuf is not None:
            self.image.set_from_pixbuf(pixbuf)
        else:
            print("no image")


def main():
    win = ClipboardWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()





代碼下載地址:http://download.csdn.net/detail/a87b01c14/9594728

發佈了166 篇原創文章 · 獲贊 24 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章