2
我對Python很新,我一直在玩Tkinter和requests來嘗試創建一個簡單的程序,可以登錄到我的Web應用程序。我已經收到以下錯誤:AttributeError:條目實例沒有屬性'__len__'
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "test.py", line 35, in login
c.post('http://fruro.com/login.php', data=self.payload)
File "/Users/Peter/Desktop/fruroPy/fruro/lib/python2.7/site-packages/requests/sessions.py", line 399, in post
return self.request('POST', url, data=data, **kwargs)
File "/Users/Peter/Desktop/fruroPy/fruro/lib/python2.7/site-packages/requests/sessions.py", line 343, in request
prep = req.prepare()
File "/Users/Peter/Desktop/fruroPy/fruro/lib/python2.7/site-packages/requests/models.py", line 222, in prepare
p.prepare_body(self.data, self.files)
File "/Users/Peter/Desktop/fruroPy/fruro/lib/python2.7/site-packages/requests/models.py", line 375, in prepare_body
body = self._encode_params(data)
File "/Users/Peter/Desktop/fruroPy/fruro/lib/python2.7/site-packages/requests/models.py", line 81, in _encode_params
return urlencode(result, doseq=True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1311, in urlencode
len(v)
AttributeError: Entry instance has no attribute '__len__'
我一直在試圖找出什麼可能出錯了好幾個小時,但我不知道我做錯了。
這裏是我的代碼,我使用:
from Tkinter import *
from requests import session
class Login:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.emLabel = Label(frame, text="Email:")
self.emLabel.pack(side=LEFT)
self.email = Entry(frame, width=15)
self.email.pack(side=LEFT)
self.pasLabel = Label(frame, text="Password:")
self.pasLabel.pack(side=LEFT)
self.password = Entry(frame, show="*", width=15)
self.password.pack(side=LEFT)
self.hi_there = Button(frame, text="Login", command=self.login)
self.hi_there.pack()
def login(self):
self.payload = {
'action': 'login',
'email': self.email,
'password': self.password
}
with session() as c:
c.post('http://fruro.com/login.php', data=self.payload)
self.request = c.get('http://fruro.com')
print self.request.headers
print self.request.text
root = Tk()
login = Login(root)
root.title("Login to your account")
root.mainloop()
任何幫助,將不勝感激。先謝謝你。
+1。我會刪除我的答案,因爲這個更好。 – Elazar 2013-05-05 21:47:17
非常感謝你解決了這個問題。 – 2013-05-05 21:49:55