1#!/usr/bin/env ruby 2 3require "tk" 4 5TkMessage.new(:width=>360, :text=><<EOM).pack 6This sample shows how to use a binary sequence between Ruby and Tk. \ 7This reads the image data from the file as the binary sequence. 8 9To treat the difference of encodings between on Ruby and on Tk seamlessly, \ 10Ruby/Tk converts the encoding of string arguments automatically. \ 11I think it is comfortable for users on almost all situations. \ 12However, when treats a binary sequence, the convert process makes troubles. 13 14Tk::BinaryString class (subclass of Tk::EncodedString class) is the class \ 15to avoid such troubles. Please see the source code of this sample. \ 16A Tk::BinaryString instance is used to create the image for the center button. 17EOM 18 19ImgFile=[File.dirname(__FILE__), 'images','tcllogo.gif'].join(File::Separator) 20 21ph1 = TkPhotoImage.new(:file=>ImgFile) 22p ph1.configinfo 23 24b_str = Tk::BinaryString(IO.read(ImgFile)) 25p [b_str, b_str.encoding] 26 27ph2 = TkPhotoImage.new(:data=>b_str) 28p ph2.configinfo 29p ph2.data(:grayscale=>true) 30 31ph3 = TkPhotoImage.new(:palette=>256) 32ph3.put(ph2.data) 33 34ph4 = TkPhotoImage.new() 35ph4.put(ph2.data(:grayscale=>true)) 36 37#p [b_str.encoding, b_str.rb_encoding] 38 39f = TkFrame.new.pack 40TkButton.new(:parent=>f, :image=>ph1, :command=>proc{exit}).pack(:side=>:left) 41TkButton.new(:parent=>f, :image=>ph2, :command=>proc{exit}).pack(:side=>:left) 42TkButton.new(:parent=>f, :image=>ph3, :command=>proc{exit}).pack(:side=>:left) 43TkButton.new(:parent=>f, :image=>ph4, :command=>proc{exit}).pack(:side=>:left) 44 45Tk.mainloop 46