⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 11 - creating a gui application with tk.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
require 'tk'root = TkRoot.new { title "Tiny Tk Application" }label = TkLabel.new(root) { text "You are a trout!" }label.packTk.mainloop#---#!/usr/bin/ruby# stopwatch.rbrequire 'tk'class Stopwatch  def start    @accumulated = 0 unless @accumulated    @elapsed = 0    @start = Time.now    @mybutton.configure('text' => 'Stop')    @mybutton.command { stop }    @timer.start  end  def stop    @mybutton.configure('text' => 'Start')    @mybutton.command { start }    @timer.stop    @accumulated += @elapsed  end    def reset    stop    @accumulated, @elapsed = 0, 0    @mylabel.configure('text' => '00:00:00.0')  end   def tick    @elapsed = Time.now - @start    time = @accumulated + @elapsed    h = sprintf('%02i', (time.to_i / 3600))    m = sprintf('%02i', ((time.to_i % 3600) / 60))    s = sprintf('%02i', (time.to_i % 60))    mt = sprintf('%1i', ((time - time.to_i)*10).to_i)    newtime = "#{h}:#{m}:#{s}:#{mt}"    @mylabel.configure('text' => newtime)  end#---  def initialize    root = TkRoot.new { title 'Tk Stopwatch' }#---    menu_spec = [                    [                    ['Program'],                     ['Start', lambda { start } ],                    ['Stop', lambda { stop } ],                  ['Exit', lambda { exit } ]                 ],                 [                  ['Reset'], ['Reset Stopwatch', lambda { reset } ]                 ]                ]    @menubar = TkMenubar.new(root, menu_spec, 'tearoff' => false)    @menubar.pack('fill'=>'x', 'side'=>'top')#---    @myfont = TkFont.new('size' => 16, 'weight' => 'bold')    @mylabel = TkLabel.new(root)    @mylabel.configure('text' => '00:00:00.0', 'font' => @myfont)    @mylabel.pack('padx' => 10, 'pady' => 10)#---    @mybutton = TkButton.new(root)    @mybutton.configure('text' => 'Start')    @mybutton.command { start }    @mybutton.pack('side'=>'left', 'fill' => 'both')#---    @timer = TkAfter.new(1, -1, proc { tick })#---    Tk.mainloop  endendStopwatch.new#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -