-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgl.py
3704 lines (3121 loc) · 105 KB
/
pgl.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File: pgl.py
"""
The pgl module implements the Portable Graphics Library (pgl) on top of
Tkinter, which is the most common graphics package for use with Python.
"""
import atexit
import inspect
import io
import math
import ssl
import sys
import time
import urllib.request
# Version information
PGL_VERSION = 0.95
PGL_BUGFIX = 0
PGL_DATE = "20-Jun-21"
# Conditional imports
try:
import tkinter # pylint: disable=import-error
try:
import tkinter.font as tk_font # pylint: disable=import-error
except Exception:
import tk_font # pylint: disable=import-error
except Exception as e:
print('Could not load tkinter: ' + str(e))
try:
from PIL import ImageTk, Image # pylint: disable=import-error
_image_model = "PIL"
except Exception:
_image_model = "PhotoImage"
spyder_flag = False
try:
import spydercustomize as customize # pylint: disable=import-error
spyder_flag = True
except Exception:
try:
import sitecustomize as customize # pylint: disable=import-error
spyder_flag = True
except Exception:
pass
if spyder_flag:
try:
sys_clear_post_mortem = customize.clear_post_mortem
def patched_clear_post_mortem():
customize.clear_post_mortem = sys_clear_post_mortem
try:
if tkinter._root is not None:
tkinter._root.mainloop()
except Exception:
pass
sys_clear_post_mortem()
customize.clear_post_mortem = patched_clear_post_mortem
except Exception:
pass
# Class GWindow
class GWindow(object):
"""
This class represents a graphics window that can contain graphical
objects.
"""
# Public constants
DEFAULT_WIDTH = 500
DEFAULT_HEIGHT = 300
MIN_WAKEUP = 20
# Constructor: GWindow
def __init__(self, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT):
"""
The constructor takes either of the following forms:
<pre>
GWindow()
GWindow(width, height)
</pre>
If the dimensions are missing, the constructor creates a
<code>GWindow</code> with a default size.
"""
try:
tk = tkinter._root
tk.deiconify()
except AttributeError:
tk = tkinter.Tk()
tkinter._root = tk
self._window_width = width
self._window_height = height
self._tk = tk
self._tk.protocol("WM_DELETE_WINDOW", self._delete_window)
for w in tk.winfo_children():
w.destroy()
self._canvas = tkinter.Canvas(tk, width=width, height=height,
highlightthickness=0)
try:
self._canvas.pack()
except:
pass
if spyder_flag:
def cancel_topmost():
tk.attributes("-topmost", False)
tk.attributes("-topmost", True)
tk.focus_force()
self._canvas.after(0, cancel_topmost)
self._canvas.update()
self._images = { }
self._timers = [ ]
self._base = GCompound()
self._base._gw = self
self._event_manager = _EventManager(self)
self.set_window_title(_get_program_name())
self._event_loop_started = False
self._active = True
if not spyder_flag:
atexit.register(self._start_event_loop)
def __eq__(self, other):
if isinstance(other, GWindow):
return self._canvas is other._canvas
return False
# Public method: close
def close(self):
"""
Deletes the window from the screen.
"""
self._delete_window()
# Public method: event_loop
def event_loop(self):
"""
Waits for events to happen in the window.
"""
self._event_loop_started = True
tkinter._root.mainloop()
# Public method: request_focus
def request_focus(self):
"""
Asks the system to assign the keyboard focus to the window, which
brings it to the top and ensures that key events are delivered to
the window.
"""
tkinter._root.canvas.focus_set()
# Public method: clear
def clear(self):
"""
Clears the contents of the window.
"""
self._base.remove_all()
# Public method: get_width
def get_width(self):
"""
Returns the width of the graphics window in pixels.
"""
return self._window_width
# Public method: get_height
def get_height(self):
"""
Returns the height of the graphics window in pixels.
"""
return self._window_height
# Public method: add_event_listener
def add_event_listener(self, type, fn):
"""
Adds an event listener of the specified type to the window.
"""
self._event_manager.add_event_listener(type, fn)
# Public method: repaint
def repaint(self):
"""
Schedule a repaint on this window.
"""
pass
# Public method: set_window_title
def set_window_title(self, title):
"""
Sets the title of the graphics window.
"""
self._window_title = title
self._tk.title(title)
# Public method: get_window_title
def get_window_title(self):
"""
Returns the title of the graphics window.
"""
return self._window_title
# Public method: add
def add(self, gobj, x=None, y=None):
"""
Adds the <code>GObject</code> to the window. The first parameter
is the object to be added. The <code>x</code> and <code>y</code>
parameters are optional. If they are supplied, the location of
the object is set to (<code>x</code>, <code>y</code>).
"""
self._base.add(gobj, x, y)
# Public method: remove
def remove(self, gobj):
"""
Removes the object from the window.
"""
self._base.remove(gobj)
# Public method: get_element_at
def get_element_at(self, x, y):
"""
Returns the topmost <code>GObject</code> containing the
point (x, y), or <code>None</code> if no such object exists.
"""
return self._base.get_element_at(x, y)
# Public method: create_timer
def create_timer(self, fn, delay):
"""
Creates a new timer object that calls fn after the specified
delay, which is measured in milliseconds. The timer must be
started by calling the <code>start</code> method.
"""
return GTimer(self, fn, delay)
# Public method: set_timeout
def set_timeout(self, fn, delay):
"""
Creates and starts a one-shot timer that calls fn after the
specified delay, which is measured in milliseconds. The
set_timeout method returns the <code>GTimer</code> object.
"""
timer = GTimer(self, fn, delay)
timer.start()
return timer
# Public method: set_interval
def set_interval(self, fn, delay):
"""
Creates and starts an interval timer that calls fn after the
specified delay, which is measured in milliseconds. The
set_interval method returns the <code>GTimer</code> object.
"""
timer = GTimer(self, fn, delay)
timer.set_repeats(True)
timer.start()
return timer
# Public method: pause
def pause(self, delay):
"""
Pauses the current thread for the specified delay, which is
measured in milliseconds. The pause method periodically checks
the event queue to update the contents of the window.
"""
n_cycles = delay // GWindow.MIN_WAKEUP
for i in range(n_cycles): # pylint: disable=unused-variable
self._tk.update_idletasks()
self._tk.update()
time.sleep(delay / n_cycles / 1000)
# Public static method: exit
@staticmethod
def exit():
"""
Closes all graphics windows and exits from the application without
waiting for any additional user interaction.
"""
sys.exit()
# Public static method: get_program_name
@staticmethod
def get_program_name():
"""
Returns the name of this program.
"""
return _get_program_name()
# Public static method: get_screen_width
@staticmethod
def get_screen_width():
"""
Returns the width of the entire display screen.
"""
return _get_screen_width()
# Public static method: get_screen_height
def get_screen_height():
"""
Returns the height of the entire display screen.
"""
return _get_screen_height()
# Public static method: convert_color_to_rgb
def convert_color_to_rgb(color_name):
"""
Converts a color name into an integer that encodes the
red, green, and blue components of the color.
"""
return _convert_color_to_rgb(color_name)
# Public static method: convert_rgb_to_color
@staticmethod
def convert_rgb_to_color(rgb):
"""
Converts an rgb value into a name in the form <code>"#rrggbb"</code>.
Each of the <code>rr</code>, <code>gg</code>, and <code>bb</code>
values are two-digit hexadecimal numbers indicating the intensity
of that component.
"""
return _convert_rgb_to_color(rgb)
# Private method: _delete_window
def _delete_window(self):
"""
Closes the window and exits from the event loop.
"""
try:
self._active = False
try:
for timer in self._timers:
timer.stop()
except:
pass
tkinter._root.destroy()
del tkinter._root
except:
pass
# Private method: _start_event_loop
def _start_event_loop(self):
"""
Starts the event loop if it wasn't run explicitly.
"""
if not self._event_loop_started:
self.event_loop()
# Private method: _rebuild
def _rebuild(self):
"""
Rebuilds the tkinter data structure for the window. This
operation is triggered if a global update is necessary.
"""
self._canvas.delete("all")
self._base._install(self, _GTransform())
# Define camel-case names
eventLoop = event_loop
requestFocus = request_focus
getWidth = get_width
getHeight = get_height
addEventListener = add_event_listener
setWindowTitle = set_window_title
getWindowTitle = get_window_title
getElementAt = get_element_at
createTimer = create_timer
setTimeout = set_timeout
setInterval = set_interval
getProgramName = get_program_name
getScreenWidth = get_screen_width
getScreenHeight = get_screen_height
convertColorToRGB = convert_color_to_rgb
convertRGBToColor = convert_rgb_to_color
# Allow British spelling
convert_colour_to_rgb = convert_color_to_rgb
convert_rgb_to_colour = convert_rgb_to_color
convertColourToRGB = convert_color_to_rgb
convertRGBToColour = convert_rgb_to_color
# Class: GObject
class GObject(object):
"""
This class is the common superclass of all graphical objects that can
be displayed on a graphical window. For examples illustrating the use
of the <code>GObject</code> class, see the descriptions of the
individual subclasses.
"""
# Constructor: GObject
def __init__(self):
"""
Creates a new <code>GObject</code>. The constructor is called
only by subclasses.
"""
self._x = 0.0
self._y = 0.0
self._sf = 1
self._angle = 0
self._color = "Black"
self._line_width = 1.0
self._visible = True
self._parent = None
self._tkid = None
self._gw = None
self._ctm_base = _GTransform()
# Public method: get_x
def get_x(self):
"""
Returns the x-coordinate of the object.
"""
return self._x
# Public method: get_y
def get_y(self):
"""
Returns the y-coordinate of the object.
"""
return self._y
# Public method: get_location
def get_location(self):
"""
Returns the location of this object as a <code>GPoint</code>.
"""
return GPoint(self._x, self._y)
# Public method: set_location
def set_location(self, x, y):
"""
Sets the location of this object to the specified point.
"""
if isinstance(x, GPoint):
x, y = x.get_x(), x.get_y()
elif isinstance(x, dict):
x, y = x.x, x.y
self._x = x
self._y = y
self._update_location()
# Public method: move
def move(self, dx, dy):
"""
Moves the object on the screen using the displacements
<code>dx</code> and <code>dy</code>.
"""
self.set_location(self._x + dx, self._y + dy)
# Public method: move_polar
def move_polar(self, r, theta):
"""
Moves the object on the screen the distance <i>r</i> in the
direction <i>theta</i>.
"""
dx = r * math.cos(math.radians(theta))
dy = -r * math.sin(math.radians(theta))
self.move(dx, dy)
# Public method: get_width
def get_width(self):
"""
Returns the width of this object, which is defined to be the width of
the bounding box.
"""
return self.get_bounds().get_width()
# Public method: get_height
def get_height(self):
"""
Returns the height of this object, which is defined to be the height
of the bounding box.
"""
return self.get_bounds().get_height()
# Public method: get_size
def get_size(self):
"""
Returns the size of the object as a <code>GDimension</code>.
"""
bounds = self.get_bounds()
return GDimension(bounds.get_width(), bounds.get_height())
# Public method: set_line_width
def set_line_width(self, line_width):
"""
Sets the width of the line used to draw this object.
"""
self._line_width = line_width
self._update_properties(width=line_width)
# Public method: get_line_width
def get_line_width(self):
"""
Returns the width of the line used to draw this object.
"""
return self._line_width
# Public method: set_color
def set_color(self, color):
"""
Sets the color used to display this object. The color parameter is
usually one of the CSS color names. The color can also be specified
as a string in the form <code>"#rrggbb"</code> where <code>rr</code>,
<code>gg</code>, and <code>bb</code> are pairs of hexadecimal digits
indicating the red, green, and blue components of the color.
"""
rgb = _convert_color_to_rgb(color)
self._color = _convert_rgb_to_color(rgb)
self._update_color()
# Public method: get_color
def get_color(self):
"""
Returns the current color as a string in the form
<code>"#rrggbb"</code>. In this string, the values <code>rr</code>,
<code>gg</code>, and <code>bb</code> are two-digit hexadecimal
values representing the red, green, and blue components.
"""
return self._color
# Public method: scale
def scale(self, sf):
"""
Scales the object by the specified scale factor.
"""
raise Exception("Not yet implemented")
# Public method: rotate
def rotate(self, theta):
"""
Transforms the object by rotating it theta degrees counterclockwise
around its origin.
"""
self._angle += theta
self._update_rotation()
# Public method: set_visible
def set_visible(self, flag):
"""
Sets whether this object is visible.
"""
self._visible = flag
self._update_visible()
# Public method: is_visible
def is_visible(self):
"""
Returns true if this object is visible.
"""
return self._visible
# Public method: send_forward
def send_forward(self):
"""
Moves this object one step toward the front in the z dimension.
If it was already at the front of the stack, nothing happens.
"""
parent = self.get_parent()
if parent is not None:
parent._send_forward(self)
# Public method: send_to_front
def send_to_front(self):
"""
Moves this object to the front of the display in the z dimension.
By moving it to the front, this object will appear to be on top of the
other graphical objects on the display and may hide any objects that
are further back.
"""
parent = self.get_parent()
if parent is not None:
parent._send_to_front(self)
# Public method: send_backward
def send_backward(self):
"""
Moves this object one step toward the back in the z dimension.
If it was already at the back of the stack, nothing happens.
"""
parent = self.get_parent()
if parent is not None:
parent._send_backward(self)
# Public method: send_to_back
def send_to_back(self):
"""
Moves this object to the back of the display in the z dimension.
By moving it to the back, this object will appear to be behind
the other graphical objects on the display and may be obscured
by other objects in front.
"""
parent = self.get_parent()
if parent is not None:
parent._send_to_back(self)
# Public method: contains
def contains(self, x, y):
"""
Returns true if the specified point is inside the object.
"""
if isinstance(x, GPoint):
x, y = x.get_x(), x.get_y()
elif isinstance(x, dict):
x, y = x.x, x.y
bounds = self.get_bounds()
if bounds is None:
return False
return bounds.contains(x, y)
# Public method: get_parent
def get_parent(self):
"""
Returns a pointer to the <code>GCompound</code> that contains this
object. Every <code>GWindow</code> is initialized to contain a
single <code>GCompound</code> that is aligned with the window.
Adding objects to the window adds them to that <code>GCompound</code>,
which means that every object you add to the window has a parent.
Calling <code>get_parent</code> on the top-level <code>GCompound</code>
returns <code>None</code>.
"""
return self._parent
# Abstract method: get_type
def get_type(self):
"""
Returns the concrete type of the object as a string, as in
"GOval" or "GRect".
"""
raise Exception("get_type is not defined in the GObject class")
# Abstract method: get_bounds
def get_bounds(self):
"""
Returns the bounding box of this object, which is defined to be the
smallest rectangle that covers everything drawn by the figure. The
coordinates of this rectangle do not necessarily match the location
returned by <code>get_location</code>. Given a <code>GLabel</code>
object, for example, <code>get_location</code> returns the
coordinates of the point on the baseline at which the string begins.
The <code>get_bounds</code> method, by contrast, returns a rectangle
that covers the entire window area occupied by the string.
"""
raise Exception("get_bounds is not defined in the GObject class")
# Protected method: _update_properties
def _update_properties(self, **options):
"""
Updates the specified properties of the object, if it is installed
in a window.
"""
gw = self._get_window()
if gw is None:
return
tkc = gw._canvas
tkc.itemconfig(self._tkid, **options)
# Protected method: _update_location
def _update_location(self):
"""
Updates the location for this object from the stored x and y
values. Some subclasses need to override this method.
"""
gw = self._get_window()
if gw is None:
return
tkc = gw._canvas
coords = tkc.coords(self._tkid)
offx = 0
offy = 0
gobj = self.get_parent()
while gobj is not None:
offx += gobj._x
offy += gobj._y
gobj = gobj.get_parent()
dx = (self._x + offx) - coords[0]
dy = (self._y + offy) - coords[1]
tkc.move(self._tkid, dx, dy)
# Protected method: _update_color
def _update_color(self):
"""
Updates the color properties. Some subclasses need to override
this method.
"""
self._update_properties(fill=self._color)
# Protected method: _update_visible
def _update_visible(self):
"""
Updates the visible property.
"""
if self._visible:
self._update_properties(state=tkinter.NORMAL)
else:
self._update_properties(state=tkinter.HIDDEN)
# Protected method: _update_rotation
def _update_rotation(self):
"""
Updates the rotation angle for this object. Subclasses that
support rotation need to override this method.
"""
raise Exception("Rotation not yet implemented for this class")
# Private method: _get_window
def _get_window(self):
"""
Returns the <code>GWindow</code> in which this <code>GObject</code>
is installed. If the object is not installed in a window, this
method returns <code>None</code>.
"""
gobj = self
while gobj._parent is not None:
gobj = gobj._parent
return gobj._gw
# Private abstract method: _install
def _install(self, target, ctm):
"""
Installs the object in the target, creating any tkinter objects
that are necessary.
"""
raise Exception("_install is not defined in the GObject class")
# Define camel-case names
getX = get_x
getY = get_y
getLocation = get_location
setLocation = set_location
movePolar = move_polar
getWidth = get_width
getHeight = get_height
getSize = get_size
setLineWidth = set_line_width
getLineWidth = get_line_width
setColor = set_color
getColor = get_color
setVisible = set_visible
isVisible = is_visible
sendForward = send_forward
sendToFront = send_to_front
sendBackward = send_backward
sendToBack = send_to_back
getParent = get_parent
# Allow British spelling
set_colour = set_color
get_colour = get_color
setColour = set_color
getColour = get_color
# Class: GFillableObject
class GFillableObject(GObject):
"""
This abstract class is the superclass of all objects that are fillable.
"""
# Constructor: GFillableObject
def __init__(self):
"""
Initializes a <code>GFillableObject</code>. Because this is an
abstract class, clients should not call this constructor explicitly.
"""
GObject.__init__(self)
self._fill_flag = False
self._fill_color = ""
# Public method: set_filled
def set_filled(self, flag):
"""
Sets the fill status for the object, where <code>False</code>
is outlined and <code>True</code> is filled.
"""
self._fill_flag = flag
self._update_color()
# Public method: is_filled
def is_filled(self):
"""
Returns <code>True</code> if the object is filled.
"""
return self._fill_flag
# Public method: set_fill_color
def set_fill_color(self, color):
"""
Sets the color used to display the filled region of the object.
"""
rgb = _convert_color_to_rgb(color)
self._fill_color = _convert_rgb_to_color(rgb)
self._update_color()
# Public method: get_fill_color
def get_fill_color(self):
"""
Returns the color used to display the filled region of this
object. If no fill color has been set, <code>get_fill_color</code>
returns the empty string.
"""
return self._fill_color
# Override method: _update_color
def _update_color(self):
"""
Updates the color properties for a <code>GFillableObject</code>.
"""
outline = self._color
if self._fill_flag:
fill = self._fill_color
if fill is None or fill == "":
fill = outline
else:
fill = ""
self._update_properties(outline=outline, fill=fill)
# Define camel-case names
setFilled = set_filled
isFilled = is_filled
setFillColor = set_fill_color
getFillColor = get_fill_color
# Allow British spelling
set_fill_colour = set_fill_color
get_fill_colour = get_fill_color
setFillColour = set_fill_color
getFillColour = get_fill_color
# Class: GRect
class GRect(GFillableObject):
"""
This class represents a graphical object whose appearance consists of
a rectangular box.
"""
# Constructor: GRect
def __init__(self, a1, a2, a3=None, a4=None):
"""
The <code>GRect</code> constructor takes either of the following
forms:
<pre>
GRect(width, height)
GRect(x, y, width, height)
</pre>
If the <code>x</code> and <code>y</code> parameters are missing,
the origin is set to (0, 0).
"""
GFillableObject.__init__(self)
if a3 is None:
x = 0
y = 0
width = a1
height = a2
else:
x = a1
y = a2
width = a3
height = a4
self._width = width
self._height = height
self.set_location(x, y)
# Public method: set_size
def set_size(self, width, height=None):
"""
Changes the size of this rectangle as specified.
"""
if isinstance(width, GDimension):
width, height = width.get_width(), width.get_height()
self._width = width
self._height = height
gw = self._get_window()
if gw is None:
return
tkc = gw._canvas
coords = tkc.coords(self._tkid)
tkc.coords(self._tkid, coords[0], coords[1],
coords[0] + width, coords[1] + height)
# Public method: set_bounds
def set_bounds(self, x, y=None, width=None, height=None):
"""
Changes the bounds of this rectangle to the specified values.
"""
if isinstance(x, GRectangle):
width, height = x.get_width(), x.get_height()
x, y = x.get_x(), x.get_y()
self.set_location(x, y)
self.set_size(width, height)
# Override method: get_bounds
def get_bounds(self):
"""
Returns the bounds of this <code>GRect</code>.
"""
ctm = self._ctm_base
lctm = _GTransform(tx=self._x + ctm._tx,
ty=self._y + ctm._ty,
rotation=self._angle + ctm._rotation,
sf=self._sf * ctm._sf)
p0 = lctm.transform(0, 0)
bb = GRectangle(p0.get_x(), p0.get_y())
bb.add(lctm.transform(self._width, 0))
bb.add(lctm.transform(0, self._height))
bb.add(lctm.transform(self._width, self._height))
return bb
# Override method: get_type
def get_type(self):
"""
Returns the type of this object.