異なるラジオボタンを別のフォルダにリンクしようとしています。私は1つのフォルダを開くことができましたが、すべてのラジオボタンはそのフォルダにリンクされています。私はそれが最初のラジオボタンのパスを使用するのと同じ褒め言葉を持っているからだと理解していますが、それをどのように変更するのか分かりません。私の質問は、どのように私は別のラジオボタンを別のフォルダにリンクできますか? また、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()
ありがとうございました!
'filedialog.askopenfilename'に渡される' initialdir'をラジオボタンごとに異なるようにしますか?とにかく、変数 'self.favorite'が選択されたラジオボタンに対応する値を含むように、各ラジオボタンに値を与えるべきです。さらに、 'askopenfilename'関数によって返されたファイルは、ボタンコマンドからは使用できません。 –
迅速な対応に感謝します。はい、すべてのラジオボタンには独自の 'initialdir'があります。価値のあるチップをありがとう、それを忘れてしまった。最後に、私は 'askopenfilename'でファイルを返すコードを見つけましたが、うまくいきません。代わりに私は何を使うべきですか? – Cucumber12
それはあなたがファイルで何をしたいかによって決まります。関数内で行うこともできますし、ファイル名を格納する属性、 'self.myfile = filename'を作成して他の関数で使用することもできます。 –