Tkinter Matplotlib, backend conflict?

北慕城南 提交于 2021-01-27 20:30:27

问题


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


回答1:


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.




回答2:


Try closing matplotlib plotting before opening a Tkinter window:

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

It works for me.



来源:https://stackoverflow.com/questions/9789777/tkinter-matplotlib-backend-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!