Hi, so I'm having quite the difficulty here. Let me start by explaining the architecture of my scripts. My "Listener.py" is a script that uses asyncore in order to listen for connections and receive commands from them. If it receives the text "play" from a connection, it calls another script, "MusicPlayer.py," that uses the tkSnack library to play a sound file (which requires root.mainloop() to be called). So, I have that working well. Listener.py receives a connection, receives the text "play," and calls "MusicPlayer.py" to play a sound file.

Now, here is where the trouble begins. What good is playing music if you can't pause/stop/restart/etc. it? If I don't change anything about my scripts, the mainloop of Tkinter prevents Listener.py from receiving any more connections/data.

Immediately, I thought to myself, "Oh, no problem! I can just give MusicPlayer.py it's own thread and then I can continue receiving connections/data!" Sadly, this didn't work. It gives me the following error from Tkinter: RuntimeError: main thread is not in main loop . I guess Tkinter doesn't want its main loop inside a separate thread.

So...what can I do about this? If I were to put Listener.py into another thread, I would have the same problem...asyncore requires that you call its mainloop method, and I would be willing to bet it would give me the same error as Tkinter did.

Thanks so, so much in advance.

Okay, I've done a little more work and hopefully somebody will be able to help me from here. I have found a way to run asyncore's loop after Tkinter's main loop (the after method), but when asyncore does its loop, Tkinter stops doing its main loop, mucking everything up. Is there a way I can run asyncore's loop to change this?

Thanks again.

I found a solution! I am still using Tkinter's after method to run asyncore's loop function, but instead of using asyncore.loop(), I use asyncore.poll(0). If I call that function every 100ms or so, it no longer has an effect on Tkinter's main loop.

Victory.

commented: congrats +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.