Я пытаюсь добавить функцию, которая позволяет мне открывать текстовый файл в блокноте, встроенном в python, но эта ошибка показывает TypeError: ожидаемый объект str, bytes или os.PathLike, а не список
Я действительно новичок в программировании, и я следую этому уроку о том, как сделать блокнот на python, я пытался импортировать os, но я не знал, как его использовать.Заранее спасибо
from tkinter import Tk, scrolledtext, Menu, filedialog, END, messagebox
from tkinter.scrolledtext import ScrolledText
from tkinter import*
#Root main window
root = Tk(className=" Text Editor")
textarea = ScrolledText(root, width=80, height=100)
textarea.pack()
# Menu options
menu = Menu(root)
root.config(menu=menu)
filename = Menu(menu)
edicion = Menu(menu)
# Funciones File
def open_file ():
file = filedialog.askopenfiles(parent=root, mode='r+', title="Select a file")
if file == None:
contenidos = file.read()
textarea.insert('1.0', contenidos)
file.close
else:
root.title(" - Notepad")
textarea.delete(1.0,END)
file = open(file,"r+")
textarea.insert(1.0,file.read)
file.close()
def savefile ():
file = filedialog.asksaveasfile(mode='w')
if file!= None:
data = textarea.get('1.0',END+'-1c')
file.write(data)
file.close()
def exit():
if messagebox.askyesno ("Exit", "Seguro?"):
root.destroy()
def nuevo():
if messagebox.askyesno("Nuevo","Seguro?"):
file= root.title("Vistima")
file = None
textarea.delete(1.0,END)
#Funciones editar
def copiar():
textarea.event_generate("<<Copy>>")
def cortar():
textarea.event_generate("<<Cut>>")
def pegar():
textarea.event_generate("<<Paste>>")
#Menu
menu.add_cascade(label="File", menu=filename)
filename.add_command(label="New", command = nuevo)
filename.add_command(label="Open", command= open_file)
filename.add_command(label="Save", command=savefile)
filename.add_separator()
filename.add_command(label="Exit", command=exit)
menu.add_cascade(label="Editar", menu=edicion)
edicion.add_command(label="Cortar", command=cortar)
edicion.add_command(label="Pegar", command=pegar)
edicion.add_command(label="Copiar", command=copiar)
textarea.pack()
#Keep running
root.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\57314\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/57314/PycharmProjects/text_editor/bucky.py", line 28, in open_file
file = open(file,"r+")
TypeError: expected str, bytes or os.PathLike object, not list