5 abr 2021

ejercicio TK -1

 #!/usr/bin/ruby

#https://sandbox.mc.edu/~bennet/ruby/code/tk2_rb.html

# Import the library.



require 'tk'


# Root window.

root = TkRoot.new { title 'Presioóname !!!!' 

  background '#111188'

}


# Add a label to the root window.

lab = TkLabel.new(root) { 

  text "Presioóname !!!!" 

  background '#3333AA'

  foreground '#CCCCFF'

}


# Make it appear.

lab.pack('side' => 'left', 'fill' => 'y')


# Here's a button.  Also added to root by default.

TkButton.new {

  text "PRESIONA"

  background '#EECCCC'

  activebackground '#FFEEEE'

  foreground '#990000'

  command { print "Gracias!\n" }

  pack('side' => 'right')

}


Tk.mainloop


Balance de cryptomonedas con TK

require 'tk'

require 'tkextlib/tile'

#require 'tkextlib/tkimg'

#ocra crypto_balance.rbw --no-autoload --add-all-core --no-lzma --gem-all --no-dep-run


root = TkRoot.new {title "Calculadora de Cryptos"}

content = Tk::Tile::Frame.new(root) {padding "5 5 12 12"}.grid( :sticky => 'nsew')

TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root, 0, :weight => 1


$cantidad = TkVariable.new; $preciocompra = TkVariable.new; $precioventa = TkVariable.new; $costocompra = TkVariable.new

$costoventa = TkVariable.new; $sumafee = TkVariable.new; $balance = TkVariable.new; $cantidadcomprada = TkVariable.new; $cantidadvendida = TkVariable.new

$pminventa = TkVariable.new; $obtienes = TkVariable.new


#cajas de entrada de valores

f0 = Tk::Tile::Entry.new(content) {width 20; textvariable $cantidad}.grid( :column => 4, :row => 1, :sticky => 'we' )

f1 = Tk::Tile::Entry.new(content) {width 20; textvariable $preciocompra}.grid( :column => 2, :row => 2, :sticky => 'we' )

f2 = Tk::Tile::Entry.new(content) {width 20; textvariable $costocompra}.grid( :column => 2, :row => 3, :sticky => 'we' )

f3 = Tk::Tile::Entry.new(content) {width 20; textvariable $precioventa}.grid( :column => 2, :row => 4, :sticky => 'we' )

f4 = Tk::Tile::Entry.new(content) {width 20; textvariable $costoventa}.grid( :column => 2, :row => 5, :sticky => 'we' )

#etiquetas que reflejarán valores calculados


f5 = Tk::Tile::Label.new(content) {background "white";textvariable $cantidadcomprada}.grid( :column => 4, :row => 2, :sticky => 'we' )

f6 = Tk::Tile::Label.new(content) {background "white";width 20;textvariable $cantidadvendida}.grid( :column => 11, :row => 3, :sticky => 'we' )

f7 = Tk::Tile::Label.new(content) {background "white";width 20;textvariable $sumafee}.grid( :column => 11, :row => 4, :sticky => 'we' )

f8 = Tk::Tile::Label.new(content) {background "white";width 20;textvariable $balance}.grid( :column => 11, :row => 5, :sticky => 'we' )

f9 = Tk::Tile::Label.new(content) {background "white";width 20;textvariable $pminventa}.grid( :column => 11, :row => 6, :sticky => 'we' )

f10= Tk::Tile::Label.new(content) {background "white";foreground  "red"; font TkFont.new('times 20 bold');textvariable $obtienes}.grid( :column => 4, :row => 4, :sticky => 'we' )


# Estas líneas es por si en vez de etiquetas se qyuieren cajas

#f5 = Tk::Tile::Entry.new(content) {width 20; textvariable $cantidadcomprada}.grid( :column => 4, :row => 2, :sticky => 'we' )

#f6 = Tk::Tile::Entry.new(content) {width 20; textvariable $cantidadvendida}.grid( :column => 11, :row => 3, :sticky => 'we' )

#f7 = Tk::Tile::Entry.new(content) {width 20; textvariable $sumafee}.grid( :column => 11, :row => 4, :sticky => 'we' )

#f8 = Tk::Tile::Entry.new(content) {width 20; textvariable $balance}.grid( :column => 11, :row => 5, :sticky => 'we' )

#f9 = Tk::Tile::Entry.new(content) {width 20; textvariable $pminventa}.grid( :column => 11, :row => 6, :sticky => 'we' )

#f10 = Tk::Tile::Entry.new(content) {width 20; textvariable $obtienes}.grid( :column => 4, :row => 4, :sticky => 'we' )


#etiquetas de texto

Tk::Tile::Label.new(content) {text 'Cantidad: '}.grid( :column => 3, :row => 1, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Precio de compra: '}.grid( :column => 1, :row => 2, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Fee compra: '}.grid( :column => 1, :row => 3, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Precio de venta: '}.grid( :column => 1, :row => 4, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Fee venta: '}.grid( :column => 1, :row => 5, :sticky => 'w')


Tk::Tile::Label.new(content) {text '<<- Compraste'; justify 'center'}.grid( :column => 10, :row => 2, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Vendiste ->> '; justify 'center'}.grid( :column => 10, :row => 3, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Suma de costos : '}.grid( :column => 10, :row => 4, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Balance total : '}.grid( :column => 10, :row => 5, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Precio de venta aconsejado : '}.grid( :column => 10, :row => 6, :sticky => 'w')

Tk::Tile::Label.new(content) {text 'Obtienes : '}.grid( :column => 4, :row => 3, :sticky => 'w')



#Botón de calcular

Tk::Tile::Button.new(content) {text 'Calcular'; command {calculate}}.grid( :column => 4, :row => 6, :sticky => 'w')


#zona de cálculo cuando se pulse en el botón

TkWinfo.children(content).each {|w| TkGrid.configure w, :padx => 5, :pady => 5}

f10.focus

root.bind("Return") {calculate  }


def calculate

  begin

     $cantidadcomprada.value = ($cantidad*$preciocompra)

     $cantidadvendida.value = ($cantidad*$precioventa)

     $sumafee.value = $costocompra+$costoventa

     $balance.value = $cantidadvendida-$cantidadcomprada-$sumafee

     $pminventa.value = $preciocompra+($costocompra)*2

     $obtienes.value = $cantidadvendida-$costoventa


       rescue

     $costocompra.value = ''

     $costoventa.value = ''

     $sumafee.value = ''

     $balance.value = ''

     $pminventa.value = ''

     $obtienes.value = ''

  end

end


Tk.mainloop

25 mar 2021

Programa para cryptos con irb

#https://replit.com/languages/ruby

require 'colorize' #gem install colorize

=begin

https://ruby.obdnet.com/2021/03/pasar-de-rb-exe-con-ocra.html

Para hacer el exe, desde el mismo directorio desde el que esté el rb, pon esto en el cmd de windows:

ocra crypto.rb --console --dll ruby_builtin_dlls\libssp-0.dll --dll ruby_builtin_dlls\libgmp-10.dll --dll ruby_builtin_dlls\libgcc_s_seh-1.dll --dll ruby_builtin_dlls\libwinpthread-1.dll

=end


def pregunta_final

puts

  puts "                ***************************************************************************"

  puts " ¿ Quieres salir (S), volver al menú (M), Ver márgenes (V) o Fibonacci (F)?"

  puts "                ***************************************************************************"

  pf = gets.chomp

  pf = pf.upcase

case pf

  when "S"

  system('cls')

  puts

  puts

  puts

  puts "* Has contestado SALIR."

  puts " Adiós"

  when "M"

  system ('cls')

  pregunta_inicial

  when "V"

  system('cls')

  margenes

  when "F"

  system('cls')

  fibo

end

end


def fibo

  fibo_alza = [0, 23.6, 38.2, 50, 61.8, 76.4, 100, 138.2]

  fibo_baja = [138.2, 100, 76.4, 61.8, 50, 38.2, 23.6, 0]

  puts

  puts

  

  puts " Sobre qué valor quieres hacer la previsión"

  v = gets.to_f

  puts " *********************************************** AL ALZA *********"

  fibo_alza.each do |f|

  x = (v*f)/100


case f

  when 61.8

  puts " Sobre el punto #{f} % sale un valor de #{"%.2f" % (x+v)} <<==".red

  when 50, 76.4

  puts " Sobre el punto #{f} % sale un valor de #{"%.2f" % (x-v)} <<==".yellow

  else

  puts " Sobre el punto #{f} % sale un valor de #{"%.2f" % (x+v)}".blue

  end

  end

  puts " *****************************************************************"

  puts

  puts " ************************************************ A LA BAJA ******"

  fibo_baja.each do |ff|

  xx = (v*ff)/100

case ff

  when 38.2

  puts " Sobre el punto #{ff} % sale un valor de #{"%.2f" % (xx-v)} <<==".red

  when 50, 23.6

  puts " Sobre el punto #{ff} % sale un valor de #{"%.2f" % (xx-v)} <<==".yellow  

  else

  puts " Sobre el punto #{ff} % sale un valor de #{"%.2f" % (xx-v)}".blue

  end

 end 

  puts " *****************************************************************"


pregunta_final

end




def margenes

puts

  puts

  puts

  unless defined? $pc

  puts "¿ Qué precio inicial de COMPRA pones ?"

  $pc = gets.to_f

  else  

  puts " El precio inicial de COMPRA tengo #{$pc}".blue

  end

  

  unless defined? $pv

  puts "¿ Qué precio inicial de VENTA pones ?"

  $pv = gets.to_f

  else  

  puts " El precio inicial de VENTA tengo #{$pv}".blue

  end

  puts

  puts

 

  puts

  $incremento = ($pv - $pc)

#  puts "Esto es $incremento: #{$incremento} = #{$pv - $pc}"

  $porcentaje =(100*$incremento)/$pc

#  puts "Esto es $porcentaje: #{$porcentaje}" 

case

when $pc < $pv

puts " Hay ganancia de #{"%.2f" % ($incremento)}"

puts " Hay un incremento de #{"%.4f"% ($porcentaje)} %"

puts

fibo

when $pc > $pv

puts " Hay pérdida de #{"%.2f" % ($incremento)}"    

puts " Hay un decremento de #{"%.4f"% ($porcentaje)} %"

puts

fibo

when $pc = $pv

puts " No Hay pérdida ni beneficio #{"%.2f" % ($incremento)}"    

puts " Hay un de #{"%.4f"% ($porcentaje)} %"

puts

else

pregunta_inicial

end


pregunta_final

end



def precios


  puts " Estamos en precios."

  puts

  puts " Precio inicial de compra".blue

  $pc = gets.to_f

  puts

  puts

  puts " Precio final de venta".yellow

  $pv = gets.to_f

  puts

  $incremento = ($pv - $pc)

  $porcentaje =(100*$incremento)/$pc

 

  case 

  when $pc < $pv

puts " Hay ganancia de #{"%.2f" % ($incremento)}"

puts " Hay un incremento de #{"%.4f"% ($porcentaje)} %"

puts

when $pc > $pv

puts " Hay pérdida de #{"%.2f" % ($incremento)}"    

puts " Hay un decremento de #{"%.4f"% ($porcentaje)} %"

puts

when $pc = $pv

puts " No Hay pérdida ni beneficio #{"%.2f" % ($incremento)}"    

puts " Hay un #{"%.4f"% ($porcentaje)} %"

puts

end


pregunta_final

end



def calcular_ganancia

puts

puts " Estamos en Calcular ganancias"

puts

puts

unless defined? $pc

  puts " ¿ Cuál es el precio de partida o compra ?"

  puts

  $pc = gets.to_f

puts " ¿ Qué ganancias en % quisieras tener ?"  

  $ganancias = gets.to_f 

  else

  puts " ¿ Qué ganancias en % quisieras tener ? sobre el precio de #{$pc} ya introducido?"

  $ganancias = gets.to_f      

end   

  precio_esperado = ($pc*$ganancias)/100

  puts " Para tener esas ganancias de #{$ganancias} % deberías esperar"

  puts " a que el precio llegue a #{precio_esperado+$pc}"

  puts


pregunta_final

end



def pregunta_inicial

puts

pregunta1 = "                               ¿ Qué quieres hacer ?          "


puts "                        ****************************************"

#pregunta1.size.times {print "*"}

puts

puts pregunta1

#40.times {print "*"}

puts

puts "                        *********************************************"

puts " *                                           *"

puts " *     MENU PRINCIPAL                        *"

puts " *     ==============                        *"

puts " *                                           *"

puts " *     1.- Ver ganancia y/o pérdida real.    *"

puts " *     2.- Calcular mi ganancia futura.      *"

puts " *     3.- Ver márgenes de Fibonacci.        *"

puts " *                                           *"

puts " *     4.- Salir.                            *"

puts " *                                           *"

puts "                        *********************************************"

puts

puts

puts " *".yellow

puts " *".yellow

puts "***".yellow

puts " *".yellow

hacer = gets.chomp.to_i


case hacer

when 1

system('cls')

precios


when 2

system('cls')

calcular_ganancia


when 3

system('cls')

fibo


when 4

system('cls')

  puts

  puts

  puts

  puts " * Quieres terminar."

  puts "      Adiós"

else

system('cls')

end

end

pregunta_inicial


22 mar 2021

Pasar de rb a exe con OCRA

Primero has de tener instalado Ruby

Después, instala la gema ocra así:

gem install ocra 

Como da problemas si tan sólo llamamos a la gema ocra desde el cmd de windows con irb así:

ocra nombredelfichero.rb

Sustituye nombredelfichero.rb por el nombre de tu fichero de ruby

hay que hacer esto:

A.- Si el programa es de salida por consola:

ocra nombredelfichero.rb --console --dll ruby_builtin_dlls\libssp-0.dll --dll ruby_builtin_dlls\libgmp-10.dll --dll ruby_builtin_dlls\libgcc_s_seh-1.dll --dll ruby_builtin_dlls\libwinpthread-1.dll


B.- Si el programa es de salida con sistema de ventanas:

ocra nombredelfichero.rb --windows --dll ruby_builtin_dlls\libssp-0.dll --dll ruby_builtin_dlls\libgmp-10.dll --dll ruby_builtin_dlls\libgcc_s_seh-1.dll --dll ruby_builtin_dlls\libwinpthread-1.dll --no-dep-run

Y de esta forma no da problemas. ✌

https://github.com/gosu/gosu/wiki/Ruby-Packaging-on-Windows


Estos enlaces son para poner colores a los textos por consola

https://programmerclick.com/article/5913177622/

https://medium.com/memorias-de-un-techie/ruby-c%C3%B3mo-colorear-la-consola-o-el-irb-13919f736ab2

https://ourcodeworld.co/articulos/leer/270/como-crear-un-ejecutable-exe-a-partir-de-un-script-ruby-en-windows-usando-ocra


ejercicio TK -1

 #!/usr/bin/ruby #https://sandbox.mc.edu/~bennet/ruby/code/tk2_rb.html # Import the library. require 'tk' # Root window. root = TkRo...