[英]Using the PodSixNet library with Python, I am having trouble using Tkinter
So I am using PodSixNet to have server-client interaction. 所以我正在使用PodSixNet进行服务器-客户端交互。 It works great, but when I try to add Tkinter, I get a maximum recursion depth in assyncore.
它很好用,但是当我尝试添加Tkinter时,我在assyncore中得到了最大的递归深度。
Here is my client, the server works flawlessly: 这是我的客户端,服务器正常运行:
import sys
from time import sleep
from sys import stdin, exit
from Tkinter import *
import Tkinter
from PodSixNet.Connection import connection, ConnectionListener
# This example uses Python threads to manage async input from sys.stdin.
# This is so that I can receive input from the console whilst running the server.
# Don't ever do this - it's slow and ugly. (I'm doing it for simplicity's sake)
from thread import *
class App:
def __init__(self, master, connection):
self.connection = connection
self.frame = Frame(master)
self.frame.pack()
self.connection.Send({"action":"nickname","nickname":"test"})
class Client(ConnectionListener):
global players
players = []
def __init__(self, host, port):
root = Tk()
root.title("CHAT")
app = App(root, connection)
start_new_thread(root.mainloop, ())
print "Enter your nickname:",
nick = raw_input()
self.Connect((host, port))
connection.Send({"action": "nickname", "nickname": nick})
# launch our threaded input loop
t = start_new_thread(self.InputLoop, ())
def Loop(self):
connection.Pump()
self.Pump()
def InputLoop(self):
global players
# horrid threaded input loop
# continually reads from stdin and sends whatever is typed to the server
while 1:
msg = raw_input()
if msg.startswith("/"):
cmd = msg[1:]
if cmd.lower().startswith("pm"):
to = cmd[3:].split(" ", 1)[0]
msg = cmd[3:].split(" ", 1)[1]
if to in players:
connection.Send({"action": "PM", "to": to, "message":msg})
else:
print "That person is not online!"
continue
elif cmd.lower().startswith("name"):
newName = cmd[5:]
connection.Send({"action":"nickname","nickname":newName})
else:
connection.Send({"action": "message", "message": msg})
#######################################
### Network event/message callbacks ###
#######################################
def Network_players(self, data):
global players
#Recieved list of players.
players = data['players']
def Network_message(self, data):
#Got a message.
print data['from'] + ": " + data['message']\
def Network_serverMessage(self, data):
msg = data['message']
print "Server:",msg
def Network_PMrecieve(self, data):
msg = data['message']
of = data['from']
print "PM from " + of + ": " + msg
def Network_PMsuccess(self, data):
success = data['success']
if success == True:
print "PM successfully sent!"
else:
print "Error, recipient may not be online."
# built in stuff
def Network_connected(self, data):
print "You are now connected to the server"
def Network_error(self, data):
print 'error:', data['error'][1]
connection.Close()
def Network_disconnected(self, data):
print 'Server disconnected'
exit()
c = Client("localhost", 12345)
while 1:
c.Loop()
sleep(0.001)
It only gives errors when I try to use connection.Send from anywhere else than the Client class 仅当我尝试使用连接时才会出现错误。从Client类以外的其他任何地方发送
I heard it might be that Tkinter is not thread safe? 我听说Tkinter可能不是线程安全的吗? If so, how do I fix it?
如果是这样,我该如何解决?
You cannot access Tkinter widgets from more than one thread. 您不能从多个线程访问Tkinter小部件。 You are creating the root window in one thread but running the event loop in another.
您正在一个线程中创建根窗口,但在另一个线程中运行事件循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.