File: ClickableBoxes.py

package info (click to toggle)
wxpython4.0 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 232,540 kB
  • sloc: cpp: 958,937; python: 233,059; ansic: 150,441; makefile: 51,662; sh: 8,687; perl: 1,563; javascript: 584; php: 326; xml: 200
file content (119 lines) | stat: -rw-r--r-- 3,070 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python

"""
This is a little demo of how to make clickable (and changeable) objects with
FloatCanvas

Also an example of constant size, rather than the usual zooming and panning

Developed as an answer to a question on the wxPYhton mailing lilst:
   "get panel id while dragging across several panels'
    April 5, 2012

"""

import random
import wx

## import the installed version
from wx.lib.floatcanvas import NavCanvas, FloatCanvas

## import a local version
#import sys
#sys.path.append("../")
#from floatcanvas import FloatCanvas as FC


colors = [ (255, 0  , 0  ),
           (0  , 255, 0  ),
           (0  , 0,   255),
           (255, 255, 0  ),
           (255,   0, 255),
           (0  , 255, 255),
           ]


class DrawFrame(wx.Frame):

    """
    A frame used for the  Demo

    """

    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        # Add the Canvas
        Canvas = FloatCanvas.FloatCanvas(self,
                                         size = (500,500),
                                         ProjectionFun = None,
                                         Debug = 0,
                                         BackgroundColor = "Black",
                                         )

        self.Canvas = Canvas

        self.Canvas.Bind(wx.EVT_SIZE, self.OnSize)

        # build the squares:
        w = 10
        dx = 14
        for i in range(9):
            for j in range(9):
                Rect = Canvas.AddRectangle((i*dx, j*dx), (w, w), FillColor="White", LineStyle = None)
                Outline = Canvas.AddRectangle((i*dx, j*dx), (w, w),
                                              FillColor=None,
                                              LineWidth=4,
                                              LineColor='Red',
                                              LineStyle=None)
                Rect.indexes = (i,j)
                Rect.outline = Outline
                Rect.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SquareHitLeft)
                Rect.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.SquareEnter)
                Rect.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.SquareLeave)

        self.Show()
        Canvas.ZoomToBB()

    def SquareHitLeft(self, square):
        print("square hit:", square.indexes)
        # set a random color
        c = random.sample(colors, 1)[0]
        square.SetFillColor( c )
        self.Canvas.Draw(True)

    def SquareEnter(self, square):
        print("entering square:", square.indexes)
        square.outline.SetLineStyle("Solid")
        self.Canvas.Draw(True)

    def SquareLeave(self, square):
        print("leaving square:", square.indexes)
        square.outline.SetLineStyle(None)
        self.Canvas.Draw(True)


    def OnSize(self, event):
        """
        re-zooms the canvas to fit the window

        """
        print("in OnSize")
        self.Canvas.ZoomToBB()
        event.Skip()

app = wx.App(False)
F = DrawFrame(None, title="FloatCanvas Demo App", size=(700,700) )
app.MainLoop()