【发布时间】:2014-07-12 15:46:16
【问题描述】:
我正在尝试从我电脑上的文件夹位置读取文本文件。然后为每个文件创建检查按钮。选择复选按钮后,我想按“提交”打印在控制台窗口中选择的每个文件。
from Tkinter import *
#Tk()
import os
root = Tk()
v = StringVar()
v.set("null") # initializing the choice, i.e. Python
def ShowChoice():
state = v
if state != 0:
print(file)
for file in os.listdir("Path"):
if file.endswith(".txt"):
aCheckButton = Checkbutton(root, text=file, variable= file)
aCheckButton.pack(anchor =W)
v = file
print (v)
submitButton = Button(root, text="Submit", command=ShowChoice)
submitButton.pack()
mainloop()
运行此代码后,结果是,当检查任何检查按钮并选择按钮时,仅打印文件夹中的最后一个文本文件。对我来说这是有道理的,因为该文件被保存为最后一个读入的文件。但是,我想不出一种方法来存储每个文件名。除非我可能将文件读入一个我不知道该怎么做的数组中。 非常感谢任何帮助!
【问题讨论】: