Python:如何关闭类中的tkinter窗口?

时间:2015-11-07 00:15:58

标签: python tkinter

我正在努力做一个聊天室。现在我正在登录屏幕上工作。

输入用户名和密码后,如果正确,则应关闭登录界面并打开另一个屏幕。(应该在另一个类中运行)。如果密码或用户名错误,则应直接关闭窗口。但我不知道该怎么做。

我尝试在类中编写login函数,然后在False函数中,如果它返回Quit,则应调用Login.Quit()函数。问题是我不知道我是否可以从外面调用类中的函数。

如果我在课堂外拨打TypeError: unbound method Quit() must be called with Login instance as first argument (got nothing instead) ,则会说

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

我也想知道如何跑到下一个班级并打开另一个窗口。

这实际上是我的功课,但我真的需要帮助。请帮助我,谢谢!

<script>
    function setEnv() {
        var env = document.getElementById("idname");
        session.setAttribute("attName",env.value);
    }
</script>

1 个答案:

答案 0 :(得分:0)

问题是你必须打电话给#34; Quit&#34;来自一个对象,而不是一个类。

问:你有没有创建一个实例类&#34;登录&#34;?

例如:

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

然后,您可以致电myFrame.Quit()

在此查看更多详情:

Python unbound method

What is the difference between objects and classes in Python