【问题标题】:Python Tkinter: Reading in file from location, creating checkbuttons for each filePython Tkinter:从位置读取文件,为每个文件创建检查按钮
【发布时间】: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()

运行此代码后,结果是,当检查任何检查按钮并选择按钮时,仅打印文件夹中的最后一个文本文件。对我来说这是有道理的,因为该文件被保存为最后一个读入的文件。但是,我想不出一种方法来存储每个文件名。除非我可能将文件读入一个我不知道该怎么做的数组中。 非常感谢任何帮助!

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    除非我将文件读入数组

    不,您不想一次读取所有这些文件。这将极大地影响性能。

    但是,如果您列出检查按钮及其相关变量,那就更好了。这样,您可以在函数ShowChoice 中轻松访问它们。

    下面是你的程序版本,它采用了这个想法。我评论了我更改的大部分行:

    from Tkinter import *
    import os
    root = Tk()
    
    # A list to hold the checkbuttons and their associated variables
    buttons = []
    
    def ShowChoice():
        # Go through the list of checkbuttons and get each button/variable pair
        for button, var in buttons:
            # If var.get() is True, the checkbutton was clicked
            if var.get():
                # So, we open the file with a context manager
                with open(os.path.join("Path", button["text"])) as file:
                    # And print its contents
                    print file.read()
    
    
    for file in os.listdir("Path"):
        if file.endswith(".txt"):
            # Create a variable for the following checkbutton
            var = IntVar()
            # Create the checkbutton
            button = Checkbutton(root, text=file, variable=var)
            button.pack(anchor=W)            
            # Add a tuple of (button, var) to the list buttons
            buttons.append((button, var))
    
    
    submitButton = Button(root, text="Submit", command=ShowChoice)
    submitButton.pack()
    
    mainloop()
    

    【讨论】:

      【解决方案2】:

      根据the checkbutton doc,您必须将 IntVar 绑定到按钮,才能查询其状态。

      因此,在构建按钮时,给它们一个 IntVar,作弊并将文件名附加到 IntVar 以便稍后获取:

      checked = IntVar()
      checked.attached_file = file
      aCheckButton = Checkbutton(root, text=file, variable=checked)
      aCheckButton.pack(anchor=W)
      buttons.append(checked)
      

      您的 ShowChoice 现在看起来像:

      def ShowChoice():
          print [button.attached_file for button in buttons if button.get()]
      

      如果选中按钮,则打印每个按钮的附件(button.attached_file)(如果选中,则 button.get() 为 1)。

      别忘了在所有这些东西之前声明一个“buttons = []”。

      您也可以阅读并采用PEP8 的风格,以一个更易读(适合所有​​人)的文件结尾。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-25
        • 1970-01-01
        • 2021-08-14
        • 2019-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多