python - 有没有办法在 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()
'''
解决方案
没有内置的东西可以做到这一点。您必须编写一个函数来迭代文件和/或检查按钮并适当地更新它们。Tkinter 为您提供了添加或删除复选按钮、更改其状态以及更改其标签所需的所有工具。
所有这些都可以在不关闭窗口的情况下完成。
推荐阅读
- python - Buildroot上的Python picamera?
- php - 没有错误,但没有使用 PDO 将数据插入数据库
- vb.net - 计算多行文本框中的字符
- sql - 在同一行中的两个 array_agg 列上相交
- swift - AVAssetWriterInput 和 isReadyForMoreMediaData 问题在 Swift 中创建视频
- ruby-on-rails - 如果 AJAX 调用未经授权,则重定向到登录页面
- javascript - JQuery 根据水平方向的 tr 的类名检查 td 中的所有复选框
- sql-server - 从sql中按限制分组的表中查找重复次数最多的元素
- java - 列出特定文件夹的媒体文件图像和视频
- angular - 从角度 6 中的 service.ts 返回 undefined