Tkinter:Toplevel窗口无法显示

时间:2014-04-18 00:41:38

标签: python tkinter

我试图通过点击顶层窗口的按钮来复制ftp服务器上的文件。我希望当前的顶层窗口消失,并显示一个新的窗口,在该窗口上显示正在复制的文件的名称。复制完成后,窗口应该销毁。但是除非我使用“pdb”或发生错误/弹出,否则不会出现新窗口。这是调试所需的代码。 (请原谅我没有使用Classes)

copy_window=Toplevel(main_window)
copy_window.resizable(False,False)
main_window.withdraw()
copy_window_Label1=Label(copy_window,text="\nTick the files you want to copy.\n").pack()

File_frame=Frame(copy_window)
Frame1=Frame(File_frame)
Frame2=Frame(File_frame)

#Some file and folder name setting code for each file edited
x_Checkbutton=Checkbutton(Frame1,variable=x_Check,text=x_file_name+" will be copied in ").pack(side=LEFT)
x_Entry=Entry(Frame1,width=10)
x_Entry.insert(0,folder_1)
x_Entry.pack(side=LEFT)
y_Checkbutton=Checkbutton(Frame1,variable=y_Check,text=y_file_name+" will be copied in ").pack(side=LEFT)
y_Entry=Entry(Frame2,width=10)



def ok_button_click():
    if (x_check.get()==1 or y_check.get()==1):

        progress_window=Toplevel(copy_window)
        label=StringVar()
        p_label=Label(progress_window,textvariable=label).pack()
        label.set("\nPlease wait while the files are being copied..\n")
        copy_window.withdraw()

        site=FTP("12345")           
        site.login("a","a")             

        x_result=True
        y_result=True

        if x_check.get()==1:                
            site.cwd(sub_path)
            if ((x_Entry.get() in site.nlst())==False):
                site.mkd(x_Entry.get())
            else:
                site.cwd(x_Entry.get())
                for item in site.nlst():
                    if (item.startswith('x_'+name):
                        x_result=tkMessageBox.askyesno("Warning !",item+" is already present in "+x_Entry.get()+"\nDo you want to copy this new file?\nIf you select Yes, Previous file will be deleted")
                        if x_result==True:
                            site.delete(item)
                        break
                site.cwd(sub_path)      
            if x_result==True:
                label.set("\nPlease wait, while the x file is being copied..\n")
                site.cwd(x_Entry.get())
                with open(x_file_name,"rb") as f:
                    site.storfile("STOR "+x_file_name,f)
        if y_check.get()==1:
            site.cwd(sub_path)
            if ((y_Entry.get() in site.nlst())==False):
                site.mkd(y_Entry.get())
            else:
                site.cwd(y_Entry.get())
                for item in site.nlst():
                    if item.startswith('y_'):
                        y_result=tkMessageBox.askyesno("Warning !",item+" is already present in "+y_Entry.get()+"\nDo you want to copy this new file?\nIf you select Yes, Previous file will be deleted")
                        if y_result==True:
                            site.delete(item)
                        break
                site.cwd(sub_path)      
            if y_result==True:
                label.set("\nPlease wait, while the y file is being copied..\n")
                site.cwd(y_Entry.get())
                with open(y_file_name,"rb") as f:
                    site.storfile("STOR "+y_file_name,f)            

        progress_window.destroy()
        if not (y_result==False and x_result==False):
            tkMessageBox.showinfo("Success !","Files copied successfully.")
    copy_window.destroy()
    main_window.update()
    main_window.deiconify()

ok_button=Button(copy_window,text="OK",width="10",command=ok_button_click).pack()

2 个答案:

答案 0 :(得分:1)

  

如果x_result == True:       label.set(“\ n请等待,而x文件正在被复制.. \ n”)       progress_window.update()

我每次更改标签值时都添加了progress_window.update()。适合我。

  

如果y_result == True:       label.set(“\ n请等待,而y文件正在被复制.. \ n”)       progress_window.update()

答案 1 :(得分:0)

如果没有出现窗口,通常意味着您不允许事件循环处理事件。我的猜测是,你正在创建窗口,然后立即进入一些内循环来进行复制。由于您从未给事件循环提供处理事件的机会(例如“绘制窗口”),因此窗口永远不会出现。

Tkinter是单线程的 - 如果你正在做一些时间密集型的功能,GUI就无法响应事件,因此会出现冻结,缓慢或根本不显示。