LoginSignup
36
38

More than 5 years have passed since last update.

いまさらはじめる Ruby/Tk

Last updated at Posted at 2015-07-31

概要

マルチプラットフォームで使えてブラウザを使わず、大きめのライブラリを入れなくても良い中小零細企業利用レベル想定のRubyで手軽に作れるGUIソフト開発をしたいということでRuby/Tkをはじめてみました。参考にしているページがRuby1.8系での説明だったり、キャプチャの画像がVineLinuxだったりと、とても楽しそうな予感がしてます(^_^)
ここではRuby/Tkの環境構築と簡単なアプリ作成まで。詳細はRuby/Tk覚え書きで書きます。

環境構築

※ この記事にコメントいただきましたように、Ruby2.4以降でtkを使う場合は別途インストールする必要があります。

gem install tk

Mac

  • ruby
    • rbenvを入れてruby2.2.2をセットしました
  • Tcl/Tk
    • /usr/bin/wishが有ることを確認します
    • バージョンは8.5
バージョンの確認
$ wish
% puts $tk_version
8.5

Ubuntu trusty(14.04)

  • ライブラリのインストール
ライブラリのインストール
$ sudo apt-get update
$ sudo apt-get install tk tcl tk8.6-dev tcl8.6-dev
  • ruby
    • 以下のようにrbenvでruby2.2.2をインストール
rubyインストール
$ RUBY_CONFIGURE_OPTS="--with-tcltkversion=8.6 \
--with-tcl-lib=/usr/lib/x86_64-linux-gnu \
--with-tk-lib=/usr/lib/x86_64-linux-gnu \
--with-tcl-include=/usr/include/tcl8.6 \
--with-tk-include=/usr/include/tcl8.6 \
--enable-pthread" rbenv install 2.2.2 -v
$ rbenv global 2.2.2
$ rbenv rehash
  • Tcl/Tk
    • /usr/bin/wishが有ることを確認します
    • バージョンは8.6

動作確認

Tcl/Tkの動作確認

helloというボタンを表示して押したら消える

hello
$ wish
% button .btn -text "hello" -command "exit"
.btn
% pack .btn  ### これを実行するとhelloとかかれた窓が表示される

スクリーンショット 2015-07-31 8.46.32.png
helloを押すとWindowが消えます。

Ruby/Tkの動作確認

exitというボタンを表示して押したら消える

exit.rb
$ vi exit.rb
====================
require 'tk'

button = TkButton.new   ### TkButtonというRuby/Tkのウィジェットをインスタンス化。
button.text = 'exit' ### ボタンに表示するテキストを設定
button.command = proc { exit }   ### ボタンが押されたときに終了する
button.pack   ### ボタンをウインドウにパッキングする(ウインドウにボタンを載せる)

Tk.mainloop ### Tkのメインループを実行
====================

$ ruby exit.rb

スクリーンショット 2015-07-31 8.58.17.png
exitを押すとWindowが消えます。

作ってみる

参考にある「チュートリアルRuby/Tk」そのまま引用。

落書き帳

rakugaki.rb
require "tk"

size = 5

c1 = TkCanvas.new {|c|
  width 200
  height 200
  bind 'B1-Motion', proc { |x,y|
    #TkcRectangle.new(c, x, y, x+size, y+size) {
    TkcOval.new(c, x, y, x+size, y+size) {
      outline 'blue'
      fill 'blue'
      tag 'p'
    }}, "%x %y"
  pack
}

TkButton.new {
  text "Clear"
  command { c1.delete('p') }
  pack
}

Tk.mainloop

以下のように青いラインで絵がかけます。clearを押すと描いた絵が消えます。
スクリーンショット 2015-08-01 16.50.56.png

電卓

dentaku.rb
require 'tk'

e = TkEntry.new { relief 'sunken' }.pack('fill' => 'x')

r = 0.25

TkFrame.new { |f|
  "789*456/123-0+=C".split(//).each_with_index do |w, i|
  TkButton.new(f) {
    text "#{w}"
    bind('1', proc { e.value = eval e.value }) if w == "="
    bind('1', proc { e.value = "" }) if w == "C"
    command { e.insert('end', w) }  if w != "C" && w != "="
    place('relx' => (i % 4) * r, 'rely' => i / 4 * r,
          'relw' => r, 'relh' => r)
  }
  i += 1
  end
  height 120
  width 120
}.pack('fill' => 'x')

Tk.mainloop

スクリーンショット 2015-08-01 16.58.49.png

3D plot

3dplot
require 'tkextlib/tcllib/plotchart'
include Tk::Tcllib::Plotchart

[[:yellow,:red],[:green,:darkgreen]].each{|c|
s = Plot3D.new([0, 10, 3], [-10, 10, 10], [0, 7.5, 2.5],width:500,height:400){
  title "3D Plot"
  grid_size(80,80)
  color(c[0],c[1])
  plot_function{|x,y|
    x1 = x.to_f/9.0
    y1 = y.to_f/9.0
    3.0 * (1.0-(x1*x1+y1*y1))*(1.0-(x1*x1+y1*y1))
  }
}.pack(side: :left)
}
Tk.mainloop

スクリーンショット 2015-08-10 8.31.37.png

このようにちょっとしたものならすぐ作れます。

参考

36
38
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
38