UnicodeDecodeError:“charmap”代码

2024-10-04 03:29:33 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我正在努力使程序尽可能整洁和高效,但程序不工作。我想它打开新的Tkinter窗口与文字后,立即程序运行

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

Exception in Tkinter callback

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()

Tags: oftheinimport程序tkinterlocalroot