开发者

Ruby/Tk: how to get smaller button widget with image

开发者 https://www.devze.com 2023-02-17 08:25 出处:网络
I\'m coding on Tk 8.5.9 from ActiveTcl on Ruby 1.8.7 on a Mac OS X 10.6. To meet my application requirements I need to make the button widgets as small as the gif image but I am not able to. I have b

I'm coding on Tk 8.5.9 from ActiveTcl on Ruby 1.8.7 on a Mac OS X 10.6.

To meet my application requirements I need to make the button widgets as small as the gif image but I am not able to. I have been hours searching and experimenting with negative results.

Greatly thankful in advance for any clues.

Following is the code i am trying to get small buttons from.

require 'tk'
require 'tkextlib/tile'

$up_img = TkPhotoImage.new("file"=>"arrowup-开发者_C百科n.gif")
$down_img = TkPhotoImage.new("file"=>"arrowdown-n.gif")

root = TkRoot.new {title "Ek Composer"}

content = Tk::Tile::Frame.new(root).pack
Tk::Tile::Button.new(content) {width 1;image $up_img;  command {move_up}  }.pack
Tk::Tile::Button.new(content) {width 1;image $down_img;command {move_down}}.pack

def move_up
    p "move up"
end

def move_down
    p "move down"
end

Tk.mainloop

But the buttons remain too big :(.


It's awkward. The OSX theme really wants to add extra space at either end of the button.

You can try switching to the classic button (in tk itself) but that puts more space vertically and looks a bit less native. Or you can put the image in a label (which you can shrink exactly) and add bindings to it to make it respond to mouse clicks.


I added binding to label. Works fine. Thanks. Follows a code snippet of label with binding as button.

require 'tk'


$resultsVar = TkVariable.new
root = TkRoot.new
root.title = "Window"

$up_img = TkPhotoImage.new("file"=>"arrowup-n.gif")
$down_img = TkPhotoImage.new("file"=>"arrowdown-n.gif")

Lbl = TkLabel.new(root) do
  image $up_img
  borderwidth 0
  font TkFont.new('times 20 bold')
  foreground  "red"
  relief      "groove"
  pack("side" => "right",  "padx"=> "50", "pady"=> "50")
end

Lbl.bind("ButtonPress-1")  {
    Lbl.configure("image"=>$down_img) 
}

Lbl.bind("ButtonRelease-1") { 
    Lbl.configure("image"=>$up_img) 
}

Lbl['textvariable'] = $resultsVar
$resultsVar.value = 'New value to display'

Tk.mainloop
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号