加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ryby/tk 小时钟

发布时间:2020-12-17 04:12:18 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/env ruby# -*- coding: utf-8 -*-require 'observer'require 'thread'require 'tk'=begin ruby/tk简单的时钟=endclass Clock #观察者模式

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'observer'
require 'thread'
require 'tk'
=begin
     ruby/tk简单的时钟
=end

class Clock
  #观察者模式
  include Observable
  def getPointAngle(time)
    #获取以y轴为线顺时针的角度,例如:3点钟则时针的角度为90度
    sec_angle = time.sec / 60.0 * 360 
    min_angle = time.min / 60.0 * 360 + sec_angle / 360 / 60
    hour_angle = time.hour.divmod(12)[1] / 12.0 * 360 + min_angle / 360 * 30
    #转换成以xy轴的角度,例如3点钟,则时针的角度为0度,12点时针的角度为180度
    return [hour_angle,min_angle,sec_angle].collect do |x|
      x <= 90 ? 90 -x : 450 - x
    end
  end

  def run()
    #一秒种刷新一次界面
    loop do
      angles = self.getPointAngle(Time.now)
      changed()      
      notify_observers(angles)
      sleep 1
    end
  end
end

class ClockView
  LENGTH_ARRAY = [40,50,70]
  def initialize(root)
    @cur_sec_line = nil
    @cur_hour_line = nil
    @cur_min_line = nil
    @canvas = TkCanvas.new(root)
    timg = TkPhotoImage.new('file' => 'w.gif')
    t = TkcImage.new(@canvas,100,'image' => timg)
    @canvas.pack('side' => 'left','fill' => 'both')
  end
  def update(angles)    
    coords = Array.new
    #将角度转换成在界面上的坐标
    angles.to_a().each_with_index do |mangle,index|
      cy = Math.sin(mangle / 180 * Math::PI) * LENGTH_ARRAY[index]
      cx = Math.cos(mangle / 180  * Math::PI) * LENGTH_ARRAY[index]
      cx = cx + 100
      cy = 100 - cy
      coords[index] = [cx,cy]
    end
    @cur_sec_line != nil and @cur_sec_line.delete()
    @cur_min_line != nil and @cur_min_line.delete()
    @cur_hour_line != nil and @cur_hour_line.delete()

    hline = TkcLine.new(@canvas,coords[0][0],coords[0][1],"width" => "3")
    mline = TkcLine.new(@canvas,coords[1][0],coords[1][1],"width" => "2")
    sline = TkcLine.new(@canvas,coords[2][0],coords[2][1],"width" => "1")

    [hline,mline,sline].map { |aline| 
      aline.fill 'yellow' 
    }

    @cur_sec_line = sline
    @cur_hour_line = hline
    @cur_min_line = mline
  end
end

root = TkRoot.new do
  title '怀旧时钟'   
  geometry "200x200+1000+80"
end
clock = Clock.new()
clock_view = ClockView.new(root)
clock.add_observer(clock_view)
Thread.new {
  clock.run
}
Tk.mainloop 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读