Ruby Shoes GUI: Continually Updating Paragraphs

2019-01-28 17:05发布

问题:

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)



标签: ruby shoes