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

发布于 2024-10-22 07:38:10 字数 717 浏览 4 评论 0原文

我正在 Mac OS X 10.6 上的 Ruby 1.8.7 上使用 ActiveTcl 在 Tk 8.5.9 上进行编码。

为了满足我的应用程序要求,我需要使按钮小部件与 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

但按钮仍然太大:(。

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-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 :(.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-10-29 07:38:10

很尴尬。 OSX 主题确实希望在按钮的两端添加额外的空间。

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

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.

假装爱人 2024-10-29 07:38:10

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文