At this point I'm still a noob when it comes to GUI and network programming so I'm hoping this will be a very simple fix. I've got a very basic understanding of the tkinter and asyncore modules having built a handful of programs in each of them, however I'm having trouble using both of them together in a program. I put together an entire UI only to find out that I could not achieve any significant asynchronous networking functionality. For the sake of simplicity, I deconstructed the program into its simplest form to illustrate the basic problem I'm having. Heres the code:
from Tkinter import *
import asyncore, socket
class Application(object):
def __init__(self, root):
mainFrame = Frame(root)
mainFrame.grid(column=1, row=1, columnspan=3, rowspan=1)
mainButton = Button(mainFrame, text='Click', command=self.makeSocket)
mainButton.grid(column=2, row=1, columnspan=1, rowspan=1, pady=7, padx=40)
def makeSocket(self):
clientSocket()
class clientSocket(asyncore.dispatcher):
def __init__(self):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect(("XXX.XXX.XXX.XXX", XXXX))
print 'init works'
def handle_connect(self):
print 'connect works'
root = Tk()
myApp = Application(root)
root.after_idle(asyncore.loop)
root.mainloop()
So when I run the program and click the button, I get the string 'init works', indicating that the clientSocket object is initialized and the connection is made successfully. However, the handle_connect method doesn't run. And if I implement the handle_read method and execute a command on the server(to send data back to the client) this method isn't called either. I'm thinking that there is some general problem that is preventing the asyncore loop from running on its own. I realize that tkinters event loop could be the culprit, but I was under the impression that the after_idle method would allow non-Tkinter events to be processed while the GUI is idle. Is it the tkinter event loop that is still causing problems or could it be something else?