~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to examples/scrap_clipboard.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
"""
3
3
Demonstrates the clipboard capabilities of pygame.
4
4
"""
 
5
import os
 
6
 
5
7
import pygame
6
8
from pygame.locals import *
7
9
import pygame.scrap as scrap
8
10
import StringIO
9
11
 
10
12
def usage ():
11
 
    print "Press the 'g' key to get all of the current clipboard data"
12
 
    print "Press the 'p' key to put a string into the clipboard"
13
 
    print "Press the 'a' key to get a list of the currently available types"
14
 
    print "Press the 'i' key to put an image into the clipboard"
 
13
    print ("Press the 'g' key to get all of the current clipboard data")
 
14
    print ("Press the 'p' key to put a string into the clipboard")
 
15
    print ("Press the 'a' key to get a list of the currently available types")
 
16
    print ("Press the 'i' key to put an image into the clipboard")
15
17
 
 
18
main_dir = os.path.split(os.path.abspath(__file__))[0]
16
19
 
17
20
pygame.init ()
18
21
screen = pygame.display.set_mode ((200, 200))
32
35
 
33
36
        elif e.type == KEYDOWN and e.key == K_g:
34
37
            # This means to look for data.
35
 
            print "Getting the different clipboard data.."
 
38
            print ("Getting the different clipboard data..")
36
39
            for t in scrap.get_types ():
37
40
                r = scrap.get (t)
38
41
                if r and len (r) > 500:
39
 
                    print "Type %s : (large buffer)" % t
 
42
                    print ("Type %s : (large buffer)" % t)
40
43
                else:
41
 
                    print "Type %s : %s" % (t, r)
 
44
                    print ("Type %s : %s" % (t, r))
42
45
                if "image" in t:
43
46
                    namehint = t.split("/")[1]
44
47
                    if namehint in ['bmp', 'png', 'jpg']:
49
52
 
50
53
        elif e.type == KEYDOWN and e.key == K_p:
51
54
            # Place some text into the selection.
52
 
            print "Placing clipboard text."
 
55
            print ("Placing clipboard text.")
53
56
            scrap.put (SCRAP_TEXT, "Hello. This is a message from scrap.")
54
57
 
55
58
        elif e.type == KEYDOWN and e.key == K_a:
56
59
            # Get all available types.
57
 
            print "Getting the available types from the clipboard."
 
60
            print ("Getting the available types from the clipboard.")
58
61
            types = scrap.get_types ()
59
 
            print types
 
62
            print (types)
60
63
            if len (types) > 0:
61
 
                print "Contains %s: %s" % (types[0], scrap.contains (types[0]))
62
 
                print "Contains _INVALID_: ", scrap.contains ("_INVALID_")
 
64
                print ("Contains %s: %s" % (types[0], scrap.contains (types[0])))
 
65
                print ("Contains _INVALID_: ", scrap.contains ("_INVALID_"))
63
66
 
64
67
        elif e.type == KEYDOWN and e.key == K_i:
65
 
            print "Putting image into the clipboard."
 
68
            print ("Putting image into the clipboard.")
66
69
            scrap.set_mode (SCRAP_CLIPBOARD)
67
 
            fp = open ("data/liquid.bmp", "rb")
 
70
            fp = open (os.path.join(main_dir, 'data', 'liquid.bmp'), 'rb')
68
71
            buf = fp.read ()
69
72
            scrap.put ("image/bmp", buf)
70
73
            fp.close ()