6 nov 2018

Teoría - Tk-CANVAS 1

Tk-CANVAS 1

CANVAS es la utilidad con la que se puede dibujar en TK.
El siguiente programa necesita de este código después de require:

module TkItemConfigOptkeys
    def itemconfig_hash_kv(id, keys, enc_mode = [], conf = [])   # Esto es necesario para que funcione
        hash_kv(__conv_item_keyonly_opts(id, keys), enc_mode, conf)
    end
end

Dibujará:
  • una línea desde origen (0,0) hasta extremo (100,100) color rojo y espesor 2.
  • un rectángulo desde (colunna1c,fila2c) hasta (columna3c, fila3c) con relleno amarillo y borde rojo
require "tk"

module TkItemConfigOptkeys
    def itemconfig_hash_kv(id, keys, enc_mode = [], conf = [])   # Esto es necesario para que funcione
        hash_kv(__conv_item_keyonly_opts(id, keys), enc_mode, conf)
    end
end

canvas = TkCanvas.new

TkcRectangle.new(canvas, '1c', '2c', '3c', '3c', 'outline' => 'red', 'fill' => 'yellow')

TkcLine.new(canvas, 0, 0, 100, 100, 'width' => '2', 'fill' => 'red')
canvas.pack

Tk.mainloop

Teoría - TK9

=begin
https://www.tutorialspoint.com/ruby/ruby_tk_guide.htm
=end


require 'tk'
$p = TkVariable.new
$cantidad = TkVariable.new
$mili = TkVariable.new
$cen = TkVariable.new
$metro = TkVariable.new
$t = TkVariable.new
$nombre = TkVariable.new

$f1 = TkFrame.new($v) {
   relief 'sunken'
   borderwidth 3
   #background "red"
   height 260
   width 640
   pack('side' => 'top')
}

$f2 = TkFrame.new($v) {
   borderwidth 2
   background "yellow"
   height 220
   width 640
   pack('side' => 'bottom')
}

$v = TkRoot.new {title "Conversor"}
$v.geometry('640x480')
$v.minsize(640,480)

TkLabel.new($f2) {
text ' Aquí aparecerán los resultados '
font TkFont.new('times 25')
   background "yellow"
place('relx'=>0.25,'rely'=>0.15)
}

def print_v
   print $p, "\n"
   puts  "\n"
   #puts $f1.methods, "\n"
end
TkLabel.new($f1) {
text ' ¿Cómo te llamas ? '
font TkFont.new('times 15')
place('relx'=>0.25,'rely'=>0.05)
}

TkEntry.new($f1) {
   textvariable $nombre
   font TkFont.new('times 15')
   place('relx'=>0.55,'rely'=>0.05)
   borderwidth 2
   }

TkLabel.new($f1) {
text 'Selecciona la unidad a convertir'
font TkFont.new('times 20 bold')
foreground  "red"
place('relx'=>0.1,'rely'=>0.28)
}

TkRadioButton.new($f1) { 
   text 'Pulgadas a S.M.I.'
   variable $p
   anchor 'w'
   value 25.4
   place('relx'=>0.75,'rely'=>0.25)
}

TkRadioButton.new($f1) { 
   text 'S.M.I. a Pulgadas'
   variable $p
   value 'top'
   value 1/25.4
   anchor 'w'
   place('relx'=>0.75,'rely'=>0.35)
}

TkLabel.new($f1) {
text 'S.I.: Sistema Métrico Internacional'
font TkFont.new('times 8 bold')
foreground  "blue"
place('relx'=>0.65,'rely'=>0.45)
}

TkLabel.new($f1) {
text 'Escribe la cantidad: '
font TkFont.new('times 15')
foreground  "blue"
place('relx'=>0.2,'rely'=>0.55)
}


TkEntry.new($f1){
   textvariable $cantidad
   font TkFont.new('times 15')
   place('relx'=>0.5,'rely'=>0.55)
   borderwidth 4
   }
   
TkButton.new($f1) {
 text 'Calcular'
 command {if $p == 25.4 then cal else cal1 end}
 place('relx'=>0.6,'rely'=>0.8)
 }
def ventana_1
TkLabel.new($f2) {     #Unas etiqueta con sorpresa
text 'Gracias'
font TkFont.new('times 15')
foreground  "blue"
background "yellow"
place('relx'=>0.01,'rely'=>0.5)

label = TkLabel.new($f2)
background "yellow"
label.image = $foto
label.place('x' => 10, 'y' => 20)
}
end

def ventana_2
  TkLabel.new($f2) {
   height 10 #Altura
   width 65 #Anchura
background "yellow"
place('relx'=>0.2,'rely'=>0.0)
}
end

def cal

TkLabel.new($f2) {
textvariable $nombre
font TkFont.new('times 15')
background "yellow"
foreground  "blue"
place('relx'=>0.0,'rely'=>0.73)
}

salir = TkButton.new($f2) {
 text 'Salir'
  command 'exit'
 place('relx'=>0.9,'rely'=>0.8)
 }
$foto = TkPhotoImage.new
$foto.file = "buena1.gif"
  begin
     $mili.value = ($p*$cantidad).round(2)
     $cen.value = ($p/10*$cantidad).round(3)
     $metro.value = ($p/100*$cantidad).round(4)
         
  rescue
     $mili.value = ''
     $cen.value = ''
     $metro.value = ''
         
  end
ventana_2

TkLabel.new($f2) {
text " #{$cantidad} pulgada/s equivalen a: "
font TkFont.new('times 12')
background "yellow"
place('relx'=>0.20,'rely'=>0.10)
}
TkLabel.new($f2) {
text " milímetros."
font TkFont.new('times 15')
background "yellow"
place('relx'=>0.70,'rely'=>0.20)
}

TkLabel.new($f2) {
text " centímetros."
font TkFont.new('times 15')
background "yellow"
place('relx'=>0.70,'rely'=>0.35)
}

TkLabel.new($f2) {
text " metros."
font TkFont.new('times 15')
background "yellow"
place('relx'=>0.70,'rely'=>0.50)
}

TkLabel.new($f2) {
textvariable $mili
foreground  "red"
font TkFont.new('times 15')
background "yellow"
place('relx'=>0.50,'rely'=>0.20)
}

TkLabel.new($f2) {
textvariable $cen
foreground  "red"
font TkFont.new('times 15')
background "yellow"
place('relx'=>0.50,'rely'=>0.35)
}

TkLabel.new($f2) {
textvariable $metro
foreground  "red"
font TkFont.new('times 15')
background "yellow"
place('relx'=>0.50,'rely'=>0.50)
}
ventana_1
end


def cal1

TkLabel.new($f2) {
textvariable $nombre
font TkFont.new('times 15')
background "yellow"
foreground  "blue"
place('relx'=>0.0,'rely'=>0.73)
}

salir = TkButton.new($f2) {
 text 'Salir'
  command 'exit'
 place('relx'=>0.9,'rely'=>0.8)
 }
$foto = TkPhotoImage.new
$foto.file = "buena1.gif"
begin
     $t.value = ($p*$cantidad).round(3)
              
  rescue
     $t.value = ''
        
  end
ventana_2

TkLabel.new($f2) {
text 'Pulgadas'
font TkFont.new('times 15')
foreground  "blue"
background "yellow"
place('relx'=>0.65,'rely'=>0.6)
}  

TkLabel.new($f2) {
text "#{$cantidad} centímetro/s equivalen a: "
font TkFont.new('times 15')
background "yellow"
place('relx'=>0.3,'rely'=>0.40)
}

TkLabel.new($f2) {
textvariable $t
foreground  "red"
background "yellow"
font TkFont.new('times 15')
place('relx'=>0.50,'rely'=>0.59)
}
  
ventana_1
end

Tk.root.bind "1", proc{print_v}
Tk.mainloop

Teoría - TK8

=begin
https://www.tutorialspoint.com/ruby/ruby_tk_guide.htm
=end

require 'tk'

$mili = TkVariable.new
$pulgadas = TkVariable.new
$cen = TkVariable.new
$metro = TkVariable.new
$nombre = TkVariable.new

v = TkRoot.new {title "Conversor"}
v.geometry('640x480')
v.minsize(600,380)

TkLabel.new(v) {
text ' ¿Cómo te llamas ? '
font TkFont.new('times 15')
place('relx'=>0.1,'rely'=>0.150)
}

e5 = TkEntry.new(v){
   textvariable $nombre
   font TkFont.new('times 15')
   place('relx'=>0.55,'rely'=>0.15)
   borderwidth 2
   }

e1 = TkLabel.new(v) {
text 'Conversión de pulgadas a milímetros'
font TkFont.new('times 20 bold')
foreground  "red"
place('relx'=>0.1,'rely'=>0.05)
}

e2 = TkLabel.new(v) {
text 'Escribe la cantidad de pulgadas: '
font TkFont.new('times 15')
foreground  "blue"
place('relx'=>0.1,'rely'=>0.25)
}

e3 = TkEntry.new(v){
   textvariable $pulgadas
   font TkFont.new('times 15')
   place('relx'=>0.55,'rely'=>0.25)
   borderwidth 2
   }
TkLabel.new(v) {
text ' Equivale a: '
font TkFont.new('times 15')
place('relx'=>0.33,'rely'=>0.50)
}

TkLabel.new(v) {
textvariable $mili
foreground  "red"
font TkFont.new('times 20')
place('relx'=>0.50,'rely'=>0.49)
}


TkLabel.new(v) {
textvariable $cen
foreground  "red"
font TkFont.new('times 20')
place('relx'=>0.50,'rely'=>0.59)
}

TkLabel.new(v) {
textvariable $metro
foreground  "red"
font TkFont.new('times 20')
place('relx'=>0.50,'rely'=>0.69)
}

TkButton.new(v) {
 text 'Calcular'
 command {cal}
 place('relx'=>0.6,'rely'=>0.38)
 }
 
e4 = TkLabel.new(v) {
text 'Una pulgada equivale a 25.4 milímetros. '
font TkFont.new('times 15')
place('relx'=>0.3,'rely'=>0.8)
}

=begin
TkLabel.new(v) {
textvariable $nombre
font TkFont.new('times 16')
foreground  "blue"
place('relx'=>0.08,'rely'=>0.73)
}
=end

TkLabel.new(v) {
text " milímetros."
font TkFont.new('times 15')
place('relx'=>0.70,'rely'=>0.50)
}

TkLabel.new(v) {
text " centímetros."
font TkFont.new('times 15')
place('relx'=>0.70,'rely'=>0.60)
}

TkLabel.new(v) {
text " metros."
font TkFont.new('times 15')
place('relx'=>0.70,'rely'=>0.70)
}

salir = TkButton.new(v) {
 text 'Salir'
 command 'exit'
 place('relx'=>0.9,'rely'=>0.9)
 }
$foto = TkPhotoImage.new
$foto.file = "buena1.gif"

# con el método .round redondeamos el número para que muestre dos decimales
def cal

  begin
     $mili.value = (25.4*$pulgadas).round(2)
     $cen.value = (2.54*$pulgadas).round(2)
     $metro.value = (0.254*$pulgadas).round(2)
         
  rescue
     $mili.value = ''
     $cen.value = ''
     $metro.value = ''
         
  end
TkLabel.new {     #Unas etiqueta con sorpresa
text 'Gracias'
font TkFont.new('times 16')
foreground  "blue"
place('relx'=>0.09,'rely'=>0.83)

TkLabel.new {
textvariable $nombre
font TkFont.new('times 16')
foreground  "blue"
place('relx'=>0.08,'rely'=>0.73)
}

label = TkLabel.new 
label.image = $foto
label.place('x' => 80, 'y' => 250)
} 
end

Tk.mainloop

Teoría - TK7

=begin
https://www.tutorialspoint.com/ruby/ruby_tk_guide.htm
=end

require 'tk'

$mili = TkVariable.new
$pulgadas = TkVariable.new

v = TkRoot.new {title "Conversor"}
v.geometry('640x480')
v.minsize(600,380)

e1 = TkLabel.new(v) {
text 'Conversión de pulgadas a milímetros'
font TkFont.new('times 20 bold')
foreground  "red"
place('relx'=>0.1,'rely'=>0.1)
}

e2 = TkLabel.new(v) {
text 'Escribe la cantidad de pulgadas: '
font TkFont.new('times 15')
foreground  "blue"
place('relx'=>0.1,'rely'=>0.25)
}

e3 = TkEntry.new(v){
   textvariable $pulgadas
   place('relx'=>0.55,'rely'=>0.25)
}
TkLabel.new(v) {
text ' Equivale a: '
font TkFont.new('times 15')
place('relx'=>0.35,'rely'=>0.50)
}

TkLabel.new(v) {
textvariable $mili
foreground  "red"
font TkFont.new('times 20')
place('relx'=>0.51,'rely'=>0.49)
}

TkButton.new(v) {
 text 'Calcular'
 command {cal}
 place('relx'=>0.6,'rely'=>0.38)
 }
 
e4 = TkLabel.new(v) {
text 'Una pulgada equivale a 25,4 mm. '
font TkFont.new('times 15')
place('relx'=>0.3,'rely'=>0.65)
}

TkLabel.new(v) {
text " milímetros"
font TkFont.new('times 15')
place('relx'=>0.70,'rely'=>0.50)
}

salir = TkButton.new(v) {
 text 'Salir'
 command 'exit'
 place('relx'=>0.9,'rely'=>0.9)
 }
foto = TkPhotoImage.new
foto.file = "buena1.gif"

label = TkLabel.new(v) 
label.image = foto
label.place('height' => foto.height, 'width' => foto.width, 'x' => 80, 'y' => 250)


def cal
  begin
     $mili.value = (25.4*$pulgadas)
  rescue
     $mili.value = ''
  end
end

Tk.mainloop

Teoría - TK6

=begin
https://www.tutorialspoint.com/ruby/ruby_tk_guide.htm
http://www.es.w3eacademy.com/ruby/ruby_tk_place.htm
=end

require 'tk'

top = TkRoot.new {title "Etiqueta, entrada de texto y botón salir"}
top.geometry('640x480')
top.minsize(400,400)


#code to add a label widget
lb0=TkLabel.new(top){
   text 'Esto es una prueba de colocación de etiquetas, campos y botones en una ventana'
   background "yellow"
   foreground "blue"
   place('relx'=>0.0,'rely'=>0.0)
}
lb1=TkLabel.new(top){
   text '0123456789 '
   background "yellow"
   foreground "blue"
   place('relx'=>0.08,'rely'=>0.08)
}

lb2=TkLabel.new(top){
   text 'Aquí puedes escribir:'
   background "green"
   foreground "black"
   place('relx'=>0.05,'rely'=>0.15)
}

text = TkText.new(top) do
   width 60
   height 15
   borderwidth 1
   font TkFont.new('times 12 bold')
   place('relx'=>0.1,'rely'=>0.2)
  

end

 vbar = TkScrollbar.new {
    orient 'vert'
    pack(:side => :right, :fill => :y)
    }
text.yscrollbar(vbar)
text.insert 'end', "Hola!\n\ntext widget example"

=begin
relx y rely son las posiciones relativas respecto de la ventana
en %, por eso es un número decimal
relx y en rely el decimal es la posición x ó y respe
=end
#code to add a entry widget
e1 = TkEntry.new(top){
   background "red"
   foreground "blue"
   place('relx'=>0.2,'rely'=>0.08)
}
sal = TkButton.new(top) {
 text 'Salir'
 command 'exit'
 place('relx'=>0.9,'rely'=>0.9)
 }
Tk.mainloop

Teoría - TK5

=begin
https://www.tutorialspoint.com/ruby/ruby_tk_guide.htm
http://www.es.w3eacademy.com/ruby/ruby_tk_place.htm
=end

require 'tk'

top = TkRoot.new {title "Etiqueta, entrada de texto y botón salir"}
top.geometry('640x480')
top.minsize(400,400)

#code to add a label widget
lb0=TkLabel.new(top){
   text 'Esto es una prueba de colocación de etiquetas, campos y botones en una ventana'
   background "yellow"
   foreground "blue"
   place('relx'=>0.0,'rely'=>0.0)
}
lb1=TkLabel.new(top){
   text '0123456789 '
   background "yellow"
   foreground "blue"
   place('relx'=>0.08,'rely'=>0.08)
}

lb2=TkLabel.new(top){
   text 'Aquí puedes escribir:'
   background "green"
   foreground "black"
   place('relx'=>0.05,'rely'=>0.15)
}
text = TkText.new(top) do
   width 60
   height 15
   borderwidth 1
   font TkFont.new('times 12 bold')
   place('relx'=>0.1,'rely'=>0.2)
end
text.insert 'end', "Hola!\n\nEsto es un ejemplo"
=begin
relx y rely son las posiciones relativas respecto de la ventana
en %, por eso es un número decimal
relx y en rely el decimal es la posición x ó y respectivamente
=end
#code to add a entry widget
e1 = TkEntry.new(top){
   background "red"
   foreground "blue"
   place('relx'=>0.2,'rely'=>0.08)
}
sal = TkButton.new(top) {
 text 'Salir'
 command 'exit'
 place('relx'=>0.9,'rely'=>0.9)
 }
Tk.mainloop

Teoría - TK4

=begin
Este es un ejemplo de una ventana con varios utensilios
CUATRO Frames o marcos y en cada cual irá un utensilio
en total se verán dos etiquetas y dos botones
todo con las mínimas líneas de código posible
y con el menor código posible
https://www.tutorialspoint.com/ruby/ruby_tk_guide.htm
http://www.es.w3eacademy.com/ruby/ruby_tk_place.htm
=end

require 'tk'

top = TkRoot.new {title "Etiqueta, entrada de texto y botón salir"}
top.geometry('640x480')

#code to add a label widget
lb0=TkLabel.new(top){
   text 'Esto es una prueba de colocación de etiquetas, campos y botones en una ventana'
   background "yellow"
   foreground "blue"
   place('relx'=>0.1,'rely'=>0.0)
}
lb1=TkLabel.new(top){
   text '0123456789 '
   background "yellow"
   foreground "blue"
   place('relx'=>0.1,'rely'=>0.1)
}

lb2=TkLabel.new(top){
   text '0123456789(0.2 - 0.5)'
   background "green"
   foreground "black"
   place('relx'=>0.5,'rely'=>0.5)
}

=begin
relx y rely son las posiciones relativas respecto de la ventana
en %, por eso es un número decimal
relx y en rely el decimal es la posición x ó y respe
=end
#code to add a entry widget
e1 = TkEntry.new(top){
   background "red"
   foreground "blue"
   place('relx'=>0.19,'rely'=>0.1)
}
sal = TkButton.new(top) {
 text 'Salir'
 command 'exit'
 place('relx'=>0.9,'rely'=>0.9)
 }
Tk.mainloop

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...