2015-03-25 74 views
-1

我正在爲我的學校編寫一個程序,並且我爲每個老師在文本文件中主持每日SBWATs(學生將能夠)。這裏是我的代碼,SchoolNet.pyAttributeError:'模塊'對象沒有屬性'urlopen'(Python 2.7)

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

+1

更改爲'urllib.urlopen()' – Aaron 2015-03-25 01:31:41

+0

哦哇...謝謝 – pythonrocks02 2015-03-25 01:35:02

回答

1

這是urlopenopenurl

urllib.urlopen() 
1

從urllib.request裏進口的urlopen

URL = 「www.webpage地址」

頁=的urlopen (URL)

text = page.read()

相關問題