我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用turtle.clear()。
def draw_canvas(state): '''Draw all the cities in the current state in a canvas. Indicate the start city with a description and the current city by the turtle pointer head ''' turtle.clear() turtle.hideturtle() turtle.up() turtle.pencolor("blue") current_city = state.current_city for city in state.cities: x = city.position[0] y = city.position[1] turtle.goto(x, y) if city.is_start: turtle.write('{}, Start'.format(city.name), align="center", font=("Arial", 12, "bold")) elif city == current_city: turtle.write('{}, Current'.format(city.name), align="center", font=("Arial", 12, "bold")) else: turtle.write('{}'.format(city.name), align="center", font=("Arial", 12, "bold")) turtle.goto(current_city.position[0], current_city.position[1])
def clear(self, lines=()): """Queue clear command""" self.enqueue(lambda: self.do_clear(lines))
def do_clear(self, lines=()): """Clear plot and draw grid lines""" turtle.clear() self.tables = [] self.lines = lines self.draw_grid()
def redraw(self): """Clear plot and draw grid lines""" turtle.clear() self.setworldcoordinates() self.draw_grid() for tables, kwargs in self.tables: self.plot_table(*tables, draw_speed=1024, **kwargs)
def run(self): """Test Plot class client thread""" p = self.plot try: p.plot([512 for i in range(256)], draw_speed=4) p.clear(lines= [{'pos': i, 'label': str(i)} for i in list(range(16, 256-15, 16)) + [255]] + [{'pos': i, 'horizontal': True, 'label': str(i)} for i in range(64, 1024-63, 64)]) p.plot([512 for i in range(256)], draw_speed=4) p.clear(lines=[{'pos': 128, 'label': 'Green line\nlabel', 'color': 'green'}, {'pos': 136, 'label': 'Blue\nline\nlabel', 'priority': 1, 'color': 'blue'}]) p.plot([512 for i in range(256)], draw_speed=4) p.zoom(4, (0, -1)) p.clear(lines=[{'pos': 128, 'label': 'Green line\nlabel', 'color': 'green'}, {'pos': 136, 'label': 'Blue\nline\nlabel', 'color': 'blue'}]) p.plot([512 for i in range(256)], draw_speed=4) p.clear() p.zoom() p.plot([512 for i in range(256)], draw_speed=4) p.clear(lines=[{'pos': i} for i in range(256)]) p.plot([512 for i in range(256)], draw_speed=2) p.clear(lines=[{'pos': 127}, {'pos':128}]) p.plot([i << 2 | i >> 6 for i in range(256)]) p.plot([i << 2 | i >> 6 for i in range(255, -1, -1)], colors=['red']) p.zoom(4, (0, 0)) p.plot([i << 2 | i >> 6 for i in range(256)], draw_speed=2, colors=['green']) p.zoom(2, (-1, 0)) p.plot([i << 2 | i >> 6 for i in range(256)], draw_speed=2, colors=['green']) p.zoom() p.enqueue(lambda: turtle.setpos(100, 512)) p.enqueue(lambda: turtle.write('test done')) print('test done') except PlotClosed as err: print(err)
def draw_final_path(state): '''Draw the TSP path given the cities in the correct order''' if not state: return None states = [] while state: states.append(state) state = state.parent cities = [state.current_city for state in states] rest = deepcopy(cities) rest = rest[1:] rest.reverse() cities = [cities[0]] + rest turtle.clear() turtle.hideturtle() turtle.up() turtle.pensize(1) for city in cities: x = city.position[0] y = city.position[1] turtle.pencolor("red") turtle.goto(x, y) turtle.pencolor("black") if city.is_start: turtle.write('{}-Start'.format(city.name), align="center", font=("Arial", 11, "bold")) else: turtle.write('{}'.format(city.name), align="center", font=("Arial", 11, "bold")) turtle.down() turtle.pencolor("red") turtle.goto(cities[0].position[0], cities[0].position[1])
def tscheme_clear(): """Clear the drawing, leaving the turtle unchanged.""" _tscheme_prep() turtle.clear() return okay
def draw_floor(floornum: int): global current_floor current_floor = floornum node_coords = utils.get_node_coords(utils.Direction.left) if floornum == len(full_floors): rooms = [] # All edges with rooms print('Showing everything') edges = chain.from_iterable(chain(((True, e) for e in edges[0]), ((False, e) for e in edges[1])) for _, edges in full_floors) # Edges with elevation changes #edges = set(chain.from_iterable((edge(v, n) for n in b.values() if set(['u', 'd']) & set(utils.get_graph().edgedata[edge(v, n)]['rooms'])) for v, b in enumerate(utils.get_graph().branches))) # All edges #... elif floornum >= 0 and floornum < len(full_floors): rooms, edges = full_floors[floornum] print(edges) edges = chain(((True, e) for e in edges[0]), ((False, e) for e in edges[1])) print(rooms) else: return turtle.showturtle() turtle.speed(0) turtle.clear() written_nodes = set() for edge_dir, (a, b) in edges: turtle.penup() x, y, _ = node_coords[a] turtle.goto(x / SHRINK + X_OFFSET, y / SHRINK + Y_OFFSET) if a not in written_nodes: turtle.write(a) written_nodes.add(a) turtle.pendown() if edge_dir: if edge_lengths[edge(a, b)] <= 0: turtle.pencolor('red') else: turtle.pencolor('black') else: if edge_lengths[edge(a, b)] <= 0: turtle.pencolor('blue') else: turtle.pencolor('green') x, y, _ = node_coords[b] turtle.goto(x / SHRINK + X_OFFSET, y / SHRINK + Y_OFFSET) turtle.pencolor('black') if b not in written_nodes: turtle.write(b) written_nodes.add(b) turtle.hideturtle() turtle.done()