urllib和tkin的Python错误

2024-09-30 12:19:43 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在玩下面的代码,但由于我不知道的原因,有些东西是错误的,当执行时,什么也没有发生。在

    '''
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 GUI,我不觉得它有什么关系。我也很难理解这个相当愚蠢的错误。URIs.txt文件存在于Imagenet目录中,因此这里肯定有其他错误。可能是for循环或f.readlines?在

有什么反馈吗?在

^{pr2}$

Tags: importaddapphomedefnicklabelcommand
1条回答
网友
1楼 · 发布于 2024-09-30 12:19:43

文件或文件权限的问题

  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文件是否存在?如果存在,那么它是否具有读取权限?在

相关问题 更多 >

    热门问题