Mouse Locations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'tk'

root = TkRoot.new { title 'Mouse Position Demo' }
canvas = TkCanvas.new(root).grid(:sticky => 'nsew')

# needed for proper resize behavior
TkGrid.columnconfigure(root, 0, :weight => 1)
TkGrid.rowconfigure(root, 0, :weight => 1)

root.bind('1',
          lambda do |e|

            TkcText.new(canvas, e.x, e.y-6,
                        :font => 'Consolas 7',
                        :text => "#{e.x}, #{e.y}")
          end)

Tk.mainloop

Progressbar Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#
# ProgressBar (pure Ruby/Tk version)
#                2009/11/29  by Hidetoshi NAGAI ([email protected])
#
require 'tk'

module Tk
  module RbWidget
    class ProgressBar < TkWindow
    end
  end
end


class Tk::RbWidget::ProgressBar
  include TkComposite

  def initialize_composite(keys = {})
    # defaults
    bar_color     = 'gray60'
    trough_color  = 'gray90'
    text_color    = 'black'
    reverse_color = 'white'

    font_size = -12

    @width  = 150
    @height =  20

    # create widgets
    @font = TkFont.new(['Helvetica', font_size])

    @var = TkVariable.new('')

    @frame.configure(:borderwidth=>3, :relief=>:ridge)

    @size_base = Tk::Frame.new(@frame, :width=>@width, :height=>@height, 
                               :borderwidth=>0)

    @size_base.pack_propagate(false)

    @base_label = Tk::Label.new(@size_base, :textvariable=>@var, :font=>@font, 
                                :highlightthickness=>0, :borderwidth=>0, 
                                :foreground=>text_color, 
                                :background=>trough_color, 
                                :padx=>1, :pady=>1).pack(:fill=>:both, 
                                                         :expand=>true)
    @prog_bar = Tk::Frame.new(@base_label, 
                              :highlightthickness=>0, :borderwidth=>0
                              ).place(:x=>0, :y=>0, 
                                      :anchor=>:nw, 
                                      :relwidth=>0.0, :relheight=>1.0)

    @prog_label = Tk::Label.new(@prog_bar, :textvariable=>@var, :font=>@font, 
                                :foreground=>reverse_color, 
                                :background=>bar_color,  
                                :highlightthickness=>0, :borderwidth=>0, 
                                :padx=>1, :pady=>1).place(:x=>0, :y=>0, 
                                                          :width=>@width, 
                                                          :height=>@height,
                                                          :anchor=>:nw)

    # for bindings
    @size_base.bindtags  <<= @frame
    @base_label.bindtags <<= @frame
    @prog_bar.bindtags   <<= @frame
    @prog_label.bindtags <<= @frame

    @size_base.bind('Configure'){
      Tk.update_idletasks
      @width = @size_base.winfo_width
      @prog_label.place(:width=>@width)
      @height = @size_base.winfo_height
      @prog_label.place(:height=>@height)
    }

    @size_base.pack(:expand=>true, :fill=>:both)

    # delegates
    delegate('DEFAULT', @frame)
    delegate('background', @prog_label)
    delegate('foreground', @base_label)

    delegate_alias('barcolor',     'background', @prog_label)
    delegate_alias('troughcolor',  'background', @base_label)
    delegate_alias('textcolor',    'foreground', @base_label)
    delegate_alias('reversecolor', 'foreground', @prog_label)

    delegate_alias('fontfamily', 'family', @font)
    delegate_alias('fontsize',   'size',   @font)
    delegate_alias('fontweight', 'weight', @font)
    delegate_alias('fontslant',  'slant',  @font)

    option_methods(:width, :height, :font)

    configure(keys) unless keys.empty?
  end

  def set(rate, text=nil)
    @prog_bar.place(:relwidth=>rate.to_f)
    @var.value = text if text
  end

  def get
    [TkPlace.current_configinfo(@prog_bar, :relwidth)['relwidth'], @var.value]
  end

  def width(size=nil)
    if size
      @size_base.width = size
      @prog_label.place(:width=>size)
      @width = size
    else
      @width
    end
  end

  def height(size=nil)
    if size
      @size_base.height = size
      @prog_label.place(:height=>size)
      @height = size
    else
      @height
    end
  end

  def font(fnt=nil)
    if fnt
      if fnt.kind_of?(TkFont)
        @base_label.font = @prog_label.font = @font = fnt
      elsif fnt.kind_of?(Hash)
        @font.configure(fnt)
      else
        fnt = TkComm.simplelist(fnt) if fnt.kind_of?(String)
        conf = {}
        conf['family'] = fnt.shift if fnt[0]
        conf['size'] = fnt.shift if fnt[0]
        fnt.each{|param|
          case param.to_s
          when 'normal', 'bold'
            conf['weight'] = param
          when 'roman', 'italic'
            conf['slant'] = param
          when 'underline'
            conf['underine'] = true
          when 'overstrike'
            conf['overstrike'] = true
          end
        }

        @font.configure(conf)
      end
    else
      @font
    end
  end
end

if __FILE__ == $0
  TkLabel.new(:text=>"Please click a progress bar.").pack

  bar1 = Tk::RbWidget::ProgressBar.new.pack
  bar2 = Tk::RbWidget::ProgressBar.new.pack(:fill=>:both, :expand=>true)
  bar3 = Tk::RbWidget::ProgressBar.new(:width=>120, :height=>30, 
                                       :barcolor=>'blue', 
                                       :troughcolor=>'white', 
                                       :fontsize=>14, :fontweight=>:bold).pack
  bar4 = Tk::RbWidget::ProgressBar.new(:font=>'courier 8 bold', 
                                       :textcolor=>'red', 
                                       :reversecolor=>'yellow', 
                                       :barcolor=>'gray25', 
                                       :width=>250, :height=>12).pack
  f = TkFrame.new.pack(:fill=>:x)
  bar5 = Tk::RbWidget::ProgressBar.new(f).pack(:side=>:right, :anchor=>:e)
  TkLabel.new(f, :text=>"bar-title:").pack(:side=>:right)

  bar1.set(0.0, '0%')
  bar2.set(0.0, '0%')
  bar3.set(0.0, '0%')
  bar4.set(0.0, '0%')
  bar5.set(0.0, 'rest 100%')

  bar1.bind('1'){puts "bar1 value = #{bar1.get.inspect}"}
  bar2.bind('1'){puts "bar2 value = #{bar2.get.inspect}"}
  bar3.bind('1'){puts "bar3 value = #{bar3.get.inspect}"}
  bar4.bind('1'){puts "bar4 value = #{bar4.get.inspect}"}
  bar5.bind('1'){puts "bar5 value = #{bar5.get.inspect}"}

  timer = TkTimer.new(50, 100){|timerobj|
    cnt = timerobj.return_value + 1
    bar1.set(cnt/100.0, "#{cnt}%")
    bar2.set(cnt/100.0, "#{cnt}%")
    bar3.set(cnt/100.0, "#{cnt}%")
    bar4.set(cnt/100.0, "#{cnt}%")
    bar5.set(cnt/100.0, "rest #{100 - cnt}%")
    cnt
  }

  b = TkButton.new(:text=>'restart', :command=>proc{
                     bar1.set(0.0, '0%')
                     bar2.set(0.0, '0%')
                     bar3.set(0.0, '0%')
                     bar4.set(0.0, '0%')
                     bar5.set(0.0, 'rest 100%')
                     timer.restart{0}
                   }).pack
  b.invoke

  Tk.mainloop
end