Mailing List Archive

tkinter call backs
I have a simple problem that I cant find the solution. I have a menu item
to open a file which has a command "openfile". I would like to activate
"tkFileDialog.askopenfilename" to get the name of the file. This requires
"parent=root" or something like that for the position of the message box.

How do I pass "root" into openfile?

Thanks
Gordon Williams



def openfile():
print "opening file"
fileName=tkFileDialog.askopenfilename(parent=root, title="Open")
if fileName: #checks for cancel
print fileName #name of file to open
file=open(fileName, 'r')
read='junk' #initialize read with something
while read != "":
read=file.readline()
print "number1",read[0:-1] # strips off line return character
# This has one more line than needed due to EOF line.
# Will use seek() to move to 2nd line
print "number of bytes ",file.tell()
file.close()
tkinter call backs [ In reply to ]
Gordon Williams <g_will@cyberus.ca> wrote:
> I have a simple problem that I cant find the solution. I have a menu item
> to open a file which has a command "openfile". I would like to activate
> "tkFileDialog.askopenfilename" to get the name of the file. This requires
> "parent=root" or something like that for the position of the message box.
>
> How do I pass "root" into openfile?

after which he promptly answered his own
question:

> fileName=tkFileDialog.askopenfilename(parent=root, title="Open")

what am I missing here?

</F>
tkinter call backs [ In reply to ]
Hi,

In fileopen(), root is a unknown name because (I believe) I dont pass root
to it. Fileopen() is a call back (and I dont know how to pass an instance
or variable through a call back). I get an name error when I then use

fileName=tkFileDialog.askopenfilename(parent=root, title="Open")

This part is in a module to make a menu. When the module is tested by
itself it runs fine and root does not give and error. print root returns .
(period). When the module is imported into the main module and then used,
I now get an error. root=Tk() is defined in the main module. Is this a
problem of local and global names? How do I overcome it?




Fredrik Lundh <fredrik@pythonware.com> wrote in article
<00ca01bed209$5543adf0$f29b12c2@secret.pythonware.com>...
> Gordon Williams <g_will@cyberus.ca> wrote:
> > I have a simple problem that I cant find the solution. I have a menu
item
> > to open a file which has a command "openfile". I would like to
activate
> > "tkFileDialog.askopenfilename" to get the name of the file. This
requires
> > "parent=root" or something like that for the position of the message
box.
> >
> > How do I pass "root" into openfile?
>
> after which he promptly answered his own
> question:
>
> > fileName=tkFileDialog.askopenfilename(parent=root, title="Open")
>
> what am I missing here?
>
> </F>
>
>
tkinter call backs [ In reply to ]
"Gordon Williams" <g_will@cyberus.ca> writes:

> I have a simple problem that I cant find the solution. I have a menu item
> to open a file which has a command "openfile". I would like to activate
> "tkFileDialog.askopenfilename" to get the name of the file. This requires
> "parent=root" or something like that for the position of the message box.
>
> How do I pass "root" into openfile?
>
> Thanks
> Gordon Williams
>
>
>
> def openfile():
> print "opening file"
> fileName=tkFileDialog.askopenfilename(parent=root, title="Open")
> if fileName: #checks for cancel
> print fileName #name of file to open
> file=open(fileName, 'r')
> read='junk' #initialize read with something
> while read != "":
> read=file.readline()
> print "number1",read[0:-1] # strips off line return character
> # This has one more line than needed due to EOF line.
> # Will use seek() to move to 2nd line
> print "number of bytes ",file.tell()
> file.close()

I get from this that you are wanting a Tkinter callback to execute
somthing like foo(bar) rather than just foo(). The Command class I
posted twice before does this:

class Command:
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw

def __call__(self, *args, **kw):
args = self.args + args
kw.update(self.kw)
apply(self.func, args, kw)

Used as following:

...
Button(text='foo', command=Command(myfunction, myarg))
...

The args (including keywords) that are passed to the Command class are
passed on to myfunction when the callback is activated.

I'm wondering if this should be part of Tkinter in some way?

--
Tim Evans
tkinter call backs [ In reply to ]
Timothy R Evans wrote:
> class Command:
> def __init__(self, func, *args, **kw):
> ...
> apply(self.func, args, kw)
>
> Used as following:
>
> Button(text='foo', command=Command(myfunction, myarg))
>
> The args (including keywords) that are passed to the Command class are
> passed on to myfunction when the callback is activated.
>
> I'm wondering if this should be part of Tkinter in some way?

You can get the same behaviour by using a lambda and defaults

Button(text='foo', command=lambda e,x=arg1, y=arg2 : function(e,x,y))

Apologies if all arguments aren't in the write place my TkInter is a bit
rusty.
--
Dan Pettersson IT Support, Uppsala University, SWEDEN
tkinter call backs [ In reply to ]
Dan Pettersson <dan.pettersson@its.uu.se> writes:

> Timothy R Evans wrote:
> > class Command:
> > def __init__(self, func, *args, **kw):
> > ...
> > apply(self.func, args, kw)
> >
> > Used as following:
> >
> > Button(text='foo', command=Command(myfunction, myarg))
> >
> > The args (including keywords) that are passed to the Command class are
> > passed on to myfunction when the callback is activated.
> >
> > I'm wondering if this should be part of Tkinter in some way?
>
> You can get the same behaviour by using a lambda and defaults
>
> Button(text='foo', command=lambda e,x=arg1, y=arg2 : function(e,x,y))
>
> Apologies if all arguments aren't in the write place my TkInter is a bit
> rusty.
> --
> Dan Pettersson IT Support, Uppsala University, SWEDEN

What bothers me about this method is readability. Part of python
appeal is clarity and simplicity, using lambdas like this goes quite a
way to breaking both these principles. Sorry if this seems a bit
strong, I just have something personal against using lambda.

--
Tim Evans