5 abr 2021

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

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