は、あなたが探しているものは以下です:
from tkinter import *
from tkinter.filedialog import *
root = Tk()
root.title("Save and Load")
root.geometry("600x500-400+50")
def importFiles():
try:
filenames = askopenfilenames()
global file
for file in filenames:
fileList.insert(END, file)
except:
pass
def removeFiles():
try:
fileList.delete(fileList.curselection())
except:
pass
def openFile():
try:
text.delete(END)
fob = open(file, 'r')
text.insert(0.0, fob.read())
except:
pass
def saveFile():
try:
fob = open(file, 'w')
fob.write(text.get(0.0, 'end-1c'))
fob.close()
except:
pass
listFrame = Frame(root)
listFrame.pack()
sby = Scrollbar(listFrame, orient='vertical')
sby.pack(side=RIGHT, fill=Y)
fileList = Listbox(listFrame, width=100, height=5, yscrollcommand=sby.set)
fileList.pack()
sby.config(command=fileList.yview)
buttonFrame = Frame(root)
buttonFrame.pack()
importButton = Button(buttonFrame, text="Import", command=importFiles)
importButton.pack(side=LEFT)
removeButton = Button(buttonFrame, text="Remove", command=removeFiles)
removeButton.pack(side=LEFT)
openButton = Button(buttonFrame, text="Open", command=openFile)
openButton.pack(side=LEFT)
saveButton = Button(buttonFrame, text="Save", command=saveFile)
saveButton.pack(side=LEFT)
text = Text(root)
text.pack()
root.mainloop()
「私はセーブとロードの両方に使用することができ、ファイル名を返す1つのダイアログをしたいです。」
ダイアログウィンドウを使用してファイル名をインポートできます。選択したファイル名をリストから削除します(追加機能)。選択したファイルを開きます。最後に、それらを書き出して保存します。
P .:私のコードにはいくつかのバグがあるかもしれませんが、アルゴリズムが質問しています。
ファイル名ではなくディレクトリを指定してください! http://stackoverflow.com/questions/11295917/how-to-select-a-directory-and-store-the-location-using-tkinter-in-python – dsgdfg
ディレクトリを要求するとファイルを選択できませんすべて。 ( "フォルダC:\ test.txtは使用できません。別のフォルダを選択してください。") – acdr
保存するかどうかを確認するには、ファイル名を読んでください。 「SAVE」、「LOAD」の2つのボタンでトップレベルウィンドウを作成します。それは単純ではありません:ファイルがロックされている(平均オープン)場合、存在する場合、上書きなど... – dsgdfg