3

I'm still working on my little Tkinter project which is simple a logging console that prints incoming text from the serial line to a Text Widget with some coloring applied.

One question is open and can be found here: Python Tkinter Text Widget with Auto & Custom Scroll

However, even without manual scrolling (so I'm using self.text.yview(END) to auto-scroll to the bottom after inserting text with self.text.insert(END, str(parsed_line)).

The script actually works but every now and then it throws some "silent" exceptions within the Tkinter thread that does not let the whole application crash:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 2813, in set
    self.tk.call((self._w, 'set') + args)
TclError: expected floating-point number but got "0.7807017543859649integer but got "end""
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 2813, in set
    self.tk.call((self._w, 'set') + args)
TclError: invalid command name ".15427224integer but got "end""

It looks as if some method expected a an integer, returns the string integer but got "end" to a method that expected a float which is concatinated with the error message. The float number in that string looks like the position of the scrollbar that I have attached to my text widget:

(...)       
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
text = Text(wrap=WORD, yscrollcommand=scrollbar.set)
scrollbar.config(command=text.yview)
text.pack(expand=YES, fill=BOTH)
(...)

I have the feeling that it happens when a lot of lines are inserted within a short time. But since I only have one thread interacting with Tkinter this cannot be a threading issue.

I also got very random errors like that before I had applied the str() function to parsed_line in self.text.insert(END, str(parsed_line)).

This is very strange behavior and I'm wondering if anyone could explain what this is about and how to fix it.

Thanks a lot!

5
  • It is difficult to answer your questions as you do not provide some code in order for us to reproduce your problem. Neither this question nor the other linked in your post provide enough information. Could you post a simplified version of your code that still produces the exceptions you indicated when run ?
    – joaquin
    Commented Jan 17, 2012 at 17:18
  • Thanks for your reply. I actually assembled a script that reproduces the issue explained in stackoverflow.com/questions/8850329/… and it seems to be related to this issue. Probably, you could have a look into this script.
    – jaw
    Commented Jan 19, 2012 at 16:17
  • @ebeb: in that other question you mention using two threads. If you are, that is almost certainly the problem here. Can you clarify if you are seeing this problem in code where you are accessing tkinter objects from more than one thread? Commented Jan 21, 2012 at 0:22
  • Tkinter and multithreading is a no-no. AFAIR, the only thing you can do from another thread (not the “main” one) is send custom events.
    – tzot
    Commented Jan 30, 2012 at 21:12
  • @tzot Yeah, I read that about sending custom events, too and I tried it (As shown in the linked thread about custom scroll). However, it does not work either. At first, I thought it worked but then from time to time the python.exe crashed somewhere being in Tk dlls. So the answer is: The only thing that REALLY works is using Tk.after().
    – jaw
    Commented Jan 31, 2012 at 10:05

1 Answer 1

2

mtTkinter allows multithreading with Tkinter, you can get it here:

http://tkinter.unpythonic.net/wiki/mtTkinter

Just import mtTkinter in place of Tkinter. This will allow you to insert text into a Text widget from multiple threads without conflict. I used it for some instant messaging software I wrote and it works wonderfully.

1
  • Thanks. Some time ago, I figured that this issue was related to my other question described in this thread. As shown, I solved the problem using the queue and polling solution. If I had to do it again, I'd definitely give mtTkinter a try. Thanks for pointing it out.
    – jaw
    Commented Mar 19, 2012 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.