UnicodeDecodeError:“字符映射”编解码器

时间:2019-12-24 17:46:30

标签: python tkinter filedialog

我的代码在下面。我正在尝试在python中创建一个Tkinter窗口,该窗口可以复制文本文件并将其粘贴到Tkinter窗口中。

我正在尝试使程序尽可能整洁和高效,但是该程序无法正常工作。我希望它在程序运行后立即用单词打开新的Tkinter窗口。

每当我运行该程序时,它都会打开文件,然后显示很多错误。我收到的一些错误消息是:

  

Tkinter回调中的异常

Traceback (most recent call last):   
File "C:\Users\sharm\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
     return self.func(*args)   
File "C:\Users\sharm\AppData\Local\Programs\Python\Python38-32\Stories.py", line 23, in slaves_of_the_north
     text1 = open(storiestxt).read()
File "C:\Users\sharm\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1252.py", line 23, in decode
     return codecs.charmap_decode(input,self.errors,decoding_table)[0] 
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 677: character maps to <undefined>

这是我的代码:

import Tkinter
from Tkinter import *
from Tkinter import filedialog
import os

root = Tk()
root.geometry("500x500")
root.title("Story")

btnrow1 = Frame(root,bg="#000")
btnrow1.pack(expand = True, fill = "both",)

def slaves_of_the_north():
    root = Tk()
    root.geometry("400x400")
    root.title("Slaves of the North")

    storiestxt = filedialog.askopenfilename \
                (filetypes=(("Text Stuff", "txt"), \
                            ("All files","*.*")))
    print(storiestxt)
    root.title()
    text1 = open(storiestxt).read()
    print(text1)

slaves_of_the_north = Button( btnrow1,
                              text = "Slaves of the North",
                              font = ("Verdana", 10),
                              relief = GROOVE,
                              border = 5,
                              command = slaves_of_the_north,
                             )

slaves_of_the_north.pack(side = LEFT, expand= True, fill = "both",)
root.mainloop()

0 个答案:

没有答案