1require 'tk' 2 3v = TkVariable.new(0) 4l = TkLabel.new(:textvariable=>v).pack(:pady=>[1, 10]) 5 6a = TkButton.new(:text=>"button A :: proc{p ['AAA', v.value]}").pack(:fill=>:x, :pady=>[1, 15], :padx=>15) 7a.command{p ['AAA', v.value]} 8 9TkLabel.new(:text=>'Callback of the button B returns LIFO order').pack 10b = TkButton.new(:text=>"button B :: proc{n = v.value; p ['B:start', n]; Tk.sleep(10000); p ['B:end', n]}").pack(:fill=>:x, :pady=>[1, 15], :padx=>15) 11b.command{n = v.value; p ['B:start', n]; Tk.sleep(10000); p ['B:end', n]} 12 13TkLabel.new(:text=>'Callback of the button C returns FIFO order').pack 14c = TkButton.new(:text=>"button C :: proc{n = v.value; Thread.new{p ['C:start', n]; Tk.sleep(10000); p ['C:end', n]}}").pack(:fill=>:x, :pady=>[1, 15], :padx=>15) 15c.command{n = v.value; Thread.new{p ['C:start', n]; Tk.sleep(10000); p ['C:end', n]}} 16 17TkLabel.new(:text=>'Callback of the button D blocks eventloop (no respond to event)').pack 18d = TkButton.new(:text=>"button D :: proc{n = v.value; p ['D:start', n]; sleep(10); p ['D:end', n]}").pack(:fill=>:x, :pady=>[1,15], :padx=>15) 19d.command{n = v.value; p ['D:start', n]; sleep(10); p ['D:end', n]} 20 21TkLabel.new(:text=>'Callback of the button E is another way to avoid eventloop blocking').pack 22e = TkButton.new(:text=>"button E :: proc{n = v.value; Thread.new{p ['D:start', n]; sleep(10); p ['D:end', n]}}").pack(:fill=>:x, :pady=>[1,15], :padx=>15) 23e.command{n = v.value; Thread.new{p ['D:start', n]; sleep(10); p ['D:end', n]}} 24 25TkButton.new(:text=>'QUIT', :command=>proc{exit}).pack 26 27TkTimer.new(500, -1){v.numeric += 1}.start 28 29Tk.mainloop 30