Código de Python - Programa de dibujo con "turtle"

Imágen de perfil
Val: 712
Bronce
Ha aumentado 1 puesto en Python (en relación al último mes)
Gráfica de Python

Programa de dibujo con "turtle"gráfica de visualizaciones


Python

Publicado el 27 de Mayo del 2020 por Antonio (76 códigos)
9.529 visualizaciones desde el 27 de Mayo del 2020
Programa para creación de dibujos, a partir del número de trazos y angulo de giro del puntero. Los colores pueden seleccionarse usando el botón "Color Pincel".
tur

1.1
estrellaestrellaestrellaestrellaestrella(2)

Publicado el 27 de Mayo del 2020gráfica de visualizaciones de la versión: 1.1
9.530 visualizaciones desde el 27 de Mayo del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

Lenguaje: Python
Librerías: tkinter, turtle, os, pickle
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import turtle
from tkinter import *
from tkinter import colorchooser
from tkinter import messagebox
from tkinter import filedialog
import os
import pickle
 
ventana=Tk()
ventana.geometry("860x800")
ventana.title("Crea Dibujo")
ventana.configure(background="gray80")
st=True
grosor=""
movs=""
grados=""
color_fondo=""
 
lista_colores=[]
canvas = Canvas(master = ventana, width = 860, height = 735)
canvas.pack()
 
t = turtle.RawTurtle(canvas)
 
def color(m):
    global color_fondo
    global lista_colores
    color_selec=colorchooser.askcolor()
    if color_selec!=(None,None):
        bgrcolor=list(color_selec)
        if m == "f":
            color_fondo=bgrcolor[1]
            t.screen.bgcolor(color_fondo)
        else:
            lista_colores.append(bgrcolor[1])
            t.color(lista_colores[0])
 
def hide():
    global st
    if st==True:
        t.hideturtle()
        st=False
    else:
        t.showturtle()
        st=True
 
def abrir():
    global lista_colores
    global color_fondo
    global movs
    global grados
    clear()
    open_archive=filedialog.askopenfilename(initialdir = "/",
                 title = "Seleccione archivo",filetypes = (("all files","*.*"),
                 ("all files","*.*")))
    if open_archive!="":
        try:
            nombre=pickle.load(open(open_archive,"rb"))
            lista_colores = default_color(nombre[2])
            color_fondo = nombre[3]
            if nombre[3] == "":
                color_fondo = "white"
            movs=int(nombre[0])
            grados=int(nombre[1])
            t.screen.bgcolor(color_fondo)
            if entGrosor.get()!="":
                t.pensize(int(entGrosor.get()))
            d=1
            val = validate_data(movs,grados)
            try:
                t.speed(0)
                for i in range(movs):
                    if len(lista_colores)>1:
                        t.color(lista_colores[i%(len(lista_colores))])
                    else:
                        t.color(lista_colores[0])
                    t.left(grados)
                    t.fd(d)
                    d+=1
            except:
                if val == False:
                    messagebox.showwarning("ERROR","Datos introducidos erroneos o insuficientes")
        except:
            messagebox.showwarning("ERROR","No se pudo cargar el atchivo")
 
def validate_data(n,d):
    validate = True
    try:
        n = int(n)
        d = int(d)
    except:
        print("ERROR")
        validate = False
    return validate
 
 
def guardar():
    archivo = [entLados.get(),entGrados.get(),lista_colores,color_fondo]
    guardar = filedialog.asksaveasfilename(initialdir = "/",
              title = "Guardar", defaultextension = ".txt",
              filetypes = (("txt files","*.txt"),("pickle files","*.pickle"),
                           ("all files","*.*")))
    if guardar != "":
        pickle.dump(archivo,open(guardar,"wb"))
        messagebox.showinfo("GUARDADO","Archivo guardado satistactoriamente")
        #print(archivo)
 
def default_color(lis):
    if lis == []:
        lis.append("black")
    return lis
 
def clear():
    global lista_colores
    t.reset()
    lista_colores=[]
 
def crear():
    global lista_colores
    d=1
    if entGrosor.get()!="":
        t.pensize(int(entGrosor.get()))
    lista_colores = default_color(lista_colores)
    val = validate_data(entLados.get(),entGrados.get())
    try:
        t.speed(0)
        for i in range(int(entLados.get())):
            if len(lista_colores)>1:
                t.color(lista_colores[i%(len(lista_colores))])
            else:
                t.color(lista_colores[0])
            t.left(int(entGrados.get()))
            t.fd(d)
            d+=1
    except:
        if val == False:
            messagebox.showwarning("ERROR","Datos introducidos erroneos o insuficientes")
 
 
etiLados=Label(master=ventana,text="Numero Mov",bg="gray80")
etiLados.place(x=1,y=744)
etiGrados=Label(master=ventana,text="Grados",bg="gray80")
etiGrados.place(x=160,y=744)
entGrados=Entry(master=ventana,width=10)
entGrados.place(x=200,y=744)
entLados=Entry(master=ventana,width=10)
entLados.place(x=85,y=744)
etiGrosor=Label(master=ventana,text="Grosor",bg="gray80")
etiGrosor.place(x=267,y=744)
entGrosor=Entry(master=ventana,width=10)
entGrosor.place(x=310,y=744)
btnColor=Button(master=ventana,text="Color Pincel",bg="gray74",command=lambda:color("c"))
btnColor.place(x=403,y=740)
btnFondo=Button(master=ventana,text="Color Fondo",bg="gray74",command=lambda:color("f"))
btnFondo.place(x=490,y=740)
btnClear=Button(master=ventana,text="Clear",bg="gray74",command=clear)
btnClear.place(x=660,y=740)
btnGuardar=Button(master=ventana,text="Guardar",bg="gray74",width=10,command=guardar)
btnGuardar.place(x=778,y=740)
btnAbrir=Button(master=ventana,text="Abrir",bg="gray74",width=8,command=abrir)
btnAbrir.place(x=710,y=740)
btnHide=Button(master=ventana,text="Hide/Show",bg="gray74",command=hide)
btnHide.place(x=580,y=740)
Button(master = ventana,text="Crear",bg="spring green",width=121,command=crear).place(x=1,y=771)
 
ventana.mainloop()



Comentarios sobre la versión: 1.1 (2)

Fabio jimenez
28 de Octubre del 2022
estrellaestrellaestrellaestrellaestrella
Excelente Maestro Alfonso, siempre veo sus videos los cuales son muy interesantes e importantes, muchas gracias por compartir sus conocimientos.
Responder
29 de Noviembre del 2023
estrellaestrellaestrellaestrellaestrella
import math
import turtle
turtle.bgcolor("black")
turtle.shape("turtle")
turtle.speed(0)
turtle.goto(0, -40)
turtle.pendown()
h = 0
phi = 137.508 * (math.pi / 180.0)
for i in range(16):
for j in range(18):
turtle.color("yellow")
h += 0.005
turtle.rt(90)
turtle.circle(150 - j * 6, 90)
turtle.lt(90)
turtle.circle(150 - j * 6, 90)
turtle.rt(180)
turtle.circle(40, 24)
turtle.penup()
turtle.goto(0, 0)
turtle.color("black")
turtle.fillcolor("brown")
turtle.begin_fill()
turtle.circle(0)
turtle.end_fill()
phi = 137.508 * (math.pi / 180.0)
for i in range(160 + 40):
r = 4 * math.sqrt(i)
theta = i * phi
x = r * math.cos(theta)
y = r * math.sin(theta)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(i * 137.508)
turtle.pendown()
if i < 160:
turtle.stamp()
turtle.penup()
turtle.goto(0, 300)
turtle.color("White")
turtle.write("Te Quiero", align="center", font=("Arial", 24, "bold"))
turtle.write("De mi para ti", align="center", font=("Arial", 24, "bold"))
Responder

Comentar la versión: 1.1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6235