# -*- coding: utf-8 -*-
import Tkinter as tk
import ttk
import sys
import os
from pprint import pprint
import pickle
class TestApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.varFilename = tk.StringVar()
self.etyFilename = ttk.Entry(master=self, textvariable=self.varFilename)
self.frmBtn = ttk.Frame(master=self)
self.btnSave = ttk.Button(master=self.frmBtn, text='Save',
command=self.save)
self.btnOpen = ttk.Button(master=self.frmBtn, text='Open',
command=self.openFile)
self.etyFilename.grid(row=0, column=0, sticky=tk.EW, padx=3, pady=3)
self.frmBtn.grid(row=1, column=0, sticky=tk.EW)
self.btnSave.grid(row=0, column=0, sticky=tk.SE, padx=3, pady=3)
self.btnOpen.grid(row=0, column=1, sticky=tk.SE, padx=3, pady=3)
self.mainloop()
def save(self, *args):
someData = {'foo': 'bar'}
filename = self.varFilename.get()
try:
with open(filename, 'wb') as f:
pickle.dump(someData, f)
except IOError, e:
print e
sys.exit(1)
def openFile(self, *args):
filename = self.varFilename.get()
print filename
print 'Files in cwd:'
pprint(os.listdir(os.getcwd()))
try:
with open(filename, 'rb') as f:
someData = pickle.load(f)
print someData
except IOError, e:
print e
sys.exit(1)
TestApp()
您正在使用哪個版本的Python?在Python 2和Python 3之間,Unicode處理是完全不同的。 – Blckknght
我正在使用Python 2.7。並在腳本開頭聲明'# - * - coding:utf-8 - * - '。 –
旁註*當我列出當前工作目錄中的文件時,它們不是utf-8編碼。* - >請注意,您的終端可能無法正確顯示它們的事實並不意味着Python不會正確處理它們。你能發佈你的實際代碼嗎? – patrick