如何将不同的radiobuttons链接到不同的文件夹

时间:2017-01-24 13:19:12

标签: python tkinter

我试图将不同的radiobuttons链接到不同的文件夹。我设法打开一个文件夹,但现在所有的radiobuttons都链接到该文件夹​​。我明白这是因为他们有同样的赞扬,使用第一个radiobutton的路径,但我不知道如何改变它。我的问题是,如何将不同的radiobuttons链接到不同的文件夹? 另外,如何同时打开1个文件夹中的多个文件? 这是我的代码:

from tkinter import*
from tkinter import filedialog 
import tkinter.constants 

class filedialogexample(tkinter.Frame):
    def __init__(self, root):
        tkinter.Frame.__init__(self, root)
        self.favorite = StringVar()

        tkinter.Radiobutton(self, text = "Browse 1", variable = self.favorite, 
                command = self.askopenfilename
                ).grid( row=2, column =0, columnspan = 2, sticky =W)
        tkinter.Radiobutton(self, text = "Browse 2", variable = self.favorite, 
                command = self.askopenfilename
                ).grid( row=3, column =0, columnspan = 2, sticky =W)
        tkinter.Radiobutton(self, text = "Browse 3", variable = self.favorite,  
                command = self.askopenfilename
                ).grid( row=4, column =0, columnspan = 2, sticky =W)
        tkinter.Radiobutton(self, text = "Browse 4", variable = self.favorite,
                command = self.askopenfilename
                ).grid( row=5, column =0, columnspan = 2, sticky =W)


        self.dir_opt = options = {} 
        options['initialdir'] = 'C:\\Users\\kom01\\Documents\\Python Scripts'


    def askopenfilename(self):
        filename = filedialog.askopenfilename(**self.dir_opt)
        if filename:
            return open(filename, 'r')

    def askdirectory(self, root):
        return filedialog.askdirectory(**self.dir_opt)

if __name__=='__main__':
    root = Tk()
    filedialogexample(root).grid()
    root.mainloop()

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

要根据选择的单选按钮将initialdir传递给filedailog.askopenfilename,您可以使用单选按钮的值选项。由于您的所有单选按钮都连接到self.favorite变量,self.favorite将包含所选单选按钮的值。

首先,您需要为每个单选按钮分配文件夹的路径(在创建单选按钮时value='path\\to\\folder')。然后,在askopenfilename中,您将self.favorite.get()作为initialdir传递。最后,返回打开的文件是没用的,因为你无法从按钮命令中获取它。因此,您应该直接对askopenfilename函数内的文件执行操作。在下面的示例中,我只打印文件的内容。

import tkinter
from tkinter import filedialog 

class filedialogexample(tkinter.Frame):
    def __init__(self, root):
        tkinter.Frame.__init__(self, root)
        self.favorite = tkinter.StringVar()

        tkinter.Radiobutton(self, text = "Browse 1", variable = self.favorite, 
                command=self.askopenfilename, value="folder1"
                ).grid(row=2, column=0, columnspan=2, sticky='w')
        tkinter.Radiobutton(self, text = "Browse 2", variable = self.favorite, 
                command=self.askopenfilename, value="folder2"
                ).grid(row=3, column=0, columnspan=2, sticky='w')
        tkinter.Radiobutton(self, text = "Browse 3", variable = self.favorite,  
                command=self.askopenfilename, value="folder3"
                ).grid(row=4, column=0, columnspan=2, sticky='w')
        tkinter.Radiobutton(self, text = "Browse 4", variable = self.favorite,
                command=self.askopenfilename, value="folder4"
                ).grid(row=5, column=0, columnspan=2, sticky='w')
        # replace folder1,..,folder4 by actual paths to folders


    def askopenfilename(self):
        # initialdir is given by the selected radiobutton
        filename = filedialog.askopenfilename(initialdir=self.favorite.get())
        if filename:
            self.do_something_on_selected_file(filename)

    def do_something_on_selected_file(self, filename):
        with open(filename, 'r') as myfile:
            print(myfile.read())

if __name__=='__main__':
    root = tkinter.Tk()
    filedialogexample(root).grid()
    root.mainloop()