预加载窗口以避免在帧加载期间出现Tkinter视觉故障

时间:2018-01-28 22:58:26

标签: python tkinter preload

在我的GUI中新页面登陆后的第一个~1秒内,我立即看到许多视觉故障,然后窗口到达正确的布局(下面的截图之前/之后)。

更新

以下代码将给出所需的错误。我觉得初始帧在调用show_frame函数时与下一帧合并,并假定在预加载(隐藏/加载)下一帧之前,必须实际手动隐藏初始帧。非常感谢任何帮助 - 感谢所有到目前为止看过的人。

import tkinter as Tk
from tkinter import font as tkfont
from tkinter import filedialog, ttk #browse directories; widget support   

class Manifold(Tk.Tk):
    def __init__(self, *args, **kwargs):
        Tk.Tk.__init__(self, *args, **kwargs)
    #custom font options:
        self.title1_font = tkfont.Font(family='Helvetica',size=13) #normal type
#customized ttk GUI theme:
        GUItheme = ttk.Style()
        GUItheme.theme_use('alt')

        container = Tk.Frame(self) 
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F,geometry,title,options,wait in zip((StartPage,PageOne),
                                    ("532x279","528x270"),
                                    ("","Experimental Data"),
                                    ((False,False),(True,False)),
                                    (100,100)):
            page_name = F.__name__
            frame = F(container, self)
            self.frames[page_name] = (frame,geometry,title,options,wait) #puts all pages in stacked order
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame("StartPage")

    def show_frame(self, page_name): #show a frame for the given page name
        frame, geometry, title, options, wait = self.frames[page_name]
        self.geometry(geometry) #changes between window sizes
        self.title(title) #changes titles of windows
        self.resizable(*options) #changes ability of user to resize each window
        self.withdraw()
        frame.tkraise() #raises window to top
        self.after(wait,self.deiconify) #preload page before viewing
        self.update_idletasks()

class StartPage(Tk.Frame):
    def __init__(self, parent, controller):
        Tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background='black') #set window background color

    #page one button:
        button1 = ttk.Button(self, text="Page 1",
                         command=lambda: controller.show_frame("PageOne"))
        button1.grid(row=0,column=0,sticky='W')

class PageOne(Tk.Frame):
    def __init__(self, parent, controller):
        Tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background='gray15') #set window background color
        self.grid_columnconfigure(0,weight=1000) #for resizing window horizontally

    #average volume filename:
        self.label1 = Tk.Label(self, text="Average Volume Filename:",fg="gray90",bg="gray25",font=controller.title1_font)
        self.label1.grid(row=0,column=0,ipadx=10,ipady=0,sticky='W')
        self.label1.config(height=1)
        self.entry1 = Tk.Entry(self,textvariable=Tk.StringVar(),highlightbackground="black",width=50)
        self.entry1.grid(row=1,column=0,sticky="WE")
        self.entry1.insert(0," .mrc, .spi")

        self.entry1.configure(state="disabled") #prevent typing
        self.browse1 = ttk.Button(self,text="Browse",
                             command=self.browse_button1)
        self.browse1.grid(row=1,column=1,sticky='W')

    #gathers volume input:
    def browse_button1(self): 
        self.entry1.configure(state="normal") #unlocks typing for program
        self.label1.config(fg="gray90",bg='gray25') #standard colors, or reset on additional wrong input
        content_initial = self.entry1.get()

if __name__ == "__main__":
    app = Manifold()
    app.mainloop()

第一页: enter image description here

过渡: enter image description here

第二页:

enter image description here

3 个答案:

答案 0 :(得分:0)

隐藏ToplevelTk实例,这些实例在经过一段时间after之后才会出现故障:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except:
    import Tkinter as tk


def hide(*toplevel_or_tk_instance_s_):
    for each in toplevel_or_tk_instance_s_:
        each.withdraw()


def show(*toplevel_or_tk_instance_s_):
    for each in toplevel_or_tk_instance_s_:
        each.iconify()
        each.deiconify()


if __name__ == '__main__':
    root = tk.Tk()
    my_toplevel1 = tk.Toplevel(root, bg='red')
    my_toplevel2 = tk.Toplevel(root, bg='green')
    hide(root, my_toplevel1, my_toplevel2)
    root.after(1000, show, root)
    root.after(2000, show, my_toplevel1)
    root.after(3000, show, my_toplevel2)
    root.mainloop()

答案 1 :(得分:0)

删除所有要更新的通话,直到您准备好让用户界面显示为止,并且不要使用<>,因为零表示代码将在tkinter有机会处理任何其他事件之前运行,包括重绘部分屏幕的请求。

答案 2 :(得分:0)

好的,我已经明白了。使用我上面的代码(在我的初始问题的更新部分),我做了两处更改。第一个是在主类结构的正下方:

show_frame

第二个是在wait循环中,前面有for F, geometry,元组(在以(10,10,10,10)开头的块中等等。设置为 def show_frame(self, page_name): #show a frame for the given page name frame, geometry, title, scaling, wait = self.frames[page_name] self.quit() #seems to be important in exiting out of previous window entirely self.geometry(geometry) #changes between window sizes self.title(title) #changes titles of windows self.resizable(*scaling) #changes ability of user to resize each window self.withdraw() frame.tkraise() #raises window to top self.after(wait,self.deiconify) #preload page before viewing self.update_idletasks()

map

如果其他人最终遇到这样的事情,我希望这有帮助!