Tk Source Code

View Ticket
Login
Ticket UUID: d1989fb7cf6e13b33f641571bb5b0304023d2e28
Title: Aqua: Empty window appears after `package require Tk`
Type: Bug Version: core-8-6-branch
Submitter: chrstphrchvz Created on: 2019-04-28 19:08:29
Subsystem: 02. Appearance Assigned To: marc_culler
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2019-05-16 19:52:28
Resolution: Fixed Closed By: marc_culler
    Closed on: 2019-05-16 19:52:28
Description: (text/x-fossil-wiki)
<strike>Sometime between 8.6.9.1 and the latest core-8-6-branch ([24b987ca])</strike> As of [5e709226], doing <code>package require Tk</code> now causes an empty window to appear immediately. <strike>I do not know what specific change introduced this behavior.</strike>

Example:

<code>
package require Tk<br />
puts "Waiting"<br />
after 3000<br />
puts "Done"<br />
</code>

An empty window will appear before <code>Waiting</code> appears, whereas when run on 8.6.9.1, or in X11, no windows would appear until <code>Done</code> appears.

As a result, when running a program, this empty window appears for a fraction of a second; before, only the program's fully-populated, ready-to-use window would appear.

Originally observed by a Tcl::pTk user: [https://rt.cpan.org/Ticket/Display.html?id=129347]
User Comments: marc_culler (claiming to be Marc Culler) added on 2019-05-16 19:52:28:
Nicolas and Christopher, thanks for the reviews!

I will merge the bugfix branch and close this ticket.

chrstphrchvz added on 2019-05-16 17:27:22: (text/x-fossil-wiki)
Hi Marc, I have confirmed that I do not observe this issue under [9e9a9189a8]. Thank you for looking into this.

nab added on 2019-05-16 14:09:53:
Hi Marc,
I've compiled and run  9e9a9189a8, and there's no more a blank window at start.
many thanks :)

++

marc_culler (claiming to be Marc Culler) added on 2019-05-16 13:25:53: (text/x-fossil-wiki)
@chrstphrchvz, @nab: It would be great if you two could review the proposed
fix in commit [https://core.tcl.tk/tk/info/9e9a9189a87fb6d6|9e9a9189a8].

marc_culler (claiming to be Marc Culler) added on 2019-05-15 17:25:49: (text/x-fossil-wiki)
I reviewed the problem reported as bug [56a1823c73],
which commit [5e709226] was meant to fix.  The underlying cause of that
bug was that the root window was being created and iconified before the NSApplication had finished launching, causing it to open off screen when
it was deiconified.

I found a better fix for [56a1823c73], which does not require processing any events in the XMapWindow and which also fixes this bug.

There was another place where calls to Tcl_DoOneEvent had been added in order
to deal with problems that arose in OSX 10.14.  That was in the fix for
bug [3efbe4a397].  I found
that those calls were no longer needed, so I removed them as well.

These changes are now in commit
[https://core.tcl.tk/tk/info/9e9a9189a87fb6d6|9e9a9189a8]
to the branch bug-d1989fb7cf.

In ticket [3efbe4a397] Kevin mentions a different problem which he thinks is
related to the grab command.  That problem is still not fixed in either the
core-8-6-branch or bug-d1989fb7cf.  It is not clear to me whether it had ever
been fixed.  In any case, it appears to be unrelated to this ticket.

marc_culler (claiming to be Marc Culler) added on 2019-05-01 13:10:17: (text/x-fossil-wiki)
I won't be able to look at this for a couple of weeks, but I know what the cause is.  This behavior was created by the fix for bug [https://core.tcl.tk/tk/tktview?name=56a1823c73|56a1823c73], which was even more annoying if you had the good fortune to run into it.  The change was  in made in order to make it possible for a toplevel to be deiconified in response to the selection of a file in tk_getOpenFile, which is a pretty natural thing to want to be able to do.

The fix for that bug was to add a loop inside of XMapWindow which calls Tcl_DoOneEvent to process any pending events in the queue when mapping a new toplevel.

The core issue that leads to the root window appearing too soon is that Tk insists on creating the root window long before it calls the platform-specific initialization code.  On the Mac this means that the root window is created before the NSApplication object exists, hence before Tk has an event source.  There are many lines of code in the Mac port devoted to working around this behavior.

Now it seems that Tk does not only create the root window before calling the platform specific initialization code, it also maps it. Since, in its current state, XMapWindow briefly runs its own mini event loop, this has the effect of actually mapping the root window <b>at the time that Tk asked for it to be
mapped.</b> Maybe the place to start with this bug would be to get a Tk expert to explain why Tk wants to map the root window before it has started its event loop and before it has run the platform specific initialization code.

nab added on 2019-05-01 09:06:34:
Hi,
I also see a blank window when [package require Tk] is invoked and I find it quite disturbing...
I agree with chrstphrchvz that no blank window should appear before any Tcl_DoOneEvent() call.

++

fvogel added on 2019-04-30 19:56:35:
Marc, please, any thoughts perhaps?

chrstphrchvz added on 2019-04-29 03:14:34: (text/x-fossil-wiki)
Bisecting identified [5e709226] as the change responsible for this behavior. Sure enough it adds a couple of temporary loops which call <code>Tcl_DoOneEvent()</code>.

I personally am not bothered by the changed behavior, but the user that reported this to me finds it "quite disturbing".

chrstphrchvz added on 2019-04-28 20:37:51: (text/x-fossil-wiki)
<blockquote>On Windows saying <code>package require Tk</code> has always triggered an empty '.' window to appear on screen.</blockquote>

Yes, when Tcl is not told to do anything else immediately after doing <code>package require Tk</code>, it gets to enter the event loop and make an empty window appear. That is what I observe on any windowingsystem, including on aqua with 8.6.9.1, and is not the issue I am describing here.

What I am describing is that a window is now appearing immediately after <code>package require Tk</code> before evaluating the next statement, and without entering the Tcl event loop (including returning to the interactive tclsh/wish REPL prompt).

In Tk wrappers that use the Tcl C API, the past behavior has been that no windows will appear until that wrapper's <code>mainloop</code> is entered, which in turn starts calling <code>Tcl_DoOneEvent()</code>. What this issue means is that as of sometime since 8.6.9.1, an empty window on aqua appears immediately after doing <code>Tcl_Eval(interp, "package require Tk")</code>, without ever calling <code>Tcl_DoOneEvent()</code>. Short C example:

<pre>
#include <stdio.h>
#include <unistd.h>
#include <tcl.h>

int main(void) {
    Tcl_Interp *interp = Tcl_CreateInterp();
    Tcl_Init(interp);
    printf("Doing `package require Tk`\n");
    Tcl_Eval(interp, "package require Tk");
    sleep(5);
    printf("Entering event loop\n");
    while(1){
        Tcl_DoOneEvent(TCL_ALL_EVENTS);
    }
    return 0;
}
</pre>

Indeed, those writing Tk programs in Tcl and using <code>tclsh</code> or <code>wish</code> interactively might be used to windows appearing and changing with each statement they type; whereas those using a non-Tcl wrapper might instead be used to seeing nothing happen until their wrapper's <code>mainloop</code> is irreversibly entered. (I do not know how non-Tcl users typically go about using Tk from an interactive shell, or if they expect to even be able to, the way tclsh/wish users might.)

fvogel added on 2019-04-28 19:45:47: (text/x-fossil-wiki)
On Windows saying <code>package require Tk</code> has always triggered an empty '.' window to appear on screen.