Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python I/O错误,无法在listbox上实现文件_Python_Csv_Tkinter - Fatal编程技术网

Python I/O错误,无法在listbox上实现文件

Python I/O错误,无法在listbox上实现文件,python,csv,tkinter,Python,Csv,Tkinter,我写了这段代码,控制台出现了一个错误,我尝试了几种无法修复的方法,你能解释一下我的错误在哪里吗。多谢各位 import os os.chdir(r"C:\Users\Umer Selmani\Desktop\prog.practice\MP1") import csv with open('FoodDB.csv','r') as FDB: file = csv.reader(FDB) for line in file: print(line) from t

我写了这段代码,控制台出现了一个错误,我尝试了几种无法修复的方法,你能解释一下我的错误在哪里吗。多谢各位

import os
os.chdir(r"C:\Users\Umer Selmani\Desktop\prog.practice\MP1")

import csv


with open('FoodDB.csv','r') as FDB:
    file = csv.reader(FDB)
    for line in file:
        print(line)

from tkinter import *

class Diet:
    def __init__(self):
        self.Left = Frame(root,width= 250,height=200,bg='salmon')
        self.Left.pack(side=LEFT,pady=5)

        self.labelrdL=Label(self.Left, text="Food Menu").grid(row=0)
        self.listboxrdL=Listbox(self.Left,width=30).grid(row=1)
        for q in file:
            self.listboxrdL.insert(END,q)
        self.buttonrdL=Button(self.Left, text="Continue",bg="red").grid(row=2)

root = Tk()
diet = Diet()   # Create an instance of Diet.
root.mainloop()
这里有一个错误

Traceback (most recent call last):
  File "C:/Users/Umer Selmani/.PyCharmEdu2018.2/config/scratches/scratch_3.py", line 26, in <module>
    diet = Diet()   # Create an instance of Diet.
  File "C:/Users/Umer Selmani/.PyCharmEdu2018.2/config/scratches/scratch_3.py", line 21, in __init__
    for q in file:
ValueError: I/O operation on closed file.

Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“C:/Users/Umer Selmani/.PyCharmEdu2018.2/config/scratches/scratch_3.py”,第26行,在
diet=diet()#创建一个diet实例。
文件“C:/Users/Umer Selmani/.PyCharmEdu2018.2/config/scratches/scratch_3.py”,第21行,在u_init中__
对于文件中的q:
ValueError:对关闭的文件执行I/O操作。
进程已完成,退出代码为1

您的“打开”将自动关闭文件。因此,您得到了错误

用open('FoodDB.csv','r')代替
,作为FDB:
尝试
FDB=open('FoodDB.csv','r')

完成后不要忘记手动关闭文件

FDB.close()
完整解决方案应如下所示:

import os
os.chdir(r"C:\Users\Umer Selmani\Desktop\prog.practice\MP1")

import csv


FDB = open('FoodDB.csv','r')
file = csv.reader(FDB)
#for line in file:
   #print(line)

from tkinter import *

class Diet:
    def __init__(self):
        self.Left = Frame(root,width= 250,height=200,bg='salmon')
        self.Left.pack(side=LEFT,pady=5)

        self.labelrdL=Label(self.Left, text="Food Menu").grid(row=0)
        self.listboxrdL=Listbox(self.Left,width=30).grid(row=1)
        for q in file:
            self.listboxrdL.insert(END,q)
        self.buttonrdL=Button(self.Left, text="Continue",bg="red").grid(row=2)

root = Tk()
diet = Diet()   # Create an instance of Diet.
root.mainloop()

您的“打开”将自动关闭文件。因此,您得到了错误

用open('FoodDB.csv','r')代替
,作为FDB:
尝试
FDB=open('FoodDB.csv','r')

完成后不要忘记手动关闭文件

FDB.close()
完整解决方案应如下所示:

import os
os.chdir(r"C:\Users\Umer Selmani\Desktop\prog.practice\MP1")

import csv


FDB = open('FoodDB.csv','r')
file = csv.reader(FDB)
#for line in file:
   #print(line)

from tkinter import *

class Diet:
    def __init__(self):
        self.Left = Frame(root,width= 250,height=200,bg='salmon')
        self.Left.pack(side=LEFT,pady=5)

        self.labelrdL=Label(self.Left, text="Food Menu").grid(row=0)
        self.listboxrdL=Listbox(self.Left,width=30).grid(row=1)
        for q in file:
            self.listboxrdL.insert(END,q)
        self.buttonrdL=Button(self.Left, text="Continue",bg="red").grid(row=2)

root = Tk()
diet = Diet()   # Create an instance of Diet.
root.mainloop()

这里有几件事你需要做

您应该遵循PEP8准则来命名变量。在文件开头定义所有导入。最后也是最重要的一点是,在更新列表框时,您应该使用
with open
语句。请记住,
with open
对应于
open
将在
with
语句结束后自动关闭文件

此外,这里的这一行
self.listboxrdL=Listbox(self.Left,width=30)。grid(row=1)
将阻止您向列表框添加信息

通过执行以下操作,确保在新行上使用几何图形管理器以防止出现问题:

self.listboxrdL = Listbox(self.Left,width=30)
self.listboxrdL.grid(row=1)
代码:


这里有几件事你需要做

您应该遵循PEP8准则来命名变量。在文件开头定义所有导入。最后也是最重要的一点是,在更新列表框时,您应该使用
with open
语句。请记住,
with open
对应于
open
将在
with
语句结束后自动关闭文件

此外,这里的这一行
self.listboxrdL=Listbox(self.Left,width=30)。grid(row=1)
将阻止您向列表框添加信息

通过执行以下操作,确保在新行上使用几何图形管理器以防止出现问题:

self.listboxrdL = Listbox(self.Left,width=30)
self.listboxrdL.grid(row=1)
代码:


正常读取工作,但不使用语句,而是按照我上面的回答直接打开文件。让我知道进展如何。我的计算机中没有所有程序包,无法测试。谢谢fam,但列表框中没有项目:(?我认为,这是因为您已经读取了文件中的所有内容。删除或注释掉开头的for循环和打印行,看看是否有任何变化。修改了答案,即注释掉两行,以跳过文件开头的文件内容。就我个人而言,我会将with open语句直接用于
用于文件中的q
循环。正常读取工作,但不使用语句,而是按照上面的回答直接打开文件。让我知道它是如何运行的。我的计算机中没有所有软件包,无法测试它。谢谢fam,但列表框中没有项目:(?我认为,这是因为您已经读取了文件中的所有内容。删除或注释掉开头的for循环和打印行,看看是否有任何变化。修改了答案,即注释掉两行,以跳过文件开头的文件内容。就我个人而言,我会将with open语句直接用于
用于文件中的q
循环。