簡體   English   中英

urllib和tkinter的Python錯誤

[英]Python error with urllib and 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 GUI,我認為它與此無關。 我也很難理解這個相當愚蠢的錯誤。 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

文件或文件權限問題

  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文件是否存在? 如果存在,那么它是否有權閱讀?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM