Python中urllib和tkinter的错误
我在玩下面的代码,但不知道为什么出问题了,执行时什么都没有发生。
'''
Created on Jul 12, 2014
@author: nick
'''
import urllib2
import urllib
from Tkinter import *
#import tkinter.messagebox
#import turtle
from PIL import *
import PIL.Image
import os
import webbrowser
def FetchURI():
response = urllib2.urlopen(custom.get())
labelText.set('OK')
html = response.read()
with open("/home/nick/Desktop/Imagenet/URIs.txt", 'w') as outfile:
outfile.write(html)
return
def aboutMe():
url = ('https://blablabla/wikis/home')
webbrowser.open_new(url)
return
def openURIS():
PATH = "/home/nick/Desktop/Imagenet/URIs.txt"
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
print "File is there and readable"
else:
print "Either missing or nor readable"
f = open(PATH)
aboutURIS.delete(1.0, END)
URIstring = ""
for i in f:
URIstring += i
aboutURIS.insert(END, URIstring)
f.close()
return
#Download images
def download_img():
f = open("/home/nick/Desktop/Imagenet/URIs.txt")
lines = f.readlines()
f.close()
#print(lines)
for i in xrange(len(lines)):
#t = urllib2.urlopen(urllib2.Request(lines[i]))
image = urllib.URLopener()
image.retrieve(lines[i], "/home/nick/Desktop/Imagenet/img%s" % i)
#urllib.urlretrieve(lines[i], "/home/nick/Desktop/Imagenet/test%s" % i)
return
def openBB():
pass
def openFeatures():
pass
def openMap():
pass
app = Tk()
app.title('Simple Tkinter GUI')
app.geometry('400x500')
menubar = Menu(app)
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label="Open URIs", command=openURIS)
filemenu.add_command(label="Bounding Boxes", command=openBB)
filemenu.add_command(label="Features", command=openFeatures)
filemenu.add_command(label="Mapping", command=openMap)
filemenu.add_separator()
filemenu.add_command(label="Quit", command=app.quit)
menubar.add_cascade(label="File", menu=filemenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About us", command=aboutMe)
menubar.add_cascade(label="Help", menu=helpmenu)
app.config(menu=menubar)
aboutURIS = Text(app)
aboutURIS.insert(END, "Paste link into the little box to fetch URIs")
aboutURIS.pack()
labelText = StringVar()
labelText.set('Click button below')
label1 = Label(app, textvariable=labelText, height=4)
label1.pack()
#checkBoxVal = IntVar()
#checkBox1 = Checkbutton(app, variable=checkBoxVal, text="Hello?")
#checkBox1.pack()
custom = StringVar(None)
legend = Entry(app, textvariable=custom)
legend.pack()
button1 = Button(app, text='Click to fetch URIs', width=20, command=FetchURI)
button1.pack(side='top', padx=15 , pady=15)
app.mainloop()
这个函数是属于一个Tkinter图形界面的,我觉得它可能和问题无关。我也在努力理解这个看起来很傻的错误。URIs.txt
文件是存在的,在Imagenet
目录里,所以这里可能还有其他问题。也许是for
循环或者f.readlines
出错了?
有什么反馈吗?
(process:2505): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed
(firefox:2505): GLib-GObject-WARNING **: Attempt to add property GnomeProgram::sm-connect after class was initialised
(firefox:2505): GLib-GObject-WARNING **: Attempt to add property GnomeProgram::show-crash-dialog after class was initialised
(firefox:2505): GLib-GObject-WARNING **: Attempt to add property GnomeProgram::display after class was initialised
(firefox:2505): GLib-GObject-WARNING **: Attempt to add property GnomeProgram::default-icon after class was initialised
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "/home/nick/workspace/gui/src/simplegui.py", line 32, in openURIS
f = open("/home/nick/Desktop/Imagenet/URIs.txt")
IOError: [Errno 2] No such file or directory: '/home/nick/Desktop/Imagenet/URIs.txt'
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "/home/nick/workspace/gui/src/simplegui.py", line 32, in openURIS
f = open("/home/nick/Desktop/Imagenet/URIs.txt")
IOError: [Errno 2] No such file or directory: '/home/nick/Desktop/Imagenet/URIs.txt'
1405329770543 addons.repository WARN Unknown type id when parsing addon: 5
1405329770551 addons.repository WARN Unknown type id when parsing addon: 5
1405329771098 addons.update-checker WARN Update manifest for ubufox@ubuntu.com did not contain an updates property
1405329771578 addons.update-checker WARN Update manifest for {972ce4c6-7e08-4474-a285-3208198ce6fd} did not contain an updates property
1405329771586 addons.update-checker WARN Update manifest for {2e1445b0-2682-11e1-bfc2-0800200c9a66} did not contain an updates property
1405329771624 addons.update-checker WARN Update manifest for online-accounts@lists.launchpad.net did not contain an updates property
1405329772296 addons.update-checker WARN Update manifest for webapps-team@lists.launchpad.net did not contain an updates property
1 个回答
-1
这是文件或文件权限的问题。
File "/home/nick/workspace/gui/src/simplegui.py", line 32, in openURIS
f = open("/home/nick/Desktop/Imagenet/URIs.txt")
IOError: [Errno 2] No such file or directory: '/home/nick/Desktop/Imagenet/URIs.txt'
请检查一下 /home/nick/Desktop/Imagenet/URIs.txt
这个文件是否存在?如果存在的话,它有没有读取的权限呢?