使用套接字的Tkinter应用程序在启动时挂起

时间:2016-07-31 11:07:42

标签: python sockets tkinter

我遇到socket / tkinter登录应用程序的问题。服务器在Raspberry Pi上,但即使服务器没有打开,我也没有理由认为这应该在启动时挂起。我已经导入了一个我将要包含的模块。

为什么要挂?

这是我的客户端代码 - 挂起的代码:

import socket, pickle
import Tkinter as tk
import loginutility

class Server(object):
    def __init__(self):
        self.s = socket.socket()
        self.p = 10000
        self.ip = "192.168.1.120"
    def addUser(self, userinfo):
        puserinfo = pickle.dumps(userinfo)
        self.s.connect((self.ip, self.p))
        self.s.sendall("check")
        self.sendall(puserinfo)
        if self.s.recv(1024) == False:
            self.s.sendall("add")
            self.send(puserinfo)
            return True
        else:
            return False
    def userDump(self):
        self.s.sendall("userdump")
        return pickle.loads(self.s.recv(1024))
class Main(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

class LoginFrame(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.pack()
        self.l = loginutility.LoginBox(self)
        login = tk.Button(self, text="Login", command=self.login)
        login.pack()
    def login(self):
        u, p = self.l.values()
        users = Server.userDump()
        if u in users and users[u] == p:
            tk.Label(self, text="Success").pack()

main = Main()
mf = LoginFrame(main)
main.mainloop()

服务器代码 - 在测试时未启用:

import loginutility as lu
import socket
import pickle
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
address = ''
port = 10000
s.bind((address, port))
s.listen(5)

users = lu.UserList("accounts")
while True:
        c, clientaddress = s.accept()
        c.send("You're Connected")
        d = c.recv(1024)
        if d == "add":
                userdata = c.recv(1024)
                username, password = pickle.loads(userdata)
                users.adduser(username, password)
                print "User {} added with password of {}.".format(username, password)
                c.send("Success")
        elif d == "check":
                username, password = pickle.loads(c.recv(1024))
                if users.checkuser(username) == False:
                        c.sendall(False)
                else:
                        c.sendall(True)
        elif d == "userdump":
                c.send(pickle.dumps(users.dumpuser())

        c.close()

必要的登录密码:

class LoginBox:
    def __init__(self, parent):
        self.l1=Label(parent, text="Username:").grid()
        self.ubox=Entry(parent)
        self.ubox.grid()
        self.l2=Label(parent, text="Password:").grid()
        self.pbox=Entry(parent, show="*")
        self.pbox.grid()
    def values(self):
        return (self.ubox.get(), self.pbox.get())

class UserList:
    def __init__(self, file=None):
        self.users = {}
        self.file = file
        if file != None:
            with open(file, "rb") as f:
                self.users = pickle.load(f)
    def adduser(self, user, pswrd):
        self.users[user] = pswrd
        if file != None:
            with open(self.file, "wb") as f:
                pickle.dump(self.users, f)
    def dumpuser(self):
        return self.users

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

它挂起的原因是因为您在同一个父窗口小部件中混合了packgrid。请考虑以下代码:

class LoginFrame(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.pack()
        self.l = loginutility.LoginBox(self)
        login = tk.Button(self, text="Login", command=self.login)
        login.pack()

在上面的代码中,您创建了一个框架(self),创建了一个LoginBox的子项self,以及一个也是{{1}的子项的按钮}}。该按钮使用selfpack使用LoginBox.__init__

您不能在共享共同父级的小部件上同时使用gridpack。在这种情况下,公共父项是grid的实例。