2016-05-31 20 views
0

ファイルパスをファイルに保存しようとしています。そのため、アプリケーションを開いたときにファイルパスを読み込んで再入力する必要がありません。これを行うために、私はpickleモジュールを使ってデータをファイルに "ダンプ"しようとしています。しかし、次のエラーが表示されます。オブジェクトを保存しようとしましたが、ttkクラスを削除できません

Traceback (most recent call last): 
    File "C:\Program Files\python34\lib\tkinter\__init__.py", line 1533, in __call__ 
    return self.func(*args) 
    File "H:\Documents\Python Scripts\GUI.py", line 71, in <lambda> 
    ttk.Button(mainframe, text="Browse", command=lambda:askfile(usbpath)).grid(column=2,row=2,sticky = E) 
    File "H:\Documents\Python Scripts\GUI.py", line 26, in askfile 
    pickle.dump(pathtype, f) 
_pickle.PicklingError: Can't pickle <class 'tkapp'>: attribute lookup tkapp on __main__ failed 

私はこれを避ける方法を理解していません。私の完全なコードは以下の通りです:

#Modules 
from tkinter import * 
from tkinter import ttk 
from tkinter.filedialog import askopenfilename 
import pickle 
import shutil 

#Functions 
def popup(info): 
    popup = Tk() 
    popup.wm_title("Attention") 
    popup.attributes("-toolwindow",1) 
    label = ttk.Label(popup, text=info) 
    label.pack(side="top", fill="x", pady=10) 
    ttk.Button(popup, text="OK", command=popup.destroy).pack() 
    popup.mainloop() 

def stopProg(e): 
    root.destroy() 
def statuslabel(window, message): 
    ttk.Label(window, text=message).grid(column=2, row=1) 
def askfile(pathtype): 
    filename = askopenfilename(parent=mainframe,title='Choose a file') 
    pathtype.set(filename) 
    f = open('store.pickle','wb') 
    pickle.dump(pathtype, f) 
    f.close() 

def copyfiles(src,dst): 
    sourcefile = src.get() 
    if sourcefile.endswith(".txt"): 
     try: 
      shutil.copy(dst.get(),r'H:\Documents\folder\backups') 
      shutil.copy(sourcefile,dst.get()) 
      statuslabel(mainframe, "Done") 
     except IOError: 
      statuslabel(mainframe, "Error") 
      popup("Error occured: Please check your paths") 
    else: 
     popup("Bad filetype or no file selected") 
#GUI 
root = Tk() 
root.title('PPSSPP Save Management') 
mainframe = ttk.Frame(root, padding = "3 3 12 12") 
mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) 
mainframe.columnconfigure(0, weight=1) 
mainframe.rowconfigure(0, weight=1) 

try: 
    open('store.pickle','x').close() 
except FileExistsError: 
    statuslabel(mainframe, "Load") 

f = open('store.pickle', 'rb') 
try: 
    usbpath = StringVar() 
    usbpath.set(pickle.load(f)) 
except EOFError: 
    usbpath = StringVar() 
try: 
    homepath = StringVar() 
    homepath.set(pickle.load(f)) 
except EOFError: 
    homepath = StringVar() 
f.close() 

usbpath_entry = ttk.Entry(mainframe,width = 60,textvariable=usbpath) 
usbpath_entry.grid(column=1, row=2, sticky=(W,E)) 
ttk.Label(mainframe, text="USB(PSP) Path").grid(column=1,row=1,sticky=W) 
ttk.Button(mainframe, text="Browse", command=lambda:askfile(usbpath)).grid(column=2,row=2,sticky = E) 
ttk.Label(mainframe, text="PC (PPSSPP) Path").grid(column=1,row=3,sticky=W) 
homepath_entry = ttk.Entry(mainframe,width = 60,textvariable=homepath) 
homepath_entry.grid(column=1,row=4, sticky=(W,E)) 
ttk.Button(mainframe, text="Browse", command=lambda:askfile(homepath)).grid(column=2, row =4,sticky=E) 

ttk.Button(mainframe, text="USB -> PC", command=lambda:copyfiles(usbpath,homepath)).grid(column=2,row=5,sticky=(N,S,W,E)) 
ttk.Button(mainframe, text="PC -> USB", command=lambda:copyfiles(homepath,usbpath)).grid(column=2,row=6,sticky=(N,S,W,E)) 
+2

ウィジェット内のテキストではなく、ウィジェットをpickleしようとしています。それは本当にあなたがしたいことですか?ファイルにパスを書き込むのではなく、なぜピクルを使用していますか? –

+0

私はウィジェットを格納する必要はありません、私はウィジェット内のテキスト(ファイルパス)を格納する必要があります。 – SolidSnackDrive

答えて

1

をこのボタンはaskfile呼び出し、それをSTRINGVAR()

ttk.Button(mainframe, text="Browse", command=lambda:askfile(usbpath)).grid(column=2,row=2,sticky = E) 

機能は、その後STRINGVAR、ないファイル名を酸洗しようとするが送信

def askfile(pathtype): ## pathtype=StrinVar 
    ## omitted code 
    pickle.dump(pathtype, f) 
+1

その代わりにOPは....(あなたの答えは少し欠けているようです) –

+1

OPが何をするつもりか分からないので、なぜエラーメッセージが表示されたのか説明できません。一般的には間違っていると仮定し、すべてのスレッドに対して一般的なルールとして無駄な時間をもたらします。 –

+1

@CurlyJoe:質問の最初の言葉は、 "ファイルパスをファイルに保存しようとしています"です。私は実際にウィジェットではなく、実際のウィジェットからのテキストだけが必要であることを尋ねました。 –

関連する問題