1 2require "tkclass" 3 4$tkline_init = FALSE 5def start_random 6 return if $tkline_init 7 $tkline_init = TRUE 8 if defined? Thread 9 Thread.start do 10 loop do 11 sleep 2 12 Line.new($c, rand(400), rand(200), rand(400), rand(200)) 13 end 14 end 15 end 16end 17 18Label.new('text'=>'Please press or drag button-1').pack 19 20$c = Canvas.new 21$c.pack 22$start_x = start_y = 0 23 24def do_press(x, y) 25 $start_x = x 26 $start_y = y 27 $current_line = Line.new($c, x, y, x, y) 28 start_random 29end 30def do_motion(x, y) 31 if $current_line 32 $current_line.coords $start_x, $start_y, x, y 33 end 34end 35 36def do_release(x, y) 37 if $current_line 38 $current_line.coords $start_x, $start_y, x, y 39 $current_line.fill 'black' 40 $current_line = nil 41 end 42end 43 44$c.bind("1", proc{|e| do_press e.x, e.y}) 45$c.bind("B1-Motion", proc{|x, y| do_motion x, y}, "%x %y") 46$c.bind("ButtonRelease-1", proc{|x, y| do_release x, y}, "%x %y") 47Tk.mainloop 48