I started building a python tkinter gui, the problem is, after adding many features to the gui, the loading started looking really ugly. 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 ).
root = MyTkRoot()
root.mainloop() # Smooth without widget loading lag
from root_gui import Root
from time import sleep
from data_handler import DataHandler
def conn_manager(): # Working pretty well
while 'smtp_client' in locals():
smtp_client = Root()
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.
On your operating system tkinter seems to show the base
Tk 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
.after it has loaded show it again.
There are a number of ways to show and hide a window, personally I'd use
.withdraw() to remove the window from the window manager (like it was never there) then
.deiconify() (basically "unminimize") to show the window again.
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...
An alternative to withdrawing the window completely is to start it minimized with
.iconify() so it will appear on the taskbar/dock but won't open the window until it is completely loaded.
Another way to hide/show the window is by changing the
-alpha property as @double_j has done 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 http://wiki.tcl.tk/9457:
-alpha double controls opacity (from 0.0 to 1.0)
-alpha double controls opacity (from 0.0 to 1.0).
This requires a compositing window manager to have any effect. [compiz] is one such, and xfwm4 (for the XFCE desktop) is another.
-alpha double how opaque the overall window is; note that changing between 1.0 and any other value may cause the window to flash (Tk changes the class of window used to implement the toplevel).
So on some unix machines
-alpha 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)
deiconify works identically amongst all platforms as far as I am aware.