2

I am quite new to python, I have been playing around with Tkinter and requests to try to create a simple program that can log into my web application. I have been getting the following error:

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__'

I have been trying to figure out what could have gone wrong for several hours but I just don't know what I'm doing wrong.

Here is my code that I'm using:

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

Any help would be appreciated. Thank you in advance.

icktoofay
  • 126,289
  • 21
  • 250
  • 231

1 Answers1

3

The problem is on this line,

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

And it is happening because the dict you are passing in as data parameter has values that are references to Entry widgets and not the content of those widgets. So when Requests tries to get the len, you get an error since Entry doesn't implement __len__ (not what you wanted anyway). To fix it change self.payload to something like,

self.payload = {
        'action': 'login',
        'email': self.email.get(),      # call get method to return
        'password': self.password.get() # the value in the Entry widgets
    }
Jared
  • 25,627
  • 7
  • 56
  • 61