Python 2.7 基于Tkinter的Python游戏PodSixNet多人网络库

Python 2.7 基于Tkinter的Python游戏PodSixNet多人网络库,python-2.7,tkinter,network-programming,multiplayer,Python 2.7,Tkinter,Network Programming,Multiplayer,我一直在试图弄清楚如何使用PodSixNet的tkinter多人游戏制作我的游戏 然后,我从两个终端会话启动server.py和main.py,并单击按钮,这将触发Deck.confirm_move()。Deck.confirm_move()应该向服务器发送信息: connection.Send({"action": "myaction", "myvar": 33}) 问题在于,只有当我关闭窗口时,响应才会在服务器上可见: > python ./server.py Starting

我一直在试图弄清楚如何使用PodSixNet的tkinter多人游戏制作我的游戏

然后,我从两个终端会话启动server.py和main.py,并单击按钮,这将触发Deck.confirm_move()。Deck.confirm_move()应该向服务器发送信息:

connection.Send({"action": "myaction", "myvar": 33}) 
问题在于,只有当我关闭窗口时,响应才会在服务器上可见:

> python ./server.py 
Starting server on localhost
('boxesServe=', '<__main__.BoxesServer listening 127.0.0.1:31425 at 0x7f7de91193b0>')
new connection: channel =  <__main__.ClientChannel connected 127.0.0.1:56286 at 0x7f7de9119638>

#THIS APPEARS WHEN I CLOSE THE WINDOW
**{'action': 'myaction', 'myvar': 33}
('myaction:', {'action': 'myaction', 'myvar': 33})**


> python ./main.py 
('connection=', '<PodSixNet.EndPoint.EndPoint 127.0.0.1:31425 at 0x7f447af96cf8>')
config.py:

#Here I initialize objects/parameters that are shared between scripts. 
#Is this good practice?
win = None
canvas = None
deck = False
Gui.py:

#Define the GUI with a canvas, buttons, etc. Call self.Connect()
import Tkinter as tk

from PodSixNet.Connection import ConnectionListener, connection
import config as cfg
from Deck import Deck
class Gui(ConnectionListener):
  def __init__(self):
    cfg.win=tk.Tk()

    """Create cfg.canvas"""
    cfg.canvas=tk.Canvas(cfg.win,height=300,width=300,name="canvas")
    """Buttons"""  self.btnConf=tk.Button(cfg.win,text="Confirm",width=6,name="btnConf",bg="white",anchor=tk.W)
    self.btnConf.bind('<ButtonRelease-1>', self.buttonCallback)
    self.btnConf_window = cfg.canvas.create_window(50,50,anchor=tk.NW, window=self.btnConf)
    cfg.canvas.grid(row = 1, column = 0, rowspan = 5)

    self.Connect()  # <--- PodSixNet's Connect 

  def buttonCallback(self, event):
    cfg.deck.confirm_move()

  def main(self):
    """Deal deck"""
    cfg.deck = Deck()
server.py:

import PodSixNet.Channel
import PodSixNet.Server
from time import sleep    
class ClientChannel(PodSixNet.Channel.Channel):
  def Network(self, data):
    print data

  def Network_myaction(self, data):
    print("myaction:", data)

class BoxesServer(PodSixNet.Server.Server):
  channelClass = ClientChannel

  def __init__(self, *args, **kwargs):
    PodSixNet.Server.Server.__init__(self, *args, **kwargs)

  def Connected(self, channel, addr):
    print 'new connection: channel = ', channel

print "Starting server on localhost"
boxesServe = BoxesServer()
print("boxesServe=",str(boxesServe))
while True:
    boxesServe.Pump()
    sleep(0.01)

您可以将cfg.canvas.mainloop()中的循环替换为对窗口对象调用update()和update_idletasks()的循环。在您的情况下,while循环变成:

while 1:
  connection.Pump()
  self.Pump()
  cfg.win.update()
  cfg.win.update_idletasks()
因为在main.py中,您现在有:

if __name__ == "__main__":
  gui_instance = Gui.Gui()
  gui_instance.main()
  #cfg.canvas.mainloop()
初始化GUI后,将while循环放在GUI.main()的末尾:

你应该准备好了

while 1:
  connection.Pump()
  self.Pump()
  cfg.win.update()
  cfg.win.update_idletasks()
if __name__ == "__main__":
  gui_instance = Gui.Gui()
  gui_instance.main()
  #cfg.canvas.mainloop()
def main(self):
  """Deal deck"""
  cfg.deck = Deck() 
  while 1:
    connection.Pump()
    self.Pump()
    cfg.win.update()
    cfg.win.update_idletasks()