2727__version__ = "1.3.3"
2828
2929## -- 重写turtle模块中的函数,在turtle模块源码的基础上加以修改 --
30+ # 由于turtle模块极少更新,修改的函数几乎不会在新版Python中不兼容
3031images = {} # 用于创建图像的引用
3132def _image (self ,filename ):
3233 img = Image .open (filename )
@@ -90,44 +91,117 @@ def register_shape(self, name, shape=None):
9091 self ._shapes [name ] = shape
9192
9293def _drawturtle (self ):
93- """Manages the correct rendering of the turtle with respect to
94- its shape, resizemode, stretch and tilt etc."""
95- screen = self .screen
96- shape = screen ._shapes [self .turtle .shapeIndex ]
97- ttype = shape ._type
98- titem = self .turtle ._item
99- if self ._shown and screen ._updatecounter == 0 and screen ._tracing > 0 :
100- self ._hidden_from_screen = False
101- tshape = shape ._data
102- if ttype == "polygon" :
103- if self ._resizemode == "noresize" : w = 1
104- elif self ._resizemode == "auto" : w = self ._pensize
105- else : w = self ._outlinewidth
106- shape = self ._polytrafo (self ._getshapepoly (tshape ))
107- fc , oc = self ._fillcolor , self ._pencolor
108- screen ._drawpoly (titem , shape , fill = fc , outline = oc ,
109- width = w , top = True )
110- elif ttype == "image" :
111- screen ._drawimage (titem , self ._position , tshape ,
112- self .heading (),self ._stretchfactor [0 ])
113- elif ttype == "compound" :
114- for item , (poly , fc , oc ) in zip (titem , tshape ):
115- poly = self ._polytrafo (self ._getshapepoly (poly , True ))
116- screen ._drawpoly (item , poly , fill = self ._cc (fc ),
117- outline = self ._cc (oc ), width = self ._outlinewidth , top = True )
118- else :
119- if self ._hidden_from_screen :
120- return
121- if ttype == "polygon" :
122- screen ._drawpoly (titem , ((0 , 0 ), (0 , 0 ), (0 , 0 )), "" , "" )
123- elif ttype == "image" :
124- screen ._drawimage (titem , self ._position ,
125- screen ._shapes ["blank" ]._data )
126- if titem in images :del images [titem ] # 如果已隐藏,则释放图像引用
127- elif ttype == "compound" :
128- for item in titem :
129- screen ._drawpoly (item , ((0 , 0 ), (0 , 0 ), (0 , 0 )), "" , "" )
130- self ._hidden_from_screen = True
94+ """Manages the correct rendering of the turtle with respect to
95+ its shape, resizemode, stretch and tilt etc."""
96+ screen = self .screen
97+ shape = screen ._shapes [self .turtle .shapeIndex ]
98+ ttype = shape ._type
99+ titem = self .turtle ._item
100+ if self ._shown and screen ._updatecounter == 0 and screen ._tracing > 0 :
101+ self ._hidden_from_screen = False
102+ tshape = shape ._data
103+ if ttype == "polygon" :
104+ if self ._resizemode == "noresize" : w = 1
105+ elif self ._resizemode == "auto" : w = self ._pensize
106+ else : w = self ._outlinewidth
107+ shape = self ._polytrafo (self ._getshapepoly (tshape ))
108+ fc , oc = self ._fillcolor , self ._pencolor
109+ screen ._drawpoly (titem , shape , fill = fc , outline = oc ,
110+ width = w , top = True )
111+ elif ttype == "image" :
112+ screen ._drawimage (titem , self ._position , tshape ,
113+ self .heading (),self ._stretchfactor [0 ])
114+ elif ttype == "compound" :
115+ for item , (poly , fc , oc ) in zip (titem , tshape ):
116+ poly = self ._polytrafo (self ._getshapepoly (poly , True ))
117+ screen ._drawpoly (item , poly , fill = self ._cc (fc ),
118+ outline = self ._cc (oc ), width = self ._outlinewidth , top = True )
119+ else :
120+ if self ._hidden_from_screen :
121+ return
122+ if ttype == "polygon" :
123+ screen ._drawpoly (titem , ((0 , 0 ), (0 , 0 ), (0 , 0 )), "" , "" )
124+ elif ttype == "image" :
125+ screen ._drawimage (titem , self ._position ,
126+ screen ._shapes ["blank" ]._data )
127+ if titem in images :del images [titem ] # 如果已隐藏,则释放图像引用
128+ elif ttype == "compound" :
129+ for item in titem :
130+ screen ._drawpoly (item , ((0 , 0 ), (0 , 0 ), (0 , 0 )), "" , "" )
131+ self ._hidden_from_screen = True
132+
133+ def _drawline (self , lineitem , coordlist = None ,
134+ fill = None , width = None , top = False ):
135+ """Configure lineitem according to provided arguments:
136+ coordlist is sequence of coordinates
137+ fill is drawing color
138+ width is width of drawn line.
139+ top is a boolean value, which specifies if polyitem
140+ will be put on top of the canvas' displaylist so it
141+ will not be covered by other items.
142+ """
143+ if coordlist is not None :
144+ cl = (value for coord in coordlist for value in
145+ (coord [0 ] * self .xscale , - coord [1 ] * self .yscale )) # 迭代器
146+ self .cv .coords (lineitem , * cl )
147+ if fill is not None :
148+ self .cv .itemconfigure (lineitem , fill = fill )
149+ if width is not None :
150+ self .cv .itemconfigure (lineitem , width = width )
151+ if top :
152+ self .cv .tag_raise (lineitem )
153+
154+ def _goto (self , end ): # 优化绘制较长天体轨道的性能
155+ """Move the pen to the point end, thereby drawing a line
156+ if pen is down. All other methods for turtle movement depend
157+ on this one.
158+ """
159+ ## Version with undo-stuff
160+ go_modes = ( self ._drawing ,
161+ self ._pencolor ,
162+ self ._pensize ,
163+ isinstance (self ._fillpath , list ))
164+ screen = self .screen
165+ undo_entry = ("go" , self ._position , end , go_modes ,
166+ (self .currentLineItem ,
167+ self .currentLine [:],
168+ screen ._pointlist (self .currentLineItem ),
169+ self .items [:])
170+ )
171+ if self .undobuffer :
172+ self .undobuffer .push (undo_entry )
173+ start = self ._position
174+ if self ._speed and screen ._tracing == 1 :
175+ diff = (end - start )
176+ diffsq = (diff [0 ]* screen .xscale )** 2 + (diff [1 ]* screen .yscale )** 2
177+ nhops = 1 + int ((diffsq ** 0.5 )/ (3 * (1.1 ** self ._speed )* self ._speed ))
178+ delta = diff * (1.0 / nhops )
179+ for n in range (1 , nhops ):
180+ if n == 1 :
181+ top = True
182+ else :
183+ top = False
184+ self ._position = start + delta * n
185+ if self ._drawing :
186+ screen ._drawline (self .drawingLineItem ,
187+ (start , self ._position ),
188+ self ._pencolor , self ._pensize , top )
189+ self ._update ()
190+ if self ._drawing :
191+ screen ._drawline (self .drawingLineItem , ((0 , 0 ), (0 , 0 )),
192+ fill = "" , width = self ._pensize )
193+ # Turtle now at end,
194+ if self ._drawing : # now update currentLine
195+ self .currentLine .append (end )
196+ if isinstance (self ._fillpath , list ):
197+ self ._fillpath .append (end )
198+ ###### vererbung!!!!!!!!!!!!!!!!!!!!!!
199+ self ._position = end
200+ if self ._creatingPoly :
201+ self ._poly .append (end )
202+ if len (self .currentLine ) > 320 : # tkinter.Canvas上一条线的最大长度,turtle中原本为42
203+ self ._newLine ()
204+ self ._update () #count=True)
131205
132206if Image : # 若导入PIL模块成功
133207 # 用重写的函数替换turtle模块中原来的函数
@@ -137,6 +211,10 @@ def _drawturtle(self):
137211 turtle .TurtleScreen .register_shape = register_shape
138212 turtle .RawTurtle ._drawturtle = _drawturtle
139213
214+ # 不依赖PIL
215+ turtle .TurtleScreenBase ._drawline = _drawline
216+ turtle .RawTurtle ._goto = _goto
217+
140218# ---------------------重写结束-------------------------
141219
142220class GravSys (solar_system .GravSys ):
0 commit comments