【发布时间】:2021-02-08 01:03:37
【问题描述】:
我一直在尝试使用 tkinter CheckButton 小部件来编辑列表中的项目 - 列表中的每个项目都是一个新的检查按钮。我想要一个保存方法来将数据保存到文本文件中,以及一个加载方法来从文本文件中加载信息并根据列表中的项目标记复选框。
到目前为止,这是我的代码,但是当我检查按钮并更新列表/文件时,列表似乎没有改变
这是我的代码,我需要知道为什么当我选中这些框时列表没有更新:
import tkinter.messagebox as box
modulesMain = Tk()
moduleChecks = []
def SaveChanges():
# Clear the text file
modules = open("modules.txt", "w") #Write mode to overwrite the whole file
modules.write("") # Put in blank text
modules.close()
modules = open("modules.txt", "a") # Append mode to append the file
for item in moduleChecks:
modules.write(item + "\n")
print(moduleChecks)
appButton = Checkbutton(modulesMain, text = "Test", variable = moduleChecks[0]).grid()
searchButton = Checkbutton(modulesMain, text = "Test", variable = moduleChecks[1]).grid()
Save = Button(modulesMain, text = "Save Changes", command = SaveChanges).grid()
【问题讨论】:
-
请edit您的问题并提供可运行的minimal reproducible example。
-
我稍微编辑了一下,但我的主要问题不在代码中,而是在文本的其余部分 - 没有实际错误,只是没有对列表做任何事情
标签: python list tkinter checkbox