0

I am trying to make a chatroom. Now I am working on my login screen.

After enter the username and password, if it is right, then the login screen should be closed and open another screen.(which should be run in anther class). If the password or username is wrong, then the window should be closed directly. But I don't know how to do that.

I try to write a Quit function in the class, then in login function, if it returns False, it should call Quit function. The problem is I don't know if I can call the function in a class from outside.

If I call Login.Quit() outside the class, it will say

TypeError: unbound method Quit() must be called with Login instance as first argument (got nothing instead)

I also want to know how to run to next class and open another window.

This is actually my homework, but I really need help. Please help me, thanks!

from Tkinter import *
import socket

########HelperFunction########
def chunkstring (block):    #Use to make the block into chunks and count the sum of ASCII value of chunks
    M = []
    for i in range(0, 512, 32):
        L = str((block[0 + i : 32 + i]))
        sum = 0
        for r in range(len(L)):
            sum = sum + ord(L[r])
        M.append(sum)
    return M

def leftrotate(x, c):
    return (x << c) & 0xFFFFFFFF | (x >> (32 - c) & 0x7FFFFFFF >> (32 - c))



########Connection########
def StartConnection (IPAddress, PortNumber):    #Use to set up the connection between computers and servers
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((IPAddress, PortNumber))
    return s

def login (s, username, password):    #Login Function
    print username
    print password
    s.send('LOGIN ' + username + '\n')
    data = s.recv(512)
    List = data.split(" ")    #send the commend and get something back

    CH = List[2]    # pick up the CHALLENGE code
    CH = CH[:-2]    # delete the last two unnecessary code
    PD = password
    message = PD + CH    # combine password and CHALLENGE together
    block = message + "1"

    block = block + "0" * (512 - len(message) - 3 - 1)    # add '0' to block and remain the space for last three digits

    numLen = len(str(len(message)))

    if numLen == 2:    #If the password is very long, we should consider the last digits may be affected
        block = block + "0" + str(len(message))
    elif numLen == 3:
        block = block + str(len(message))

    M = chunkstring(block)

    ########## MD5
    P = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
         5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
         4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
         6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]

    K = [0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
         0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
         0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
         0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
         0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
         0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
         0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
         0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
         0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
         0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
         0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
         0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
         0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
         0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
         0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
         0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391]

    #Initialize variables
    a0 = 0x67452301
    b0 = 0xefcdab89
    c0 = 0x98badcfe
    d0 = 0x10325476
    A = a0
    B = b0
    C = c0
    D = d0

    #Mainloop
    for i in range(0, 64):
        if i >= 0 and i <= 15:
            F = (B & C) | ((~ B) & D)
            F = F & 0xFFFFFFFF
            g = i

        elif i >= 16 and i <= 31:
            F = (D & B) | ((~ D) & C)
            F = F & 0xFFFFFFFF
            g = (5 * i + 1) % 16

        elif i >= 32 and i <= 47:
            F = B ^ C ^ D
            F = F & 0xFFFFFFFF
            g = (3 * i + 5) % 16

        elif i >= 48 and i <= 63:
            F = C ^ (B | (~ D))
            F = F & 0xFFFFFFFF
            g = (7 * i) % 16

        dTemp = D
        D = C
        C = B
        B = B + leftrotate((A + F + K[i] + M[g]), P[i])
        B = B & 0xFFFFFFFF
        A = dTemp

    #Add this chunk's hash to result so far:
    a0 = (a0 + A) & 0xFFFFFFFF
    b0 = (b0 + B) & 0xFFFFFFFF
    c0 = (c0 + C) & 0xFFFFFFFF
    d0 = (d0 + D) & 0xFFFFFFFF
    result = str(a0) + str(b0) + str(c0) + str(d0)

    s.send("LOGIN " + username + " " + result + "\n")    #send messagedigest to server
    reply = s.recv(512)
    print reply

    if "Successful" in reply:
        return True
    else:
        Login.Quit()
        return False


########Interface#########
class Login(Frame):
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        First.geometry("250x250")
        self.lab1 = Label(frame, text = "Username")
        self.lab1.grid(row = 0, column = 125)
        self.ent1 = Entry(frame)
        self.ent1.grid(row = 1, column = 125)

        self.lab2 = Label(frame, text = "Password")
        self.lab2.grid(row = 2, column = 125)
        self.ent2 = Entry(frame, show = "*")
        self.ent2.grid(row = 3, column = 125)

        self.button = Button(frame, text = "OK", command = self.Submit)
        self.button.grid(row = 5, column = 125)

    def Submit(self):
        username = self.ent1.get()
        password = self.ent2.get()
        login(ss, username, password)

    def Quit(self):
        self.Login.quit()

First = Tk()
First.title("Login")
Login(First)
ss = StartConnection("86.36.34.215", 15112)
First.mainloop()
mino
  • 43
  • 7

1 Answers1

0

The problem is that you must call "Quit" from an object, not a class.

Q: Do you ever create an instance of class "Login"?

For example:

First = Tk()
First.title("Login")
myFrame = new Login(First)
...
First.mainloop()

Then, you can call myFrame.Quit()

Look here for more details:

Python unbound method

What is the difference between objects and classes in Python

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks! I have created a instance, but I meet another problem. In my code, I write a Quit function in class and then I call it. But it returns 'AttributeError: Login instance has no attribute 'Login' – mino Nov 07 '15 at 00:34
  • That's because your Login.Quit() method calls "self.Login.quit()". Where is there "Login" member in your "Login" class??? SUGGESTION: 1) Change your method to `def Quit():`. Now that you're creating a class instance, you no longer need the "self" parameter. 2) Change "self.Login.quit()" to `root.quit()`. PS: Feel free to "upvote" and/or "accept" this response if it was helpful to you. – paulsm4 Nov 07 '15 at 04:24