Tkinter-预加载窗口? [英] Tkinter - Preload window?

查看:482
本文介绍了Tkinter-预加载窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始构建一个python tkinter gui,问题是,在gui中添加了许多功能之后,加载开始看起来真的很丑.启动主循环时,在加载小部件之前,空白窗口会显示几毫秒,其他顶级窗口也会发生同样的情况(除了静态元素很少且不需要更新的窗口).

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 ).

有没有一种方法可以预加载"窗口,以便当我调用它时,它可以顺利启动?

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

root = MyTkRoot()
root.build_stuff_before_calling()
root.mainloop() # Smooth without widget loading lag

我的代码的一部分

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()

关于git的完整项目

在这里没有太多代码...

The full project on git

Too much code to put in here...

https://github.com/cernyd/smtp_client

推荐答案

在您的操作系统上,tkinter似乎在加载窗口小部件之前显示了基本的Tk窗口,从而导致了几毫秒的空白窗口.要使其与所有已加载的小部件一起显示,您需要在启动时隐藏该窗口,并且.after它已加载后再次显示该窗口.

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.

有多种显示和隐藏窗口的方法,就我个人而言,我会使用.withdraw()从窗口管理器中删除该窗口(就像从未出现过的那样),然后.deiconify()(基本上是最小化")再次显示窗口.

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...

完全退出窗口的另一种方法是使用.iconify()将其最小化,以便它出现在任务栏/停靠栏上,但直到完全加载窗口时才打开窗口.

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.

另一种隐藏/显示窗口的方法是将-alpha属性更改为 @double_j已经完成,但是我不建议在生产代码中使用该窗口,因为从技术上讲该窗口仍然存在,因此可以短暂地单击/与之交互(或关闭按钮等),然后再显示,这可能是不希望的,并且其行为在其中也可能是模棱两可的操作系统,来自 http://wiki.tcl.tk/9457 :

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:

Macintosh属性

-alpha双控制不透明度(从0.0到1.0)
...

Macintosh Attributes

-alpha double controls opacity (from 0.0 to 1.0)
...

-alpha双控制不透明度(从0.0到1.0).
这需要合成窗口管理器才能起作用. [compiz]就是这样的一个 xfwm4(用于XFCE桌面)是另一个.
...

-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两倍,整个窗口不透明; 请注意,在1.0和其他任何值之间进行更改都可能导致窗口闪烁(Tk 更改用于实现顶层的窗口的类.

-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).

因此,在某些UNIX计算机上,-alpha可能没有任何作用,并且在Windows上可能会导致窗口闪烁(甚至在打开之前可能不是问题,但仍然值得注意)

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)

据我所知,withdrawdeiconify在所有平台上的工作方式完全相同.

Where as withdraw and deiconify works identically amongst all platforms as far as I am aware.

这篇关于Tkinter-预加载窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆