Hi there! This is Todd again.
I have a problem with displaying images in Ruby. It just doesn’t work,
no matter what I do. I’ve tried different methods, but still no results.
Here is one particular method…
require ‘tk’
$resultsVar = TkVariable.new
root = TkRoot.new
root.title = “Window”
image = TkPhotoImage.new
image.file = “TEST.png”
label = TkLabel.new(root)
label.image = image
label.place(‘height’ => image.height,
‘width’ => image.width,
‘x’ => 10, ‘y’ => 10)
Tk.mainloop
When I try to run this particular file, I keep getting “Couldn’t open
TEST.png; No such file or directory”.
I’ve tried inserting the entire file path and I’ve tried just using it
as shown in the above example. No results. I just keep getting error
messages. I don’t know what file path it’s looking for or what directory
I’m supposed to be in. I’m very lost there.
I’ve also tried this method…
#!/usr/bin/env ruby
require ‘tk’
require “tkextlib/tkimg/jpeg”
require “open-uri”
photo = open(“IMAGE URL GOES HERE”, “rb”) {|io| io.read}
TkRoot.new {title “Fox” }
TkLabel.new {
image TkPhotoImage.new( :data => Tk::BinaryString( photo ) )
width 300
pack
}
TkLabel.new {
font TkFont.new( ‘verdana 24 bold’ )
text “The Great Fox Was Here!”
pack
}
TkButton.new {
text ‘Quit’
command ‘exit’
pack
}
Tk.mainloop
It will not load anything for this. It keeps telling me “TkPackage
can’t find package img::jpeg”. I’ve tried a few different file types,
but none work, including GIF and PNG. I’ve checked the code over and
over. It’s all precisely like it was in the book. I only changed the
text and tried to use other images from the internet. I also uploaded an
image to my own website, and that didn’t work either.
So I have two problems here (Can’t use images from my computer or the
internet) and no solutions. Any help? It would be much
appreciated.
By the way, I figured out how to get TK working, so it’s not that.
- Todd