1#!/usr/bin/env ruby
2
3require 'tk'
4require 'tkextlib/vu'
5
6#######################################
7
8puts "Show off barchart and dial widgets"
9
10speed = TkVariable.new(0)
11
12dial = Tk::Vu::Dial.new(:resolution=>0.001, :from=>-0.1, :to=>0.1,
13                        :showvalue=>true, :minortickinterval=>0.01,
14                        :tickinterval=>0.1, :radius=>50, :label=>"Dial",
15                        :beginangle=>-20, :endangle=>260, :dialcolor=>'red3',
16                        :active=>'red2', :variable=>speed)
17
18bar = Tk::Vu::Bargraph.new(:from=>0, :to=>100, :relief=>:groove,
19                           :border=>2, :label=>"Bar Chart")
20
21#######################################
22
23green   = 25
24blue    = 50
25purple  = 75
26current = 50
27
28def rand_bool
29
30end
31
32update = TkTimer.new(200, -1, proc{
33                       if (rand() - 0.5 + speed.numeric * 3) > 0
34                         current += 1
35                       else
36                         current -= 1
37                       end
38                       bar.set(current)
39                       if current < green
40                         current = 100 if current <= 0
41                         bar[:barcolor] = 'green'
42                       elsif current < blue
43                         bar[:barcolor] = 'blue'
44                       elsif current < purple
45                         bar[:barcolor] = 'purple'
46                       else
47                         bar[:barcolor] = 'red'
48                         current = 0 if current >= 100
49                       end
50                     })
51
52#######################################
53
54gobar = TkButton.new(:text=>"Start", :command=>proc{update.start})
55nobar = TkButton.new(:text=>"Stop",  :command=>proc{update.stop})
56quit  = TkButton.new(:text=>"Exit",  :command=>proc{exit})
57
58Tk.grid('x', gobar, :sticky=>:ew, :padx=>4, :pady=>4)
59Tk.grid(dial, bar, :sticky=>:news)
60Tk.grid('x', nobar, :sticky=>:ew, :padx=>4, :pady=>4)
61Tk.grid(quit, '-', '-', :sticky=>:ew, :padx=>4, :pady=>4)
62Tk.root.grid_columnconfigure(2, :weight=>1)
63Tk.root.grid_rowconfigure(1, :weight=>1)
64
65#######################################
66
67Tk.mainloop
68