首页 > 解决方案 > 有没有办法在 tkinter 中刷新/更新顶级窗口?

问题描述

我在顶级窗口上创建检查按钮这些检查按钮适用于存储在缓存文件夹中的所有文件但是当缓存文件夹中的文件添加或删除时检查按钮没有更新是否有任何方法可以刷新或更新顶级窗口而不关闭它。'''

class encrypt_files:
            def __init__(self):
                pass
            def encrypt_file(self):
                paths =  filedialog.askopenfilenames()
                for item in paths:
                    # encryption/decryption buffer size - 64K
                    bufferSize = 64 * 1024
                    password = "FootWork"
                    # adding encrypted files to cache folder
                    pyAesCrypt.encryptFile(item,path.basename(item) + ".aes", password, bufferSize)
        class encrypt_folder:
            def __init__(self):
                pass
            def open_folder(self):
       #creating top level window
                top_level = tk.Toplevel()
                top_level.geometry("700x500+500+200")
                top_level.title("Encyption Files")
                p2 = encrypt_files()

        
                tool_bar = ttk.LabelFrame(top_level)
                tool_bar.pack(side = "top",fill = "both")
                
                main_body = ttk.LabelFrame(top_level)
                main_body.pack(side = "top",fill = "both")
                
                upload_button = ttk.Button(tool_bar,text = "Upload file",command = lambda :p2.encrypt_file())
                upload_button.grid(row = 0,column = 0)
        
        #checking for files in cache folder
                if "cache" not in os.getcwd().split(r"\ ".replace(" ", "")):
                    os.chdir(os.getcwd() + r"\cache")
                dirlist  = os.listdir()
                radiolist = {}

        #placing all check buttons on toplevel window which i want to update
                for i in dirlist:
                    radiolist[i] = tk.IntVar()
                    check_button = ttk.Checkbutton(main_body,text = i,variable = radiolist[i])
                    check_button.grid(sticky="w")
        
                top_level.mainloop()

'''

标签: pythontkinter

解决方案


没有内置的东西可以做到这一点。您必须编写一个函数来迭代文件和/或检查按钮并适当地更新它们。Tkinter 为您提供了添加或删除复选按钮、更改其状态以及更改其标签所需的所有工具。

所有这些都可以在不关闭窗口的情况下完成。


推荐阅读