AttributeError:'module'对象没有属性'urlopen'(Python 2.7)

时间:2015-03-25 01:30:21

标签: python python-2.7 urllib

我正在为我的学校编写一个程序,并且我将在文本文件中为每位教师主持每日SBWAT(学生将能够参加)。这是我的代码,SchoolNet.py

import Tkinter
import urllib
print urllib.__file__

def showsbwat(teacher):
    sbwat = urllib.openurl("192.168.1.203/" + teacher + ".txt")
    print sbwat

def showshecdule():
    mainwindow.withdraw()
    schedulewindow.deiconify()
    firstperiodbutton = Tkinter.Button(schedulewindow, text = periodlist[0], command = lambda: showsbwat(periodlist[0]))
    firstperiodbutton.pack()
    global sbwatlabel
    sbwatlabel = Tkinter.Label(schedulewindow, text = "")
    sbwatlabel.pack()

def login():
    try:
        schedulefile = open(usernamevar.get() + ".txt", "r")
        global periodlist
        periodlist = schedulefile.readlines()
        print periodlist
        mainwindow.deiconify()
        loginwindow.withdraw()
    except:
        usernamevar.set("Invalid ID")

loginwindow = Tkinter.Tk()
loginwindow.wm_title('Login to SchoolNet')

mainwindow = Tkinter.Tk()
mainwindow.wm_title('SchoolNet')

schedulewindow = Tkinter.Tk()
schedulewindow.wm_title('SchoolNet Schedule')

mainwindow.withdraw()
schedulewindow.withdraw()
loginwindow.deiconify()

schedulebut = Tkinter.Button(mainwindow, text = 'Schedule', command=showshecdule)
schedulebut.pack()

usernamevar = Tkinter.StringVar()
usernameentry = Tkinter.Entry(loginwindow, textvariable=usernamevar)
usernameentry.pack()

loginbut = Tkinter.Button(loginwindow, text="Login", command=login)
loginbut.pack()

Tkinter.mainloop()

但是,当我运行它时,我不断收到此错误:

Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
    return self.func(*args)
  File "SchoolNet.py", line 12, in <lambda>
    firstperiodbutton = Tkinter.Button(schedulewindow, text = periodlist[0], com
mand = lambda: showsbwat(periodlist[0]))
  File "SchoolNet.py", line 6, in showsbwat
    sbwat = urllib.openurl("192.168.1.203/" + teacher + ".txt")
AttributeError: 'module' object has no attribute 'openurl'

我用urllib和urllib2尝试了这个,但是我得到了同样的错误。目录中的其他任何文件都没有与任何python模块相同。我究竟做错了什么? 我使用的是Python 2.7

2 个答案:

答案 0 :(得分:1)

urlopen而非openurl

 urllib.urlopen()

答案 1 :(得分:1)

来自urllib.request import urlopen

网址=&#34; www.webpage-address&#34;

page = urlopen(URL)

text = page.read()