PDA

View Full Version : A simple script using TK


SlipKnot
December 9th, 2005, 04:14 PM
This is using the TK library, I'm still leraning, so don't laught.
http://img214.imageshack.us/img214/3401/tktut9yh.png
# this is always needed for a Tk aplication
require "tk"

# Our main window:
main = TkRoot.new{title "Tk example"}

# Instance variable
@var1 = TkVariable.new

# A label with text "Check:"
label1 = TkLabel.new(main,'text'=>"Check:")

# A check button
check1 = TkCheckButton.new(main,'variable'=>@var1)

# Packing the first label and the check button
[label1,check1].each{|obj|
obj.pack('side'=>'left','padx'=>10,'pady'=>10)}

# Return the state of the check button
def check_var
case @var1#.value
when 0 then return "unchecked"
when 1 then return "checked" end
end

# Another label, it shows the state of the check button
label2 = TkLabel.new(main,'text'=>"none")

# This button update the "label2"
but1 = TkButton.new(main,'text'=>"Show Check",
'command'=>proc{
label2.configure('text'=>check_var) }
)

# Packing the second label and the normal button
[label2,but1].each{|obj|
obj.pack('side'=>'top','pady'=>10,'padx'=>10)}

# Other thing always needed in Tk aplication
Tk.mainloop

sheefo
December 14th, 2005, 04:58 PM
Whats the 'TK library'? Is it a GUI toolkit of some sort?
Ah, its built in, I diden't need to install anything extra (unlike wxRuby).
You should also try wxRuby it's much better and easier, plus it has a visual wx thingy for making applications, similar to Visual Basic.

SlipKnot
December 15th, 2005, 03:13 PM
Thanks Sheefo:cool: