Помогите исправить ошибку expected str, bytes or os.PathLike object, not NoneType
Прошу прощения за неудобства кто пытался помочь. Ошибку выдаёт на этом фрагменте кода:
def dataInsert():
global tree
global count
global file
data = [dateEntry.get(), serviceEntry.get(), monthEntry.get(), borrowEntry.get(), spentEntry.get()]
if dateEntry.get() and serviceEntry.get() and monthEntry.get() and borrowEntry.get() and spentEntry.get():
with open(file, "a+"):
file.write(str(data) + "\n")
tree.insert(parent="", index="end", iid=count, values=data)
count+=1
clearEntry()
else:
messagebox.showwarning("Warning", "You should write something in the text boxes")
И собственно говоря какую ошибку выдаёт:
Traceback (most recent call last):
File "C:\Program Files (x86)\Thonny\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\5kara\OneDrive\Рабочий стол\pyProjects\bills\bills.py", line 90, in dataInsert
with open(file, "a+"):
TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter.ttk import *
import os
#initializing window
root = Tk()
root.geometry("640x480")
root.title("Bills")
genIco = PhotoImage(file="money.png")
root.iconphoto(False, genIco)
# initializating file
file = None
# creating tree.
tree = Treeview(root)
#initializingcolumns
tree["columns"] = ("Date", "Service", "Month", "Borrow", "Spent")
#setting up columns
tree.column("#0", width=40, minwidth=40 , anchor=W, stretch=False)
tree.column("Date", width=120, minwidth=120, anchor=W, stretch=False)
tree.column("Service", width=160, minwidth=160, anchor=W, stretch=False)
tree.column("Month", width=120, minwidth=120, anchor=W, stretch=False)
tree.column("Borrow", width=90, minwidth=90, anchor=W, stretch=False)
tree.column("Spent", width=90, minwidth=90, anchor=W, stretch=False)
#setting up headings
tree.heading("#0", text="№", anchor=W)
tree.heading("Date", text="Date", anchor=W)
tree.heading("Service", text="Service", anchor=W)
tree.heading("Month", text="Month", anchor=W)
tree.heading("Borrow", text="Borrow", anchor=W)
tree.heading("Spent", text="Spent", anchor=W)
#count
global count
count=0
#creating entry's
dateEntry = Entry(root, width=20)
serviceEntry = Entry(root, width=20)
monthEntry = Entry(root, width=20)
borrowEntry = Entry(root, width=20)
spentEntry = Entry(root, width=20)
# ========================================================================================
# ====================================MENU===============================================
# def createNew():
#
def openFile():
global file
file = root.filename = filedialog.askopenfilename(initialdir="/bills", title="Open a file", filetypes=(("Text Document", "*.txt"), ("All files", "*.*")))
with open(file) as file:
lines = file.readlines()
for line in lines:
data = line
tree.insert(parent="", index="end", iid=count, values=data)
# ========================================================================================
# ====================================BUTTONS============================================
#clearEntry
def clearEntry():
dateEntry.delete(0, END)
serviceEntry.delete(0, END)
monthEntry.delete(0, END)
borrowEntry.delete(0, END)
spentEntry.delete(0, END)
#fillEntry
def fillEntry():
selected_item = tree.focus()
details = tree.item(selected_item)
date = details.get("values")[0]
service = details.get("values")[1]
month = details.get("values")[2]
borrow = details.get("values")[3]
spent = details.get("values")[4]
dateEntry.insert(0, date)
serviceEntry.insert(0, service)
monthEntry.insert(0, month)
borrowEntry.insert(0, borrow)
spentEntry.insert(0, spent)
#autoFillEntry
# def autoFill():
# if tree.selection:
# fillEntry()
#insert
def dataInsert():
global tree
global count
global file
data = [dateEntry.get(), serviceEntry.get(), monthEntry.get(), borrowEntry.get(), spentEntry.get()]
if dateEntry.get() and serviceEntry.get() and monthEntry.get() and borrowEntry.get() and spentEntry.get():
with open(file, "a+"):
file.write(str(data) + "\n")
tree.insert(parent="", index="end", iid=count, values=data)
count+=1
clearEntry()
else:
messagebox.showwarning("Warning", "You should write something in the text boxes")
#deleteAll
def dataDeleteAll():
global tree
global count
global file
for record in tree.get_children():
tree.delete(record)
count-=count
path = os.path.join(os.path.abspath(os.path.dirname(__file__)), file)
os.remove(path)
clearEntry()
#deleteLog
def dataDelete():
global tree
global count
global file
data = [dateEntry.get(), serviceEntry.get(), monthEntry.get(), borrowEntry.get(), spentEntry.get()]
dele = tree.selection()[0]
tree.delete(dele)
with open(file):
lines = file.readlines()
with open(file, "w"):
for line in lines:
if line.strip("\n") != str(data):
file.write(line)
count-=1
clearEntry()
# ========================================================================================
# ================================FINAL SETTINGS==========================================
#creating label's
dateLabel = Label(root,
text="Date")
serviceLabel = Label(root,
text="Service")
monthLabel = Label(root,
text="Month")
borrowLabel = Label(root,
text="Borrow")
spentLabel = Label(root,
text="Spent")
#creating buttons
submitButton=Button(root, width=35, text="Submit", command = dataInsert)
deleteAllButton=Button(root, width=13, text="Delete All", command=dataDeleteAll)
deleteOneButton = Button(root, width=13, text="Delete One", command=dataDelete)
entryFill = Button(root, width=13, text="Fill Entry's", command=fillEntry)
#creating menu
menu = Menu(root)
root.config(menu=menu)
fileMenu = Menu(menu,tearoff=0)
menu.add_cascade(label="File",menu=fileMenu)
# add some commands in fileMenu
fileMenu.add_command(label="New")
fileMenu.add_command(label="Open", command=openFile)
fileMenu.add_command(label="Save")
fileMenu.add_separator()
fileMenu.add_command(label="Settings")
fileMenu.add_command(label="Exit")
#placing widgets
dateEntry.place(x=100, y =260)
serviceEntry.place(x=100, y =290)
monthEntry.place(x=100, y =320)
borrowEntry.place(x=100, y =350)
spentEntry.place(x=100, y =380)
dateLabel.place(x=9, y=260)
serviceLabel.place(x=9, y=290)
monthLabel.place(x=9, y=320)
borrowLabel.place(x=9, y=350)
spentLabel.place(x=9, y=380)
submitButton.place(x=9, y=410)
deleteAllButton.place(x=230, y=259)
deleteOneButton.place(x=230, y=289)
entryFill.place(x=230, y=319)
tree.pack()
#run
# autoFill()
root.mainloop()
Источник: Stack Overflow на русском