Ruby Shoes GUI: Continually Updating Paragraphs

爷,独闯天下 提交于 2019-11-28 10:05:51

问题


The Shoes GUI kit for Ruby seems like a really nice and easy way to add a GUI to my various scripts, but after banging my head against it, I can't seem to make a paragraph be continually updated with a loop.

Here's my minimal code to show what I'm trying to do:

Shoes.app do
    stack do
        @exit = button "Exit"
        @log = stack { para "Logging goes here..." }
        @exit.click { exit }

    end

    loop do
        sleep 1
        contents = `ls`
        @log.append { para contents }
    end

end

But this just blocks forever and my GUI never shows up until I kill the ruby process, at which time all my info appears.

I've tried putting the "contents" checking loop and append in a separate class, in its own "stack" or "flow" loop, tried passing @log to a separate class's method as per the "Block Redirection" header in the Shoes Rules (http://shoesrb.com/manual/Rules.html), still no joy after trying everything I can think of. Any ideas how I can get this working? I'm thinking I just don't have a complete grasp on how Shoes sets up the GUI.


回答1:


If you're looking to list all files in a directory and refresh the list every second, then I think this is what you're looking for:

Shoes.app do

  stack do
    @btn_exit = button("Exit") {exit}
    @log = para "Logging goes here..."
  end

  every 1 do
    @log.text = Dir.entries('C:/Test').select{|file| file != "."*file.length}.join("\n")
  end

end



回答2:


it seems like you need the every method (search for it here). The every method will call your code every second (or as many second as you pass in).

Should work like this:

every do
  # your appending code
end

(might need 1 as an argument, not 100% sure atm)



来源:https://stackoverflow.com/questions/18067381/ruby-shoes-gui-continually-updating-paragraphs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!