gpt4 book ai didi

python - 属性错误 : Entry instance has no attribute '__len__'

转载 作者:行者123 更新时间:2023-12-04 04:58:56 27 4
gpt4 key购买 nike

我对 python 很陌生,我一直在玩 Tkinter 和 requests尝试创建一个可以登录到我的 Web 应用程序的简单程序。我一直收到以下错误:

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()

任何帮助将不胜感激。提前谢谢你。

最佳答案

问题出在这条线上,

c.post('http://fruro.com/login.php', data=self.payload)

它的发生是因为 dict您以 data 的身份传入参数的值是对 Entry 的引用小部件而不是这些小部件的内容。所以当 Requests试图获得 len ,你会收到一个错误,因为 Entry不实现 __len__ (反正不是你想要的)。要修复它,请更改 self.payload类似的东西,
self.payload = {
'action': 'login',
'email': self.email.get(), # call get method to return
'password': self.password.get() # the value in the Entry widgets
}

关于python - 属性错误 : Entry instance has no attribute '__len__' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389356/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com