3

在鞋中创建按钮后,是否可以更改文本?我尝试修改按钮样式中的 :text 键——@button.style.inspect 确认文本已更改——但按钮仍显示原始文本。

4

3 回答 3

2

我还没有弄清楚如何更改现有按钮上的文本。我怀疑它只是目前还不支持。您可以创建一个新按钮并替换旧按钮。不幸的是,至少在 Windows 上,移除一个按钮会破坏所有的点击事件。我还没有在其他平台上尝试过,但也许它会起作用。尝试这样的事情:

Shoes.app do
  para 'This is some text.'

  @btn = button 'a' do |btn|
    alert 'Hello, World!'
  end

  para 'Blah blah blah'

  button 'Change!' do |btn|
    old = @btn
    new_style = old.style.dup
    txt = new_style[:text].next!
    old.parent.before(old) do
      @btn = button txt, new_style
    end
    old.remove #This messes up the click events on Windows.
  end

end
于 2009-08-10T13:40:40.420 回答
0

一个非常古老的问题,但有一个解决方案。你没有提到你的鞋子颜色,所以我用绿色。绿鞋是基于 GTK2 的,所以如果你像这样提取 GTK2 对象,你可以使用 GTK2 的方法。

require 'green_shoes'

Shoes.app do
  @btn = button('old text ') {|btn|alert('Hello, World!')}
  button('Change!') {|btn|@btn.real.set_label("new")}
end
于 2014-09-14T22:20:21.727 回答
0

我找到了一种模拟更改按钮文本的方法,方法是将其替换为与旧按钮具有相同操作的新按钮。

按钮首先从其容器(堆栈或流)中删除,然后添加一个新的。这个新按钮将出现在容器的末尾,因此可能需要为此按钮设置一个堆栈或流。

# Display 2 buttons, A and B.  Initially button A has text "a".
# Pressing the top button, A, just shows the value of @ch (which is
# the text of the button) on the console.
# Pressing the second button appears to change the text of button A.
# It actually replaces it with a new button with the same action as the old.
Shoes.app do
  def button_a_function
    info("click!  #{@ch}")
  end
  @ch = "a"
  @the_flow = flow do
    para "Button A, whose to be changed"
    @b1 = button @ch do
      button_a_function
    end
  end
  flow do
    para "Button B"
    @b2 = button("Change it") {
      @ch.succ!
      @b1.remove
      @b1 = @the_flow.button (@ch) {
        button_a_function
      }
    }
  end
end
于 2021-05-20T00:43:00.513 回答