【问题标题】:GUI App shows File not found in directory ErrorGUI 应用程序显示在目录中找不到文件错误
【发布时间】:2021-08-05 09:40:17
【问题描述】:

我为 python 程序制作了一个 GUI,它允许我读取 csv 文件并在 GUI 上显示数据。当我通过 IDLE 运行程序时,GUI 工作得非常好。但是,当我尝试从 Windows 资源管理器中运行程序文件时,它会显示以下错误。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Python\gui1.py", line 43, in find_result
    with open("RESULTS.csv") as a:
FileNotFoundError: [Errno 2] No such file or directory: 'RESULTS.csv'

这是程序代码:

import tkinter as tk
import csv


root=tk.Tk()
root.title('Results')

tk.Label(root,text="Enter your Roll Number:").grid(row=1,column=1,sticky=tk.W)

tk.Label(root,text="Name:").grid(row=4,column=1,sticky=tk.W)

tk.Label(root,text="Total (Core 5 Subjects):").grid(row=5,column=1,sticky=tk.W)

tk.Label(root,text="Total (Best 5 Subjects):").grid(row=6,column=1,sticky=tk.W)

tk.Label(root,text="Percentage:").grid(row=7,column=1,sticky=tk.W)

tk.Label(root,text="Rank:").grid(row=8
                                 ,column=1,sticky=tk.W)

rno=tk.StringVar()


name_=tk.StringVar()
t1_=tk.StringVar()
t2_=tk.StringVar()
perc_=tk.StringVar()
rank_=tk.StringVar()
                   
tk.Entry(root, textvariable=rno,justify=tk.RIGHT).grid(row=1,column=2,padx=3,pady=3)
def find_result():
    

    tk.Label(root,textvariable=name_,justify=tk.RIGHT).grid(row=4,column=2,sticky=tk.E)

    tk.Label(root,textvariable=t1_,justify=tk.RIGHT).grid(row=5,column=2,sticky=tk.E)

    tk.Label(root,textvariable=t2_,justify=tk.RIGHT).grid(row=6,column=2,sticky=tk.E)

    tk.Label(root,textvariable=perc_,justify=tk.RIGHT).grid(row=7,column=2,sticky=tk.E)

    tk.Label(root,textvariable=rank_,justify=tk.RIGHT).grid(row=8,column=2,sticky=tk.E)

    with open("RESULTS.csv") as a:
        reader=csv.reader(a)
        for i in reader:
            if i!=[]:
                if i[0]==rno.get():
                    name=i[1][2:]
                    t1=i[19]
                    t2=i[20]
                    perc=i[21]
                    rank=i[22]

                    name_.set(name)
                    t1_.set(t1)
                    t2_.set(t2)
                    perc_.set(perc)
                    rank_.set(rank)


tk.Button(root, text="Check your Result",command=find_result).grid(row=2,column=2)

root.mainloop()

用 IDLE 打开程序:

通过资源管理器打开程序:

【问题讨论】:

    标签: python csv user-interface python-idle


    【解决方案1】:

    open("RESULTS.csv") 在当前工作目录中查找该文件,不管它是什么。对于运行代码的两种不同方式,这必须不同。执行以下操作之一。

    1. open 一个文件的绝对路径而不是相对路径。如果您知道数据文件相对于程序文件位置的位置,则使用具有程序文件位置的__file__ 作为起点。注意 1:当以交互方式工作时,不运行文件,__file__ 不存在。注 2:如果您不了解“绝对路径”,请进行网络搜索。

    2. 将当前目录显式设置为包含os.chdir(path) 文件的目录。其中路径是绝对的,而不是相对的。如果在该目录中打开多个文件,这会更有用。

    【讨论】:

      猜你喜欢
      • 2019-11-10
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多