Tkinter - Preload window?
<p>I started building a python tkinter gui, the problem is, after adding many features to the gui, the loading started looking <strong>really ugly</strong>. When starting the mainloop, a blank window shows up for a few miniseconds before the widgets are loaded, the same happens with the other Toplevel windows ( except the ones with few static elements that don't need updates ). </p>

<h2>My question is</h2>

<p>Is there a way to 'preload' the window so that when I would call it, it would start smoothly?</p>

<h3>For example</h3>

<pre><code>root = MyTkRoot()
root.build_stuff_before_calling()
root.mainloop() # Smooth without widget loading lag
</code></pre>

<h3>Part of my code</h3>

<pre><code>from root_gui import Root
import threading
from time import sleep
from data_handler import DataHandler


def handle_conn():
    DataHandler.try_connect()
    smtp_client.refresh()


def conn_manager(): # Working pretty well
    while 'smtp_client' in locals():
        sleep(3)
        handle_conn()


smtp_client = Root()
handle_conn()


MyConnManager = threading.Thread(target=conn_manager)
MyConnManager.start() # Thanks to the second thread, the tk window doesn't have to wait for 3 seconds before loading the widgets.
smtp_client.mainloop()
</code></pre>

<h3>The full project on git</h3>

<p>Too much code to put in here...</p>

<p><a href="https://github.com/cernyd/smtp_client" rel="nofollow">https://github.com/cernyd/smtp_client</a></p>

<p>On your operating system tkinter seems to show the base <code>Tk</code> window before loading it's widgets causing that few milliseconds of blank window.  To make it appear with all the widgets already loaded you will need to hide the window when starting and <code>.after</code> it has loaded show it again.</p>

<p>There are a number of ways to show and hide a window, personally I'd use <code>.withdraw()</code> to remove the window from the window manager (like it was never there) then <code>.deiconify()</code> (basically "unminimize") to show the window again.</p>

<pre><code>class TEST(tk.Tk):
    def __init__(self,*args,**kw):
        tk.Tk.__init__(self,*args,**kw)
        self.withdraw() #hide the window
        self.after(0,self.deiconify) #as soon as possible (after app starts) show again
        #setup app...
</code></pre>

<p>An alternative to withdrawing the window completely is to start it minimized with <code>.iconify()</code> so it will appear on the taskbar/dock but won't open the window until it is completely loaded.</p>

<p>Another way to hide/show the window is by changing the <code>-alpha</code> property as <a href="http://stackoverflow.com/a/39211981/5827215">@double_j has done</a> but I would not recommend that in production code because the window is technically still there, it (and the close button etc.) can be clicked on /interacted with for a brief moment before showing which could be undesired, as well it's behaviour can be ambiguous amongst operating systems, from <a href="http://wiki.tcl.tk/9457" rel="nofollow">http://wiki.tcl.tk/9457</a>:</p>

<blockquote>
  <h2>Macintosh Attributes</h2>
  
  <p>-alpha double controls opacity (from 0.0 to 1.0)
  <br>...<br></p>
  
  <h2>Unix/X11 Attributes</h2>
  
  <p>-alpha double controls opacity (from 0.0 to 1.0).<br> This requires a compositing window manager to have any effect. [compiz] is one such,
  and xfwm4 (for the XFCE desktop) is another.
  <br>...<br></p>
  
  <h2>Windows Attributes</h2>
  
  <p>-alpha double how opaque the overall window is; <strong>note that changing between 1.0 and any other value may cause the window to flash</strong> (Tk
  changes the class of window used to implement the toplevel).</p>
</blockquote>

<p>So on some unix machines <code>-alpha</code> may have no effect and on windows it may cause the window to flash (probably not an issue before it is even opened but still worth noting)</p>

<p>Where as <code>withdraw</code> and <code>deiconify</code> works identically amongst all platforms as far as I am aware.</p>

python user-interface tkinter lag