Ruby/Tk:如何让图像获得更小的按钮部件

问题描述:

我在Mac OS X 10.6上从Ruby 1.8.7上的ActiveTcl编码Tk 8.5.9。Ruby/Tk:如何让图像获得更小的按钮部件

为了满足我的应用需求,我需要使按钮小部件像GIF图像一样小,但我无法做到。我一直在搜索和试验负面结果。

非常感谢任何线索提前。

以下是我试图从中获取小按钮的代码。

require 'tk' 
require 'tkextlib/tile' 

$up_img = TkPhotoImage.new("file"=>"arrowup-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 

但按键仍然过大:(。

这是尴尬。在OSX主题真的希望在按钮的两端添加额外的空间。

你可以尝试切换到经典按钮(在tk本身中),但它可以垂直放置更多空间并且看起来不太原生,或者您可以将图像放在标签中(您可以精确缩小)并添加绑定,以响应鼠标点击。

+0

我会尝试你的最新建议,添加绑定到标签。 – Elias 2011-03-18 11:45:44

+0

我添加了绑定到标签。工作正常。谢谢。 – Elias 2011-03-28 04:54:46

我添加了绑定到标签。工作正常。谢谢。按照带有绑定的标签的代码片段作为按钮。

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