[Tutor] Why does this function execute before being called?

Chris Roy-Smith chris_roysmith at internode.on.net
Fri Nov 6 23:34:02 EST 2015


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


More information about the Tutor mailing list