Discussion:
[Tutor] Why does this function execute before being called?
Chris Roy-Smith
2015-11-07 04:34:02 UTC
Permalink
Hi,
Environment:
Python 2.7
Linux (Ubuntu 15.10)

I am experiencing a problem with the code below running the "genF"
function on opening the second window. I expected that function to be
executed on clicking the 'fill text' button. The text widget gets filled
on opening the window. This is my first attempt at opening a second
window, so I expect I have done something stupid.


#! /usr/bin/python
from Tkinter import *

root=Tk()


def genF(ofield):
for x in range(10):
ofield.insert(END, x)
ofield.insert(END, "\n")


def second():
main=Toplevel(root)
ofield=Text(main, height=15, width=15)
ofield.pack()
B3=Button(main, text='exit', command=main.destroy)
B3.pack()
B4=Button(main, text='fill text', command=genF(ofield))
B4.pack()
main.mainloop()

b1=Button(root, text='open second window', command=second)
b1.pack()
b2=Button(root, text='exit', command=root.destroy)
b2.pack()
root.mainloop()


Thanks,
Chris
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-11-07 09:18:30 UTC
Permalink
def genF(ofield): ...
main=Toplevel(root)
ofield=Text(main, height=15, width=15)
ofield.pack()
B3=Button(main, text='exit', command=main.destroy)
B3.pack()
B4=Button(main, text='fill text', command=genF(ofield))
You call a function by sup[plying the parens after its name.
So the function gets called here. The normal way to circumvent
that in Tkinter is to use a lambda expression to defer execution,
like so:

B4=Button(main, text='fill text', command=lambda wgt=ofield : genF(wgt))
B4.pack()
main.mainloop()
I'm not sure you need the second mainloop. I think the
root level mainloop will work for your window too.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Chris Roy-Smith
2015-11-07 09:28:47 UTC
Permalink
Post by Alan Gauld
def genF(ofield): ...
main=Toplevel(root)
ofield=Text(main, height=15, width=15)
ofield.pack()
B3=Button(main, text='exit', command=main.destroy)
B3.pack()
B4=Button(main, text='fill text', command=genF(ofield))
You call a function by sup[plying the parens after its name.
So the function gets called here. The normal way to circumvent
that in Tkinter is to use a lambda expression to defer execution,
B4=Button(main, text='fill text', command=lambda wgt=ofield : genF(wgt))
This certainly wasn't obvious from what I could find on the internet.
Now I see an application for Lambda
Post by Alan Gauld
B4.pack()
main.mainloop()
I'm not sure you need the second mainloop. I think the
root level mainloop will work for your window too.
Just tried out leaving this second mainloop, and every works the same. I
had assumed I needed to create a loop the same as the top window.

Thanks for clearing up this mystery

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-11-07 12:36:39 UTC
Permalink
Post by Chris Roy-Smith
Post by Alan Gauld
B4=Button(main, text='fill text', command=lambda wgt=ofield : genF(wgt))
This certainly wasn't obvious from what I could find on the internet.
Now I see an application for Lambda
I should point out you can use a def if you prefer:

def second():
main=Toplevel(root)
ofield=Text(main, height=15, width=15)
ofield.pack()
B3=Button(main, text='exit', command=main.destroy)
B3.pack()
def genFwrapper(widget = ofield): return genF(widget)
B4=Button(main, text='fill text', command=genFwrapper)
B4.pack()


The lambda just saves a line.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...