0

I'm trying to implement a simple ui to show network messages. client.connect() returns a string. However, I get out of space exception and of course no UI updates

allText = ''

class Receive():
  def __init__(self, client):
    while 1:
      try:
        text = client.connect()
        if not text: 
            pass
        else:
            global allText
            allText = allText + text + '\n'
      except Exception as e: print(e)

class App(thr.Thread):
    client = cvClient()
    client.login()
    client.register()
    client.setWorking()
    def __init__(self, master):
        thr.Thread.__init__(self)
        frame = TK.Frame(master)
        frame.pack()
        self.gettext = tkst.ScrolledText(frame, height=10,width=100)
        self.gettext.pack()
        self.gettext.insert(TK.END,'Welcome to Chat\n')
        self.gettext.configure(state='disabled')
        sframe = TK.Frame(frame)
        sframe.pack(anchor='w')
        self.pro = TK.Label(sframe, text="Client>>");
        self.sendtext = TK.Entry(sframe,width=80)
        self.sendtext.focus_set()
        self.sendtext.bind(sequence="<Return>", func=self.Send)
        self.pro.pack(side=TK.LEFT)
        self.sendtext.pack(side=TK.LEFT)
    def Send(self, args):
        self.gettext.configure(state='normal')
        text = self.sendtext.get()
        if text=="": text=" "
        self.gettext.insert(TK.END,'Me >> %s\n'%text)
        self.sendtext.delete(0,END)
        self.client.send(text)
        self.sendtext.focus_set()
        self.gettext.configure(state='disabled')
        self.gettext.see(TK.END)

    def callback(self):
        print ("in callback")
        self.gettext.configure(state='normal')
        global allText
        self.gettext.insert(TK.END,'Server >> %s\n'%allText)
        self.gettext.configure(state='disabled')
        self.gettext.see(TK.END)
        root.after(1000, self.callback)
    def run(self):
        self.callback()
        Receive(self.client)


root = TK.Tk()
root.title('Client Chat')
app = App(root).start()
root.mainloop()

This is the error I get

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/home/innereye/anaconda2/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "cvClient.py", line 332, in run
    self.callback()
  File "cvClient.py", line 325, in callback
    self.gettext.configure(state='normal')
  File "/home/innereye/anaconda2/lib/python2.7/lib-tk/Tkinter.py", line 1331, in configure
    return self._configure('configure', cnf, kw)
  File "/home/innereye/anaconda2/lib/python2.7/lib-tk/Tkinter.py", line 1322, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: out of stack space (infinite loop?)

Any suggestions? I'm not blocking the UI thread or calling it in an infinite loop, so I don't really understand why this is happening.

JLev
  • 705
  • 1
  • 9
  • 30
  • Please post the actual error. – Bryan Oakley Dec 29 '19 at 17:41
  • ***"calling it in an infinite loop"***: Add a `print('allText:', allText)` just after `allText = allText ...` to see how fast it grows. Consider to use this approach: [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Dec 29 '19 at 19:14
  • Added the error – JLev Dec 30 '19 at 06:24
  • allText grows when connect return which is about every 1 second. Not so fast I gues... – JLev Dec 30 '19 at 06:24
  • @JLev ***"which is about every 1 second"***: There is no **delay** within `while 1:`, but i don't know anything about `client.connect()`. Does it blocking? – stovfl Dec 30 '19 at 08:48
  • It is blocking. This is the reason I thought it should work fine... – JLev Dec 30 '19 at 09:08
  • @JLev Try [how-do-i-profile-memory-usage-in-python?](https://stackoverflow.com/questions/552744/how-do-i-profile-memory-usage-in-python?) – stovfl Dec 30 '19 at 19:50

0 Answers0