class Ventana(): def __init__(self, titulo, alto, ancho): assert isinstance(titulo, str) assert isinstance(alto, int) and alto > 0 assert isinstance(ancho, int) and ancho > 0 ## Crea la ventana y un canvas para dibujar self.root = TK.Tk() self.root.title(titulo) self.canvas = TK.Canvas(self.root, width=ancho, height=alto) self.canvas.pack() ## Crea un TurtleScreen y la tortuga para dibujar self.fondo_ventana = TurtleScreen(self.canvas) self.fondo_ventana.setworldcoordinates(0, alto, ancho, 0) ## Establece el color de fondo self.canvas["bg"] = "blue" self.canvas.pack() ## Crea una tortuga para dibujar self.pencil = RawTurtle(self.fondo_ventana) self.pencil.pencolor("white")
class App: def __init__(self): self.win = tk.Tk() self.win.geometry("640x400") self.canvas = Canvas() self.canvas.pack() self.scr = TurtleScreen(self.canvas) self.t = RawTurtle(self.scr) self.btn = Button(self.win, text="Press me!", command=self.do_action()) self.btn.pack() def do_action(self): self.t.pensize(3) self.t.pencolor(random.choice(colors)) self.t.speed(0) self.length = 5 while self.length < 500: self.t.left(89) self.t.forward(self.length) self.t.right(-105) self.t.forward(self.length) self.length += 3 def run(self): self.win.mainloop()
def main(pattern: str) -> None: master = Tk() canvas = ScrolledCanvas(master) canvas.pack() screen = TurtleScreen(canvas) screen.colormode(255) turt = RawTurtle(screen) draw_dots(turt) turt.pencolor((178, 34, 34)) draw_pattern(turt, pattern) screen.mainloop()
class Ventana(): def __init__(self, titulo, alto, ancho): assert isinstance(titulo, str) assert isinstance(alto, int) and alto > 0 assert isinstance(ancho, int) and ancho > 0 self.root = TK.Tk() self.root.title(titulo) self.canvas = TK.Canvas(self.root, width=ancho, height=alto) self.canvas.pack() self.fondo_ventana = TurtleScreen(self.canvas) self.fondo_ventana.setworldcoordinates(0, alto, ancho, 0) self.canvas["bg"] = "gold" self.canvas.pack() self.pencil = RawTurtle(self.fondo_ventana) self.pencil.pencolor("white")
def draw_polygon(self, node, screen): pen = RawTurtle(screen) pen.speed(0) pen.hideturtle() pen.penup() try: linecolor = node.attrs['color'] except KeyError: linecolor = None try: fillcolor = node.attrs['fillcolor'] except KeyError: fillcolor = None if linecolor is not None: pen.pencolor(*linecolor) else: pen.pencolor(*(0, 0, 0)) polygon = node.data polygon = [self.translate_coords(screen, x) for x in polygon] points = to_tups(polygon) pen.goto(*(points[0])) pen.pendown() if fillcolor: pen.fillcolor(*fillcolor) pen.pencolor(*fillcolor) pen.begin_fill() for point in points[::-1]: pen.goto(*point) if fillcolor: pen.end_fill()
START_POS = (-WIDTH / 2), (-HEIGHT / 2) + HEIGHT / 20 # print(START_POS) root = Tk() # set up canvas canvas = Canvas(root, width=WIDTH, height=HEIGHT, highlightthickness=0) turtle_screen = TurtleScreen(canvas) turtle_screen.bgcolor("black") canvas.pack() # set up turtle turt = RawTurtle(turtle_screen) turt.hideturtle() turt.speed(0) turt.pencolor("RED") turt.width(3) turt.up() turt.setx(START_POS[0]) turt.sety(START_POS[1]) turt.down() farthest_pos = [0, 0] def update_pos(): tpos = turt.pos() pos = [tpos[0] - START_POS[0], tpos[1] - START_POS[1]] if abs(pos[0]) > abs(farthest_pos[0]): farthest_pos[0] = pos[0] if abs(pos[1]) > abs(farthest_pos[1]):