11
11
12
12
class Window :
13
13
14
+ """
15
+ A Window holding a canvas.
16
+ """
17
+
14
18
_windows = []
15
19
16
20
def __init__ (self , width = 320 , height = 320 , x = None , y = None ,
17
21
fill_color = 'white' , title = 'A-Turtle' ):
22
+ """
23
+ Initialize a Window with the given `width` and `height`, filled in
24
+ `fill_color`, with the given `title`.
25
+
26
+ Positive `x` and `y` values place the Window from the screen's top-
27
+ left corner. Negative values place it from the bottom-right corner.
28
+
29
+ Windows hold a canvas object that automatically sizes up/down on
30
+ Window resizes, and keeps its (0, 0) origin at the horizontal and
31
+ vertical Window center.
32
+ """
18
33
19
34
tk_window = tkinter .Tk () if not Window ._windows else tkinter .Toplevel ()
20
35
Window ._windows .append (self )
@@ -63,13 +78,18 @@ def __init__(self, width=320, height=320, x=None, y=None,
63
78
64
79
@property
65
80
def x (self ):
66
-
81
+ """
82
+ The Window's horizontal position.
83
+ """
67
84
return self ._tk_window .winfo_x ()
68
85
69
86
70
87
@x .setter
71
88
def x (self , value ):
72
-
89
+ """
90
+ Set the Window's horizontal position.
91
+ Negative values place the Window from the right of the screen.
92
+ """
73
93
if value < 0 :
74
94
value = self ._tk_window .winfo_screenwidth () - self .width + value
75
95
@@ -78,13 +98,18 @@ def x(self, value):
78
98
79
99
@property
80
100
def y (self ):
81
-
101
+ """
102
+ The Window's vertical position.
103
+ """
82
104
return self ._tk_window .winfo_y ()
83
105
84
106
85
107
@y .setter
86
108
def y (self , value ):
87
-
109
+ """
110
+ Set the Window's vertical position.
111
+ Negative values place the Window from the bottom of the screen.
112
+ """
88
113
if value < 0 :
89
114
value = self ._tk_window .winfo_screenheight () - self .height + value
90
115
@@ -93,25 +118,33 @@ def y(self, value):
93
118
94
119
@property
95
120
def width (self ):
96
-
121
+ """
122
+ The Window width.
123
+ """
97
124
return self ._tk_window .winfo_width ()
98
125
99
126
100
127
@width .setter
101
128
def width (self , value ):
102
-
129
+ """
130
+ Set the Window width.
131
+ """
103
132
self ._tk_window .geometry (f'{ value } x{ self .height } ' )
104
133
105
134
106
135
@property
107
136
def height (self ):
108
-
137
+ """
138
+ The Window height.
139
+ """
109
140
return self ._tk_window .winfo_height ()
110
141
111
142
112
143
@height .setter
113
144
def height (self , value ):
114
-
145
+ """
146
+ Set the Window height.
147
+ """
115
148
self ._tk_window .geometry (f'{ self .width } x{ value } ' )
116
149
117
150
@@ -237,7 +270,12 @@ def unbind_direct_key(self, keysym=None):
237
270
238
271
239
272
def close (self ):
273
+ """
274
+ Closes this Window.
240
275
276
+ Raises RuntimeError if this is the first created Window and there still
277
+ are other open Windows.
278
+ """
241
279
is_root = self ._tk_window is Window ._windows [0 ]._tk_window
242
280
if is_root and len (Window ._windows ) > 1 :
243
281
raise RuntimeError ('Must be last to close.' )
@@ -251,7 +289,11 @@ def close(self):
251
289
252
290
@classmethod
253
291
def close_all (cls , strict = True ):
292
+ """
293
+ Closes all Windows.
254
294
295
+ Raises RuntimeError if `strict` is true and there are no open Windows.
296
+ """
255
297
if not cls ._windows :
256
298
if strict :
257
299
raise RuntimeError ('No windows.' )
0 commit comments