2

I'm quite new to python but I've spent my last week trying to code a software to visualize some weavy thingy.

The basic cinematic is: the user enters every information needed into a GUI then click proceed and I have an other big function to genereate all the graphics.

This was working but the problem was that when I ran the function, which lasts like 2 minutes, the tkinter window was freezing. I read that I should use threads. Then I found this: http://uucode.com/texts/pylongopgui/pyguiapp.html This is an example that basicaly does what I want plus some other things.

I'm now trying to adapt my code to make it fit this.

And here is my problem: everything seems to work fine except that at one moment in my function a new window called "tk" pop up and everything freeze.

Everything freeze at this moment:

# On trace les fils de chaine
for i in range(0, Couches_Trame + 1):
    t = np.arange(0, np.pi, 0.1)
    plt.figure(i)
    plt.title('Plan de Trame ' + str(i+1), fontsize = '16')
    ax = plt.gca()
    ax.yaxis.set_visible(False)
    ax.xaxis.set_visible(False)
    plt.axis([-1, Plans, Fils_Chaine + 1, -1])
    for j in range(0,Plans):
        for k in range(0,Fils_Chaine):
            plt.fill_between(np.cos(t)/8+j, np.sin(t+np.pi)/8+k+0.5, \
            np.sin(t)/8+k+0.5, color='r')
    plt.savefig('Blabla'+int(i))
plt.figure(Couches_Trame)
plt.title('Plan de Trame: Projection')

When I run it directly without using Tkinter everything works fine so I have no idea what's causing this.

Also I've tried replacing this piece of code by and infinite loop like this:

i=1
while i > 0:
    i=i+1
    print(i)

This works and nothing is freezing. But then I tried this:

i=1
while i > 0:
    i=i+1
    plt.plot((i,i))

And everything freezes and the window called "tk" pop-up and instantly freezes.

I read somewhere that this could be a conflict beetwen Tkinter and matplotlib backend but that's all and this didn't help me much.

Edit: I don't know if this help but I'm using Python Portble 2.7.2.1

Leo
  • 1,129
  • 4
  • 23
  • 38

2 Answers2

3

I can't write comments, but a few things to check would be:

  • on the python cmdline, try plotting a very simply graph. E.g.:

    >>> import pyplot
    >>> pyplot.plot([1,3,1,3,1])
    

My guess is that that will show a TK window, but then stalls.

  • see if Tkinter actually works. try for example:

    >>> import Tkinter
    >>> import _tkinter
    >>> Tkinter._test()
    

The last command should show a little window with buttons.

Also, you don't really specify what you mean by "freeze":

  • does your system lock up completely?

  • does the script lock up? Or can you close the window and the scripts simply stops?

  • is something being drawn, or just an empty TK window pops up?

On the other hand, since you mention threads, you may have run into the general GUI issue: a GUI waits for user input. If you want it to wait for that, and in the mean time do calculations, the latter indeed have to be in a separate thread. Then again, if you want to update your graph each time the new figure has been calculated, there shouldn't be any need for that. See e.g. http://matplotlib.sourceforge.net/examples/animation/simple_anim_tkagg.html

Lastly, it may help if you specify your OS, if it comes to debugging your setup. And I assume Python Portble is Portable Python.

  • Well seems as you can't edit your comments after 5 minutes and that the time it took me to write it. Also I've reached the maximum character so my answer will be disposed in 2 comments: I tried ploting a simple graph and it doesn't show anything unless I use the .show() function and then it works great. Tkinter is actually working. When I said freeze I meant that you can move the windows but for instance if the different windows overlap then this will result in a white rectangle at the junction and they are not responding (I don't know if I'm clear here). – Leo Mar 20 '12 at 18:04
  • 1
    The function and the gui are ran in two different threads so that when I run my function the gui is still responsive. Finally I found the solution to my problem by reading this: http://matplotlib.sourceforge.net/faq/usage_faq.html#what-is-a-backend I then changed the defaut backend of myplotlib from TkAgg to WxAgg and it works perfectly now! By the way my OS is windows xp and yes Python Portable meant Portable Python. – Leo Mar 20 '12 at 18:05
  • Could to hear you solved it. Odd though that changing from Tkinter to WxWidgets did make it work; it would appear that your Tkinter setup is not properly working. I don't know if you tried the Tkinter tests I suggested, nor if you're still interested in doing this (since you've got it working with Wxwidgets), but I guess that may show if there would be a problem with the Tkinter setup. Just in case you'd like to use Tkinter in the future. –  Mar 23 '12 at 10:32
  • I don't know if I was clear but I didn't change everything from Tkinter to Wx. I just changed the backend used by matplotlib. And I did the Tkinter tests you suggested and it worked perfectly. Besides if I changed from Tkinter to Wx it's only because a Tkinter showed up when I was using matplotlib that shouldn't have. – Leo Mar 23 '12 at 19:43
  • Well, as far as I can tell, the TkAgg backend uses Tkinter for popping up the windows from matplotlib, while the WxAgg backend uses WxWidgets (which, in its turn, uses whatever is suitable for the platform). So, in effect, by switching your backend, you've switched the GUI kit that matplotlib uses. E.g., on Linux (or on other OSes if installed), you could even use the GTKAgg backend, which uses GTK for the GUI part. That's for clarification. It's all behind the scenes, so it otherwise doesn't matter. –  Mar 25 '12 at 18:10
0

Try closing matplotlib plotting before opening a Tkinter window:

plt.close()
tk = Tkinter()
...

It works for me.

Apostolos
  • 3,115
  • 25
  • 28