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>The short answer is yes. You can set the window to appear as though you can't see it. Perform any code that takes time, and then display the window.</p>

<p>Here's how:</p>

<pre><code>class MyTkRoot(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.attributes('-alpha', 0.0)  # make window transparent
        # build gui...
        self.after(0, self.attributes, "-alpha", 1.0)  # back to normal
root = MyTkRoot()
root.mainloop()
</code></pre>

<p><a href="http://effbot.org/tkinterbook/wm.htm#Tkinter.Wm.attributes-method" rel="nofollow">Here's</a> a reference to the code with the example of a Toplevel. It will also work with the root however.</p>

python user-interface tkinter lag