Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过套接字发送的空字典_Python_Python 3.x_Sockets_Dictionary - Fatal编程技术网

Python 通过套接字发送的空字典

Python 通过套接字发送的空字典,python,python-3.x,sockets,dictionary,Python,Python 3.x,Sockets,Dictionary,我目前有通过套接字发送数据的代码,它正在工作,但我正在发送的字典中没有任何内容。我是否在这段代码中做错了什么 class ChessBoard(tk.Frame): def __init__(self, parent, rows=8, columns=8, size=70, color1="White", color2="lightgrey"): self.rows = rows self.columns = columns self.s

我目前有通过套接字发送数据的代码,它正在工作,但我正在发送的字典中没有任何内容。我是否在这段代码中做错了什么

class ChessBoard(tk.Frame):
    def __init__(self, parent, rows=8, columns=8, size=70, color1="White", color2="lightgrey"):

        self.rows = rows
        self.columns = columns
        self.size = size
        self.color1 = color1
        self.color2 = color2
        self.pieces = {}
        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0,
                            width=canvas_width, height=canvas_height, background="bisque")
        self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2)
        color = self.color2

        for row in range(self.rows):
               color = self.color1 if color == self.color2 else self.color2
               for col in range(self.columns):
                  x1 = (col * self.size)
                  y1 = (row * self.size)
                  x2 = x1 + self.size
                  y2 = y1 + self.size
                  self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=color, tags="square")
                  color = self.color1 if color == self.color2 else self.color2


def addpiece(self):

        self.canvas.WhiteKing = tk.PhotoImage(file = 'E:\\Final Project + Report\\Pieces\\WhiteKing.png')
        self.canvas.create_image(0,0, image=self.canvas.WhiteKing, tags="WhiteKing", anchor="c")
        self.placepiece("WhiteKing", row = 7, column = 4)

        self.canvas.WhiteQueen = tk.PhotoImage(file = 'E:\\Final Project + Report\\Pieces\\WhiteQueen.png')
        self.canvas.create_image(0,0, image=self.canvas.WhiteQueen, tags="WhiteQueen", anchor="c")
        self.placepiece("WhiteQueen", row = 7, column = 3) ######


def placepiece(self, name, row, column):
        self.pieces[name] = (row, column)
        x0 = (column * self.size) + int(self.size/2)
        y0 = (row * self.size) + int(self.size/2)
        self.canvas.coords(name, x0, y0)

if __name__ == "__main__":
    root = tk.Tk()                                                                     
    board = ChessBoard(root)
    board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
    board.addpiece()
    root.mainloop()
这是我的密码:

        serverName = '127.0.0.1'
        serverPort = 8000
        root = tk.Tk()
        a = Final_Chess_Game.ChessBoard(root)
        serializedDict = json.dumps(a.pieces)
        self.sock.sendto(serializedDict.encode(),(serverName,serverPort))
        print(serializedDict)
因此,当程序运行时,我从另一个包含所有片段的文件导入一个字典

以下是我收到的图片:


嗯,您可能不喜欢这个答案,但它收到的是一个空字典,因为您发送的是一个空字典!代码按预期运行,问题是a.pieces为空或是序列化为空字典的数据结构。如果您发布更多代码,也许我们可以帮助进一步诊断。就目前情况而言,这有点难说

只是为了好玩,试试这个

serverName = '127.0.0.1'
serverPort = 8000
root = tk.Tk()
a = Final_Chess_Game.ChessBoard(root)
a.addpiece()
serializedDict = json.dumps(a.pieces)
self.sock.sendto(serializedDict.encode(),(serverName,serverPort))
print(serializedDict)

您的代码是类的一部分吗?Chessboard方法是什么?是的,Chessboard是一个类,该类包含我需要的字典。实际上,我刚刚在测试字典上测试了代码,它可以工作!是的,问题出在我的字典上。只有当我运行程序时,我的字典才会充满数据,我认为这就是我遇到的问题,但我会用剩下的代码更新答案!好的,那你们是如何创造最终的国际象棋游戏的呢?看起来您需要运行.addpiece方法来填充字典。除非最后的棋局是这样做的,。棋子将永远不会被填充!最后的棋局只是文件名。我发现只有当我实际运行我的程序时,我的字典才会充满数据。所以,没有其他方法来解决这个问题吗?我以为你说它正在工作,对不起!它还在工作吗?