2011-03-18 71 views
0

我在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 

但按鍵仍然過大:(。

回答

1

這是尷尬。在OSX主題真的希望在按鈕的兩端添加額外的空間。

你可以嘗試切換到經典按鈕(在tk本身中),但它可以垂直放置更多空間並且看起來不太原生,或者您可以將圖像放在標籤中(您可以精確縮小)並添加綁定,以響應鼠標點擊。

+0

我會嘗試你的最新建議,添加綁定到標籤。 – Elias 2011-03-18 11:45:44

+0

我添加了綁定到標籤。工作正常。謝謝。 – Elias 2011-03-28 04:54:46

0

我添加了綁定到標籤。工作正常。謝謝。按照帶有綁定的標籤的代碼片段作爲按鈕。

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