Python Tkinter Loop
<p>I'm trying to make an infinite loop (with a break) that runs alongside my Tkinter window and interacts with widgets on it. Yes, I <strong>have</strong> done my research, but when I run my code and activate the function the window stops responding.</p>

<p>I've deleted the 'while true' from the method, and at the end of it placed:</p>

<pre><code>if self.connected:
    root.after(100, self.listenServer(id, req))
</code></pre>

<p>I think the problem is that the function takes a little while to complete (I'm making an Omegle client, so it has to connect to the server). I can tell that it runs six times, because I put a print statement in the method.</p>

<p>Is there a simple <strike>(AKA no threading)</strike> method to solve this problem?</p>

<p>Well, if threading is the only way to do it, then I guess it would be fine</p>

<p>Here are my connect and listen methods:</p>

<pre><code>    def listenServer(self, id, req):
    site = url.urlopen(req)

    #We read the HTTP output to get what's going on
    rec = site.read()

    if 'waiting' in rec:
        self.chatbox.config(state=NORMAL)
        self.chatbox.insert(END, "Waiting...\n", "italic")
        self.chatbox.config(state=DISABLED)

    elif 'connected' in rec:
        self.chatbox.config(state=NORMAL)
        self.chatbox.insert(END, "Stranger connected\n", "italic")
        self.chatbox.config(state=DISABLED)

    elif 'strangerDisconnected' in rec:
        self.chatbox.config(state=NORMAL)
        self.chatbox.insert(END, "Stranger Disconnected\n", "italic")
        self.chatbox.config(state=DISABLED)
        self.connected = False

    elif 'typing' in rec:
        self.chatbox.config(state=NORMAL)
        self.chatbox.insert(END, "Stranger is typing\n", "italic")
        self.chatbox.config(state=DISABLED)

    elif 'gotMessage' in rec:
        self.chatbox.config(state=NORMAL)
        self.chatbox.insert(END, "Stranger: " + rec[17:len(rec) - 3] + "\n")
        self.chatbox.config(state=DISABLED)

def OmegleConnect(self):
    self.chatbox.config(state=NORMAL)
    self.chatbox.insert(END, "Connecting...\n", "italic")
    self.chatbox.config(state=DISABLED)

    site = url.urlopen('http://omegle.com/start', '')
    id = site.read()
    id = id[1:len(id) - 1]
    self.chatbox.config(state=NORMAL)
    self.chatbox.insert(END, "Stranger ID: " + id + "\n", "title")
    self.chatbox.config(state=DISABLED)
    req = url.Request('http://omegle.com/events', urllib.urlencode({'id':id}))
    self.chatbox.config(state=NORMAL)
    self.chatbox.insert(END, "Finding a stranger...\n", "italic")
    self.chatbox.config(state=DISABLED)
    self.connected = True
    root.after(100, self.listenServer(id, req))
</code></pre>

<p>Yes, I know this is quite inefficient with the way it writes to the text widget. I tried making a method to do it more easily, but it wasn't working. I'll worry about that once I get this up and running.</p>
