=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
Ruby es un lenguaje es un universo, donde todo parte de un mismo objeto "main" y desde ahí se diferencian los demás. Es realmente fácil de ir comprendiendo y nos va a permitir ir plasmando la idea que generó la necesidad de hacer ese programa pero muy humanamente.
6 nov 2018
Teoría - TK9
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...
-
#!/usr/bin/ruby #https://sandbox.mc.edu/~bennet/ruby/code/tk2_rb.html # Import the library. require 'tk' # Root window. root = TkRo...
-
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 oc...
-
https://sourceforge.net/projects/redsky-ruby/files/RedSky%20Stable%20Setup/RedSky%20Ruby%20v2019.1%20SETUP.exe/download