【发布时间】: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