2
我试图弄清楚如何使用PodSixNet来使用tkinter多人游戏。基于Tkinter的Python游戏的PodSixNet多人联网库
然后我从两个终端会话中启动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>')
我有几个文件/类,所以我做了一个最小的例子。如果您对我如何组织脚本有意见,请告诉我。
main.py:
#launch the GUI and mainloop
from PodSixNet.Connection import ConnectionListener, connection
import Gui
import config as cfg
if __name__ == "__main__":
gui_instance = Gui.Gui()
gui_instance.main()
cfg.canvas.mainloop()
pass
while 1:
connection.Pump()
gui_instance.Pump()
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()
Deck.py:
#Here is where the actions of the game are run.
#Here I want to use Send to send information to the server
from PodSixNet.Connection import ConnectionListener, connection
class Deck(ConnectionListener):
def __init__(self):
pass
def confirm_move(self):
print("connection=",str(connection))
connection.Send({"action": "myaction", "myvar": 33})
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)