简体   繁体   中英

Tkinter : Toplevel window not getting displayed

I am trying to copy files on a ftp server via a button click of a toplevel window. I want the current toplevel window to disappear and a new one to appear on which the name of the file being copied is dispalyed. After the copying is completed, the window should destroy. But the new window does not appear unless I use "pdb" or an error/pop up occurs. Here's the required code for debugging. (Excuse me for not using 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()

if x_result==True: label.set("\\nPlease wait, while the x file isbeing copied..\\n") progress_window.update()

I just added progress_window.update(), each time I changed the label value. Works for me.

if y_result==True: label.set("\\nPlease wait, while the y file isbeing copied..\\n") progress_window.update()

If a window doesn't appear, it usually means you aren't allowing the event loop to process events. My guess is, you're creating the window and then immediately entering some inner loop to do the copying. Since you never give the event loop a chance to process events (such as "draw the window"), the window never appears.

Tkinter is single threaded -- if you're doing some time-intensive function the GUI can't respond to events, and thus will appear frozen, sluggish, or not appear at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM