简体   繁体   English

在Python中使用PodSixNet库,使用Tkinter遇到麻烦

[英]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.

相关问题 无法通过Python API使用C库:我在做什么错? - Having trouble using a C library via a Python API: What am I doing wrong? 我在使用“范围内”的Python列表显示时遇到问题 - I am having trouble with a Python list display using “in range” 我在使用 python 计算简单算术时遇到问题 - I am having trouble in computing a simple arithmetic using python 我正在尝试使用python 3中的ascii art创建字母Y,并且在完成练习时遇到麻烦 - I am trying to create a letter Y using ascii art in python 3 and am having trouble finishing the excersise 无法在python中导入sikuli模块。 我正在使用RIDE运行python脚本 - Having trouble in importing sikuli modules in python . I am using RIDE to run python script 我在导入 PyDictionary 库时遇到问题 - I am having a trouble importing the PyDictionary library 我在 python 3.9 中使用 tkinter - I am using tkinter in python 3.9 我无法使用 python 单击带有 selenium 的 reddit 注册页面上的注册按钮 - I am having trouble clicking the signup button on a reddit sign up page with selenium using python 我在使用python从Excel工作表中读取日期和时间时遇到问题 - I am having trouble in reading date and time from an excel sheet using python 我在 Python Tkinter 中使用了一种新字体 - I am using a new Font in Python Tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM