Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby, tk lib. Output from getopenfile

I am trying to load multiple files via ruby/tk lib and put them into array:

def openFiles
return Tk.getOpenFile(  'title' => 'Select Files',
                        'multiple' => true, 
                        'defaultextension' => 'csv',
                        'filetypes' => "{{Comma Seperated Values} {.csv}} {TXT {.txt}} {All files {.*}}")
end

and then in code

filess = TkVariable.new()

button1 = TkButton.new(root){
text 'Open Files'
command (proc {filess.value = openFiles; puts filess; puts filess.class; puts filess.inspect})

}.grid(:column => 1, :row => 1, :sticky => 'we')

The problem is that I can not manage to get the output as array and I do not know if it is possible or I will have to somehow parse the output. Hm? Please help. Thank you.

this is the output, when I click on the button:

C:\file1
C:\file2
TkVariable
#<TkVariable: v00000>

I think it should be: (for the array part)

['C:\file1','C:\file2']
like image 232
user_pruser Avatar asked Aug 15 '26 17:08

user_pruser


1 Answers

TkVariable implements #to_a, which you can use to convert its value into the Array you want.

button1 = TkButton.new(root) {
  text 'Open Files'
  command (proc do
    filess.value = openFiles
    puts filess.to_a.class
    puts filess.to_a.inspect
  end)
}.grid(:column => 1, :row => 1, :sticky => 'we')
Array
["C:\file1", "C:\file2"]
like image 151
Kristján Avatar answered Aug 17 '26 12:08

Kristján



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!