Standard File Dialogs

411 views
Skip to first unread message

Eric S

unread,
Feb 24, 2015, 6:22:19 PM2/24/15
to julia...@googlegroups.com
I'm enjoying Julia, but one thing I'm missing is standard file open and file save dialogs. I've found the Gtk package and tried that. It does indeed generate a file dialog, but it is really ugly and nonstandard (on OS X) and also generates the following error like messages in the terminal (below). Perhaps there is something wrong with my installation. 

Is there something like how R works where you can just type "> file.choose()" and a nice file picker dialog comes up.

-Eric

Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): Gtk-WARNING **: Error loading theme icon 'image-missing' for stock: Icon 'image-missing' not present in theme


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed


(<unknown>:2678): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed

Eric S

unread,
Feb 24, 2015, 6:56:21 PM2/24/15
to julia...@googlegroups.com
I did try the Tk package instead. The message dialog look a lot better, but there is nothing in the documentation which suggests how to get at Tk's askopenfile or other dialogs. Any help would be appreciated.

Eric

j verzani

unread,
Feb 24, 2015, 7:55:52 PM2/24/15
to julia...@googlegroups.com
You can try `GetOpenFile`, `GetSaveFile`, or `ChooseDirectory` in the Tk.jl package. They all return a file name or "" if canceled. If you have other calls you want, these are just simple wrappers. For example, `GetOpenFile() = tcl("tk_getOpenFile")`.

Jameson Nash

unread,
Feb 24, 2015, 8:16:27 PM2/24/15
to julia...@googlegroups.com
It's nice to see that Tk's usual inflexibility as a toolkit actually serves it well here on OS X, and it just uses the native dialog boxes. Gtk has better themes, although it's stock look does leave a bit to be desired on OSX and the homebrew installation seems to have failed at the theme installation/configuration step, resulting in the non-fatal warnings that you observed (please file a bug against this on Gtk.jl – I use a MacPorts-installed gtk, so I don't see this same issue).

I'm hoping someday there will be a Julia package that provides simple access to each platforms basic standard (native) dialog boxes for open/save/etc.

Steven G. Johnson

unread,
Feb 24, 2015, 10:16:25 PM2/24/15
to julia...@googlegroups.com
Yes, something like the EasyDialogs Python package would be great.

Steven G. Johnson

unread,
Feb 24, 2015, 10:18:19 PM2/24/15
to julia...@googlegroups.com
On Tuesday, February 24, 2015 at 10:16:25 PM UTC-5, Steven G. Johnson wrote:
Yes, something like the EasyDialogs Python package would be great.

(EasyDialogs doesn't work anymore and was never cross-platform, but that sort of interface seems useful.)

Eric S

unread,
Feb 25, 2015, 10:09:33 AM2/25/15
to julia...@googlegroups.com
Thanks,

This is exactly what I want. The documentation for the Tk package doesn't list all the available methods/functions. There needs to be a more discoverable method of finding them than asking in the user group.

Eric

Tim Holy

unread,
Feb 25, 2015, 10:27:14 AM2/25/15
to julia...@googlegroups.com
By all means, please improve the documentation!

--Tim

Eric S

unread,
Feb 25, 2015, 10:35:57 AM2/25/15
to julia...@googlegroups.com
I guess I would if I had the knowledge. I was hoping the author of the package might help here since I'm assuming that person could do it much more efficiently.

Eric

Tim Holy

unread,
Feb 25, 2015, 11:08:41 AM2/25/15
to julia...@googlegroups.com
It's a mix: the author has a lot of implicit knowledge that's s/he takes for
granted, and that makes it harder to write good documentation. In combination
with edits from the package author, a user who is just learning a package is
the _perfect_ person to write documentation.

The other point is that developers who release code are giving a gift to the
community, and then being extra nice by answering questions on the mailing
list. Asking the developers to do even more is, in a sense, asking too much.
In my opinion, it's simply a fair exchange if the asker then takes that
information that s/he has received, polishes it up as necessary, and adds it
to the README. I help you, you help me.

Converting "one answered question on the user list" into "one community-
contributed improvement in the documentation" will go a long, long ways to
improving the documentation for future users---if everyone did that, we would
erase our documentation problems very quickly.

--Tim

Tony Kelman

unread,
Feb 25, 2015, 12:39:57 PM2/25/15
to julia...@googlegroups.com
Here's a version for Windows that should just work (tm) on Vista-and-newer where Powershell is installed by default:

chomp(readall(`powershell -Command "[System.Reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\") | Out-Null;
    \$dialog = New-Object System.Windows.Forms.OpenFileDialog;
    \$dialog.InitialDirectory=\"$(pwd())\";
    \$dialog.ShowDialog() | Out-Null;
    \$dialog.FileName"`))

It even works on XP if you've manually installed powershell at some point. Replace pwd() with desired starting location.

Not sure if anything quite as short is possible on OSX or Linux.

-Tony

Eric S

unread,
Feb 25, 2015, 1:44:28 PM2/25/15
to julia...@googlegroups.com
Is there some documentation describing how to add to the documentation? I'd be happy to do so if I had a clue how.

Eric

Tim Holy

unread,
Feb 25, 2015, 1:53:41 PM2/25/15
to julia...@googlegroups.com
Sure, just click on the source file (e.g., README.md) and then on the little
pencil icon in the upper right hand corner.

See also https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md

Thanks for tackling this!
--Tim

Tony Kelman

unread,
Feb 25, 2015, 1:56:18 PM2/25/15
to julia...@googlegroups.com
For Tk.jl, it looks like the documentation is here - https://github.com/JuliaLang/Tk.jl/blob/master/examples/README.md

You can click the little pencil-looking icon to make suggested edits to the markdown document in Github's web interface, and directly create a pull request that way.

Tobias Knopp

unread,
Feb 25, 2015, 3:41:51 PM2/25/15
to julia...@googlegroups.com
The error message is actually because the Gtk version was bumped to a new version that comes with a nice default theme. Still not native but looking better. The error is because there either the data for the icons is not installed or some path is not properly said.


Cheers,

Tobi

Eric S

unread,
Feb 27, 2015, 9:57:57 AM2/27/15
to julia...@googlegroups.com
Tim,

I did edit the documentation README as suggested. I've discovered (I think), that while one can call the various file dialogs, none of the options are supported such as specifying file extensions, etc. I'm gathering those just aren't coded yet. Is there a obvious way to make this request?

Eric

Tim Holy

unread,
Feb 27, 2015, 10:26:41 AM2/27/15
to julia...@googlegroups.com
Thanks for the documentation improvements!

I think such things are implemented for Gtk.jl, but perhaps not Tk.jl. You can
always file an issue with the package; however, it seems that Tk.jl has
received less attention now that Gtk.jl is mature, so who knows when it might
be implemented. You can, of course, always take a shot at implementing it
yourself. That CONTRIBUTING.md document outlines the logistics (i.e.,
everything except writing the code).

Best,
--Tim

j verzani

unread,
Feb 27, 2015, 11:25:20 AM2/27/15
to julia...@googlegroups.com
The Tk dialogs can be redefined easily. For example, instead of this line (https://github.com/JuliaLang/Tk.jl/blob/master/src/dialogs.jl#L4)

GetOpenFile() = tcl("tk_getOpenFile")

You had

GetOpenFile(;kwargs...) = tcl("tk_getOpenFile"; kwargs...)

Then you can pass in the named arguments such as

GetOpenFile(initialdir="/tmp")

You did a good job on the docs, want to put this together for the function definitions?

Eric S

unread,
Feb 27, 2015, 1:06:50 PM2/27/15
to julia...@googlegroups.com
I did see that code and tried a bit to make it work, but didn't get anywhere. I was clueless how to add the keywords. Strings? I could see it should be doable but not how to do it. I might be able to figure something out if I had some examples. I should note that I'm competent in MATLAB and dabble in Python but otherwise I'm not a programmer as my background is in Mechanical Engineering. 30 years ago I learned programming in FORTRAN. I've just started playing with Julia and I'm enjoying it. I'm hoping to displace MATLAB. I was into Python with Numpy/Scipy but that felt sort of bolted on and not as elegant. If there was someone willing to help me along, I could endeavor to add this functionality.

Eric

Eric S

unread,
Feb 27, 2015, 1:31:19 PM2/27/15
to julia...@googlegroups.com
I see from the definition of the Messagebox function much of what I might do. I'm having a real problem figuring out how to define the filetypes however. I looked to Python's tk interface for file dialogs for a clue, but still got stuck.

Eric

Milan Bouchet-Valat

unread,
Feb 27, 2015, 1:53:00 PM2/27/15
to julia...@googlegroups.com
Le vendredi 27 février 2015 à 10:31 -0800, Eric S a écrit :
> I see from the definition of the Messagebox function much of what I
> might do. I'm having a real problem figuring out how to define the
> filetypes however. I looked to Python's tk interface for file dialogs
> for a clue, but still got stuck.
This seems to work:
tcl("tk_getOpenFile", filetypes=["Text files {.txt}", "PNG files {.png}"])

Thus a definition like this seems to be fine:
GetOpenFile(; filetypes="") = filetypes == "" ?
tcl("tk_getOpenFile") :
tcl("tk_getOpenFile", filetypes=filetypes)


But if you want to support the other keyword arguments ( -initialdir,
-initialfile, -multiple and -title I think), you'll need a better logic,
or the number of possible combinations will explode. There's probably a
trick to avoid passing arguments which have been left to their default
value.

One might also prefer replacing "Text files {.txt}" with something more
Julian, like "Text files" => ".txt" and/or
("Text files", ".txt").


Regards

Eric S

unread,
Feb 27, 2015, 3:16:37 PM2/27/15
to julia...@googlegroups.com
Milan,

Thanks for the hint. When I try the following call on my computer (OS X 10.10.2) it invokes the open file dialog (native too), but all the files are grayed out and not selectable.

julia> z=tcl("tk_getOpenFile", filetypes=["Text files {.txt}"])


If I leave out the filetypes, I can select any files. As for the implementation, you can pass in as few or as many keyword arguments in a Dict. I think just allowing the user to set up the Dict is easier than exposing each of the keywords separately in the function call just because there are so many of them. The following works for instance:


julia> kwarg = Dict();


julia> kwarg["title"]="Pick a file";


julia> kwarg["multiple"]=true;


julia> z=tcl("tk_getOpenFile", kwarg)


Eric

>                  ...

Milan Bouchet-Valat

unread,
Feb 27, 2015, 4:20:51 PM2/27/15
to julia...@googlegroups.com
Le vendredi 27 février 2015 à 12:16 -0800, Eric S a écrit :
> Milan,
>
>
> Thanks for the hint. When I try the following call on my computer (OS
> X 10.10.2) it invokes the open file dialog (native too), but all the
> files are grayed out and not selectable.
>
>
> julia> z=tcl("tk_getOpenFile", filetypes=["Text files {.txt}"])
Sorry, I didn't pay attention to the actual result. This actually needs
to be "{Text files} {.txt}". Probably another argument in favor of
writing it as "Text files" => ".txt".

> If I leave out the filetypes, I can select any files. As for the
> implementation, you can pass in as few or as many keyword arguments in
> a Dict. I think just allowing the user to set up the Dict is easier
> than exposing each of the keywords separately in the function call
> just because there are so many of them. The following works for
> instance:
>
>
> julia> kwarg = Dict();
>
>
> julia> kwarg["title"]="Pick a file";
>
>
> julia> kwarg["multiple"]=true;
>
>
> julia> z=tcl("tk_getOpenFile", kwarg)
No, I don't think forcing the user to explicitly create a dict is a good
idea. Keyword arguments are precisely here to avoid this. If you don't
want to take care of the various possible combinations of keywords, you
can always write the function simply as:
GetOpenFile(; kwargs...) = tcl("tk_getOpenFile"; kwargs...)

But I'd say it's clearer for users to list all the accepted keyword
arguments, and raise an error when passing an unsupported one. In R, the
help page for tkgetOpenFile() and for many Tcl/Tk functions looks like
this:
tkgetOpenFile(...)
tkgetSaveFile(...)
tkchooseDirectory(...)
tkmessageBox(...)
tkdialog(...)
tkpopup(...)

This is a nightmare when you don't remember the exact name of an
argument, and auto-completion doesn't work.

Here's a possible way of writing this function, but it's relatively
verbose:
function GetOpenFile(; filetypes=nothing, initialdir=nothing,
initialfile=nothing, multiple=nothing, title=nothing)
args = Dict{Symbol, Any}()
filetypes != nothing && push!(args, :filetypes=>filetypes)
initialdir != nothing && push!(args, :initialdir=>initialdir)
initialfile != nothing && push!(args, :initialfile=>initialfile)
multiple != nothing && push!(args, :multiple=>multiple)
title != nothing && push!(args, :title=>title)
tcl("tk_getOpenFile"; args...)
end

Any other ideas to handle this situation (which should be quite common
in Tcl/Tk)? Maybe `to_tcl(::Pair)` should return `""` when the value is
`nothing`, so that default values can be passed directly without any
effect?

Eric S

unread,
Feb 27, 2015, 5:40:53 PM2/27/15
to julia...@googlegroups.com
Milan,

I'm just learning Julia so some of what you wrote isn't familiar to me yet. For instance I'm not familiar with this construct: "Text files" => ".txt". In any case, I still can't get the filetypes keyword to work on my Mac. Even with your latest suggestion, all the files end up grayed out. It may be that the Mac dialogs don't support the use of filetypes in this context. I'm supposing there must be some function for determining the computer type in case we need to make some keywords conditional on platform.

Thanks,

Eric
>      ...

Eric S

unread,
Feb 27, 2015, 5:44:13 PM2/27/15
to julia...@googlegroups.com
A correction, the newest suggestion for filetypes does indeed work on my Mac, I just needed to find a folder with a proper file ending in ",txt"!

Eric
...

Eric S

unread,
Feb 27, 2015, 6:48:10 PM2/27/15
to julia...@googlegroups.com
Well, I'm trying but not getting too far. I tried the code as suggested by Milan but got errors. I then tried to mimic what I say in the Messagebox code in the Tk package. This is what I have so far:

function GetOpenFile2(; filetypes::Array=[], initialdir::String="",
                       initialfile
::String="", multiple::Bool=[], title::String="")
  args
= Dict()
 
if length(filetypes) > 0 args["filetypes"] = filetypes end
 
if length(initialdir) > 0 args["initialdir"] = initialdir end
 
if length(initialfile) > 0 arg["initialfile"] = initialfile end
 
if length(multiple) > 0 args["multiple"] = multiple end
 
if length(title) > 0 args["title"] = title end
  tcl
("tk_getOpenFile"; args)
end

I coded this in Juno but got an error message that keyword args needs a default value. Not sure why since it doesn't have one in Messagebox. Not sure how to proceed.

Eric
...

Milan Bouchet-Valat

unread,
Feb 28, 2015, 8:55:52 AM2/28/15
to julia...@googlegroups.com
Le vendredi 27 février 2015 à 15:48 -0800, Eric S a écrit :
Well, I'm trying but not getting too far. I tried the code as suggested by Milan but got errors. I then tried to mimic what I say in the Messagebox code in the Tk package. This is what I have so far:


function GetOpenFile2(; filetypes::Array=[], initialdir::String="", 
                       initialfile::String="", multiple::Bool=[], title::String="") 
  args = Dict()
  if length(filetypes) > 0 args["filetypes"] = filetypes end
  if length(initialdir) > 0 args["initialdir"] = initialdir end 
  if length(initialfile) > 0 arg["initialfile"] = initialfile end 
  if length(multiple) > 0 args["multiple"] = multiple end 
  if length(title) > 0 args["title"] = title end 
  tcl("tk_getOpenFile"; args) 
end 


I coded this in Juno but got an error message that keyword args needs a default value. Not sure why since it doesn't have one in Messagebox. Not sure how to proceed.
I don't get this error, but a possible problem is that multiple::Bool=[] is not correct: [] is not a Bool. You can simply set it to false, which is more explicit anyway, and that way you don't even need to check length(multiple) > 0.

As regards =>, that's a new syntax on 0.4 which creates a Pair object holding both values [1]. On 0.3, using a tuple is probably fine. But it doesn't look like to_tcl() handles tuples and pairs in a very useful way at the moment:
julia> Tk.to_tcl(("a", "b"))
"(\"a\",\"b\")"

julia> Tk.to_tcl("a"=>"b")
"\"a\"=>\"b\""


Regards


1: https://github.com/JuliaLang/julia/pull/8521

Eric S

unread,
Feb 28, 2015, 9:58:45 AM2/28/15
to julia...@googlegroups.com
Milan,

My problem was a semicolon instead of a comma in the call to tcl("tk_getOpenFile", args). The function now seems to work. I can work up versions for GetSaveFile and ChooseDirectory. Once they seem to work, what is the process for suggesting they be included in the Tk package?

function GetOpenFile2(; filetypes::Array=[], initialdir::String="",

                       initialfile
::String="", multiple=false, title::String="")

  args
= Dict()
 
if length(filetypes) > 0 args["filetypes"] = filetypes end
 
if length(initialdir) > 0 args["initialdir"] = initialdir end

 
if length(initialfile) > 0 args["initialfile"] = initialfile end
  args
["multiple"] = multiple
 
if length(title) > 0 args["title"] = title end
  tcl
("tk_getOpenFile", args)
end

In [46]:
GetOpenFile2(filetypes=["{Text files} {.txt}","{CSV files} {.csv}"],
title="Select File",initialdir="/Users/ericshain/Documents/")
Out[46]:
"/Users/ericshain/Documents/MATLAB/Test Data/CMV/11910_FP_PCR_3192010135222_MC.csv"


Milan Bouchet-Valat

unread,
Feb 28, 2015, 10:05:57 AM2/28/15
to julia...@googlegroups.com
Le samedi 28 février 2015 à 06:58 -0800, Eric S a écrit :
Milan,


My problem was a semicolon instead of a comma in the call to tcl("tk_getOpenFile", args). The function now seems to work. I can work up versions for GetSaveFile and ChooseDirectory. Once they seem to work, what is the process for suggesting they be included in the Tk package?
You can open a pull request against that package.

Before that, I think you should change
filetypes::Array=[]
to
filetypes::Vector=[]
(or even
function GetOpenFile{S<:String}(; filetypes::Vector{S}=String[]
so that it is clear that a vector of strings is expected)

and change
multiple=false
to
multiple::Bool=false


Regards

Eric S

unread,
Feb 28, 2015, 10:59:29 AM2/28/15
to julia...@googlegroups.com
Milan,

I don't quite get the GetOpenFile{S<:String}(; filetypes::Vector{S}=String[] syntax. I thought a function call needed () after it rather than curly braces. In any case here are my three functions (with an extra "2" in the file names so I could test them without worrying about duplicating existing functions. It isn't clear to me whether the GetSaveFile should remove the filetypes keyword. I'm only setup to test this on a Mac so I don't know how they work on Linux or Windows. One less than ideal issue is that on my computer the dialog isn't presented on top so I need to sometimes find it behind other windows. Something with the parent keyword might be needed, but I could only get that to work by creating a Tk TopLevel and even then it still was associated with the Terminal and not my browser or Juno. If you think the code as written is good enough to submit, I can try to do a pull request. 

Many thanks,

Eric

function GetOpenFile2(;filetypes::Vector=String[], initialdir::String="",
    initialfile
::String="", multiple::Bool=false, title::String="")

  args
= Dict()
 
if length(filetypes) > 0 args["filetypes"] = filetypes end
 
if length(initialdir) > 0 args["initialdir"] = initialdir end
 
if length(initialfile) > 0 args["initialfile"] = initialfile end
  args
["multiple"] = multiple
 
if length(title) > 0 args["title"] = title end
  tcl
("tk_getOpenFile", args)
end


function GetSaveFile2(;filetypes::Vector=String[], initialdir::String="",
                       initialfile
::String="", title::String="")

  args
= Dict()
 
if length(filetypes) > 0 args["filetypes"] = filetypes end
 
if length(initialdir) > 0 args["initialdir"] = initialdir end
 
if length(initialfile) > 0 args["initialfile"] = initialfile end

 
if length(title) > 0 args["title"] = title end

  tcl
("tk_getSaveFile", args)
end


function ChooseDirectory2(;initialdir::String="",mustexist=false,title::String="")
  args
= Dict()

 
if length(initialdir) > 0 args["initialdir"] = initialdir end

 
if length(title) > 0 args["title"] = title end

  args
["mustexist"] = mustexist
  tcl
("tk_chooseDirectory", args)
end

Milan Bouchet-Valat

unread,
Feb 28, 2015, 11:17:02 AM2/28/15
to julia...@googlegroups.com
Le samedi 28 février 2015 à 07:59 -0800, Eric S a écrit :
Milan,


I don't quite get the GetOpenFile{S<:String}(; filetypes::Vector{S}=String[] syntax. I thought a function call needed () after it rather than curly braces.
These are called parametric methods: see http://docs.julialang.org/en/latest/manual/methods.html#parametric-methods

The idea here is that we want to accept a Vector{S} with S being any type inheriting from String., including for example a Vector{String} and Vector{ASCIIString} (this behavior is called covariance). Writing filetypes::Vector{String} would not accept the second (which is called invariance).


In any case here are my three functions (with an extra "2" in the file names so I could test them without worrying about duplicating existing functions. It isn't clear to me whether the GetSaveFile should remove the filetypes keyword.
No, it's also useful to tell users what file types are supported. Anyway, the goal is to mirror the Tk API, we don't need to worry about every design choice they made.


I'm only setup to test this on a Mac so I don't know how they work on Linux or Windows. One less than ideal issue is that on my computer the dialog isn't presented on top so I need to sometimes find it behind other windows. Something with the parent keyword might be needed, but I could only get that to work by creating a Tk TopLevel and even then it still was associated with the Terminal and not my browser or Juno. If you think the code as written is good enough to submit, I can try to do a pull request.
Yes, it looks good to me (though other will probably have more comments). The fact that the window is not presented on top is a broader problem which can be investigated separately.


Regards
Reply all
Reply to author
Forward
0 new messages