gpt4 book ai didi

python-3.x - Tkinter 列表框 "update"或 "refresh"

转载 作者:行者123 更新时间:2023-12-05 04:11:35 25 4
gpt4 key购买 nike

我制作了一个简单的 GUI,其中包含用于输入的条目、用于将输入保存到 ini 文件的按钮。这篇文章的目的是在我添加的列表框中。如何刷新/更新列表框?如果我输入我的名字并点击保存,文件就会生成并存储在文件夹中。但是 GUI 中的列表 dosent 更新。

当我打开 GUI 时,我希望列表框显示添加的新文件。也许是更新 GUI 的按钮?

enter code here root 

root = Tk()
root.geometry('400x300')

L1 = Label(root, text='Input')
L1.place(x=10, y=10)

e1 = Entry(root)
e1.place(x=10, y=40)

def SaveInput():
config = configparser.ConfigParser()
config.add_section("DATA")
config.set("DATA", "NAME", e1.get())
list_files = os.listdir(os.getcwd())
list_numbers = [int(x[:-4]) for x in list_files if x.endswith(".ini")]

if len(list_numbers) != 0:

new_file_num = max(list_numbers) + 1

else:
new_file_num = 1

new_file_name = str(new_file_num) + ".ini"

with open(new_file_name, "w") as file_obj:
config.write(file_obj)

L1 = Listbox(root, height=5, width=50)
L1.place(x=10, y=100)
# LISTBOX
def get_filenames():
path = "C:/Users/ita9bi/Desktop/Test list"
return os.listdir(path)

for filename in get_filenames():
L1.insert(END, filename)

B1 = Button(root, text='Save', command=SaveInput)
B1.place(x=10, y=60)



root.mainloop()

最佳答案

您可以将上次保存的项目添加到列表框中,也可以在每次按 Save 按钮时重新填充所有文件名。

def SaveInput():
....
....
new_file_name = str(new_file_num) + ".ini"
L1.insert(END, new_file_name)

def SaveInput():
....
....
L1.delete(0, END) #clear listbox
for filename in get_filenames(): #populate listbox again
L1.insert(END, filename)

与其重新填充 eveytime,不如只添加最后一项会更快更高效。

关于python-3.x - Tkinter 列表框 "update"或 "refresh",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42483881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com