[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Select a File...



On 7/31/07, Toni Alatalo <antont@xxxxxxxxxxxxx> wrote:
On Wednesday 01 August 2007 08:39:18 altern wrote:
> Ian Mallett(e)k dio:
> > By command-line I mean something where you select the file from the
> > console window.  I once wrote a little program that does that, but only
> > to lower level directories.  If I were to use a command-line thing, it

i did not quite get that, am confused about how cmdline is related to the
fileselector / gui things, (did not read all the mails too carefully, sorry)

You know how if you open a command line window and type cd C:\dev\whatever it changes the directory?  I wrote a little program that prints all the files in a directory and allows you to change directory.  Unfortunately, I don't know how to make code to make it go one directory higher.  Hence, we're looking for something that can.  Graphics would be nice instead. 

> > I think wxPython is what I need.  I don't know how to use it, though.
> > I'm looking at wxFileSelector()
> i dont think using pygame together with wxpython is agood idea, if it is

just figured to mention for the sake of completeness, and 'cause have had
actually surprisingly good experiences with it, the (good) old tkinter! this
is a one/two-liner i have in many pygame apps that use data in a directory:

--- copy .. ---

import tkFileDialog

#print "asking directory"
#directory="../scale/renderout"

try:
    directory = sys.argv[1]

except IndexError: #no cmdline arg given
    directory = tkFileDialog.askdirectory()

--- ... pasted ---

The nice things are: tkinter is there by default in the std py install (needs
a separate package for macosx though 'cause Apple does not bundle it, but
more on that later), the  command is really simple to use (no need to even
import tkinter, just that tkFileDialog module, nor to deal with any App
framework kind of stuff) .. just pop up a dialog and back you get the dir as
string. ok you do also get an empty tk window as a side effect but you can
ignore that..

On the unfortunate / poor side is the fact that as tk uses / is also an old
own widget lib, e.g. on win32 the tkFileDialog is not implemented using the
standard MFC whatever things, so it is not the file dialog you are used to
and want on windows, it does not have you list of favourite dirs from the
OS / desktop env etc.

The suprising cool thing was that on Mac OS X tk is implemented so that e.g.
that file dialogue uses native widgets, hence you get the nice aqua thing you
want.

I guess on x11 things can vary .. but basically the situation is similar to
win32, i mean that e.g. on Gnome or KDE you also don't get the 'native'
widgets of that desktop .. or does it depend on your tk? But the tk widget is
not awful, and quite usable on win32 too for this simple thing .. and is
pre-installed there already if there is Python.

So my view has been that things like Wx that use native things are better, but
for simple things the old standard / legacy tk stuff can be nice and are very
useful for us in our in-house things and even products .. especially on macs,
and I guess you can hack tkFileDialog use the dialog of your choice in other
envs too..

Dunno if this addresses the question, but figured is an experience worth
sharing anyway, as it is exactly with pygame apps i've used this technique a
lot and for long a time now. Of course doing a proper gui inside the pygame
app is nicer, thanks to previous posters for info on those .. but this is a
one liner you can do in a std py install so it is kinda cool even for
interactive shell sessions :)

>>> import tkFileDialog
>>> tkFileDialog.askdirectory()
u'/home/antont/.emacs.d'

> enrike

~Toni

To everyone:  pgu works to select the file.  There are, however, a few glitches, like the first directory on the highest level is not listed.  I was also hoping for something like a popup window.  Any program with a 'open' function has EXACTLY what I want.  How do you do that function?