即使正确缩进,下面的代码仍显示关闭文件的I / O错误。错误可能从何而来?还要检查一下函数调用,我是一个初学者,那里也可能有错误。
from tkinter import *
from tkinter import ttk
import csv
win= Tk()
win.resizable(0, 0)
win.title('ADD PRODUCT')
global text_input
global int_input1
global int_input2
global int_input3
global int_input4
text_input=StringVar()
int_input1=IntVar()
int_input2=IntVar()
int_input3=IntVar()
int_input4=IntVar()
with open('products_database.csv', 'w', newline='') as x:
fieldnames=('Barcode', 'item', 'sales_price', 'purchase_price', 'reorder_level')
writer=csv.DictWriter(x, fieldnames=fieldnames)
writer.writeheader()
def saver():
with open('products_database.csv', 'a', newline='') as y:
writer.writerow({'Barcode':int_input1.get(), 'item':text_input.get(),'sales_price':int_input2.get(),'purchase_price':int_input3.get(), 'reorder_level':int_input4.get()})
y.close()
def clear():
operator=text_input.set('')
operator=int_input1.set('')
operator=int_input2.set('')
operator=int_input3.set('')
operator=int_input4.set('')
a=Label(win, text='Scan barcode').grid(column=0, row=0)
b=Entry(win, text=int_input1).grid(column=1, row=0)
c=Label(win, text='Item').grid(column=0, row=1)
d=Entry(win, text=text_input).grid(column=1, row=1)
e=Label(win, text='Sale price').grid(column=0, row=2)
f=Entry(win, text=int_input2).grid(column=1, row=2)
h=Label(win, text='Purchase price').grid(column=0, row=3)
i=Entry(win, text=int_input3).grid(column=1, row=3)
j=Label(win, text='Reorder level').grid(column=0, row=4)
k=Entry(win, text=int_input4).grid(column=1, row=4)
l=Button(win, text='Clear', width=20, command=clear).grid(column=0, row=5, columnspan=1)
m=Button(win, text='Save', width=20, command=saver).grid(column=1, row=5)
答案 0 :(得分:0)
当您定义一个使用其外部作用域中的变量的函数时,它将保留对这些变量的引用,以便在调用它时可以使用它们,但是当您使用with
语句时,文件将在退出时关闭因此,在您调用saver
x
时,它已经关闭了,然后尝试使用writer
,它使用x
,但失败。尝试在保存标头的部分之外定义saver
函数,并使自己的csv编写器像这样:
from tkinter import *
from tkinter import ttk
import csv
win= Tk()
win.resizable(0, 0)
win.title('ADD PRODUCT')
global text_input
global int_input1
global int_input2
global int_input3
global int_input4
text_input=StringVar()
int_input1=IntVar()
int_input2=IntVar()
int_input3=IntVar()
int_input4=IntVar()
with open('products_database.csv', 'w') as x:
fieldnames=('Barcode', 'item', 'sales_price', 'purchase_price', 'reorder_level')
writer=csv.DictWriter(x, fieldnames=fieldnames)
writer.writeheader()
def saver():
with open('products_database.csv', 'a') as y:
writer=csv.DictWriter(y, fieldnames=fieldnames)
writer.writerow({'Barcode':int_input1.get(), 'item':text_input.get(),'sales_price':int_input2.get(),'purchase_price':int_input3.get(), 'reorder_level':int_input4.get()})
def clear():
operator=text_input.set('')
operator=int_input1.set('')
operator=int_input2.set('')
operator=int_input3.set('')
operator=int_input4.set('')
a=Label(win, text='Scan barcode').grid(column=0, row=0)
b=Entry(win, text=int_input1).grid(column=1, row=0)
c=Label(win, text='Item').grid(column=0, row=1)
d=Entry(win, text=text_input).grid(column=1, row=1)
e=Label(win, text='Sale price').grid(column=0, row=2)
f=Entry(win, text=int_input2).grid(column=1, row=2)
h=Label(win, text='Purchase price').grid(column=0, row=3)
i=Entry(win, text=int_input3).grid(column=1, row=3)
j=Label(win, text='Reorder level').grid(column=0, row=4)
k=Entry(win, text=int_input4).grid(column=1, row=4)
l=Button(win, text='Clear', width=20, command=clear).grid(column=0, row=5, columnspan=1)
m=Button(win, text='Save', width=20, command=saver).grid(column=1, row=5)
您也不需要调用y.close()
,因为它位于with
语句内(尽管这不是错误的根源,但是文件可以关闭多次,但只能关闭第一个)实际上会产生任何效果)