我正在使用python:我将unicode文本保存为'.txt文件扩展时出错

时间:2015-04-13 14:11:00

标签: python-2.7 tkinter

from Tkinter import *
from  tkFileDialog import *
import tkMessageBox
import Tkinter as tk
import Translator
from PyQt4.QtCore import *
try:
    _fromUtf8 = QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

app = Tk()
app.title("Eng2Yor (PRONOUN)")
app.geometry("700x500+300+50")

menubar = Menu(app)

#For File Menu
filemenu = Menu(menubar, tearoff = 0)
def open():
    f = askopenfile(mode= 'r')
    t = f.read()
    text1.delete(0.0, END)
    text1.insert(0.0, t)
filemenu.add_command(label = "Open       Ctrl + O", command = open)
filemenu.add_separator()
def Save():
    name=asksaveasfile(mode='w',defaultextension=".txt")
    text2save=str(text2.get(0.0,END))
    name.write(text2save)
filemenu.add_command(label = "Save", command = Save)
def SaveAs():
    name=asksaveasfile(mode='w',defaultextension=".txt")
    text2save=str(text2.get(0.0,END))
    name.write(text2save)
filemenu.add_command(label = "Save As  Ctrl + S", command = SaveAs)

filemenu.add_separator()
def exit():
    if tkMessageBox.askyesno("Quit", "Do you really want to quit?"):
        app.destroy()
filemenu.add_command(label="Exit", command=exit)

#For View Menu
viewmenu = Menu(menubar, tearoff = 0)
def Translate():
    translate()
viewmenu.add_command(label = "Translate", command = Translate)
def Paste():
    try:
        t = app.selection_get(selection='CLIPBOARD')
        text1.insert(INSERT, t)
    except TclError:
        pass
viewmenu.add_command(label = "Paste", command = Paste)

#For Menus
menubar.add_cascade(label = "File", menu = filemenu)
menubar.add_cascade(label = "Translate", menu = viewmenu)

#For Help Menu
def about():
    tkMessageBox.showinfo("About", "Just an ENGLISH to YORUBA \nPronoun     Translation Machine System \nWhich replaces gender pronouns\nIn yoruba     equivalence")
helpmenu = Menu(menubar, tearoff = 0)
helpmenu.add_cascade(label = "About", command = about)
def help():
    tkMessageBox.showinfo("About", "It replaces gender pronoun\n'He', 'She'     and 'It'")
helpmenu.add_cascade(label = "Help", command = help)
menubar.add_cascade(label = "Help", menu = helpmenu)

app.config(menu = menubar)

#Toolbar
def translate():
    text2.delete(1.0, END)
    s = text1.get(1.0,END)
    text2.insert(1.0,Translator.translate(s))

def reset():
    text1.delete(1.0, END)
    text2.delete(1.0, END)

toolbar = Frame(app, bg = "#555")
translatebutton = tk.Button(toolbar, font = ('arial black', 12),text =     "Translate", bg = "#266", fg = "#fff", borderwidth=2, relief =     "raised",command=translate)
translatebutton.pack(side="left", expand=1, fill= X)
resetbutton = tk.Button(toolbar, font = ('arial black', 12),text = "Reset",     bg = "#266", fg = "#fff", borderwidth=2, relief = "raised",command=reset)
resetbutton.pack(side="left", expand=1, fill= X)
clossbutton = tk.Button(toolbar, font = ('arial black', 12),text = "Close",     bg = "#266", fg = "#fff", borderwidth=2, relief = "raised",command=exit)
clossbutton.pack(side="left", expand=1, fill= X)
toolbar.pack(side = TOP, fill = X, ipady = 5, ipadx = 15)

#Frames
topFrame = Frame(app, bg = "#bbb", relief=RIDGE)
topFrame.pack(side = TOP, anchor = W, fill = X)
englishtext = Label(topFrame, bg = "#555", font = ('arial black', 12), fg =     "#fff",text="ENGLISH SENTENCE")
englishtext.pack(side="left", expand=1, fill= X)
yorubatext = Label(topFrame, bg = "#555", font = ('arial black', 12), fg =     "#fff",text="YORUBA TRANSLATION")
yorubatext.pack(side="left", expand=1, fill= X)

bottomFrame = Frame(app, relief=FLAT, borderwidth=10, bg = "#000")
bottomFrame.pack(fill = BOTH, expand = 1, anchor = CENTER)

insideFrame = Frame(bottomFrame, relief=FLAT, borderwidth = 0)
insideFrame.pack(fill = BOTH, expand = 1, anchor = CENTER)

#Text windows
text1 = Text(insideFrame)
text1.config(font=("consolas", 12), undo=True, wrap='word')
text1.pack(side = LEFT, fill = BOTH, expand = 1)

text2 = Text(insideFrame)
text2.config(font=("consolas", 12), undo=True, wrap='word')
text2.pack(side = LEFT, fill = BOTH, expand = 1)

scrollbar = Scrollbar(text1)
scrollbar.pack(side=RIGHT, fill=Y)
scrollbar.config(command=text1.yview)
text1.config(yscrollcommand=scrollbar.set)

scrollbar = Scrollbar(text2)
scrollbar.pack(side=RIGHT, fill=Y)
scrollbar.config(command=text2.yview)
text2.config(yscrollcommand=scrollbar.set)

app.mainloop()

It gives the following error: messagetext2save=str(text2.get(0.0,END)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 2: ordinal not in range(128)

任何人都可以帮我修复此代码。我在这里要做的是,当我在我的应用程序中输入英文文本时,它会将其翻译成另一种语言,当我将输出保存到" .txt"包含超出ASCII字符范围的unicodes的文件,它会出错,所以任何人都可以帮我修复上面的代码。我需要的是能够将文本内容保存到" .txt文件中#34;。

0 个答案:

没有答案