2011-03-18 1 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

+0

라벨에 바인딩을 추가했습니다. 잘 작동합니다. 감사. – Elias

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