Tkinter Matplotlib,后端冲突? [英] Tkinter Matplotlib, backend conflict?

查看:76
本文介绍了Tkinter Matplotlib,后端冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 python 很陌生,但我上周花了我的时间尝试编写一个软件来可视化一些复杂的东西.

基本的影像效果是:用户将所需的所有信息输入到GUI中,然后单击继续",而我还有另一个重要功能可以生成所有图形.

这是有效的,但问题是当我运行该函数时,该函数持续了大约 2 分钟,tkinter 窗口冻结了.我读到我应该使用线程.然后我发现了这个:http://uucode.com/texts/pylongopgui/pyguiapp.html这是一个基本上可以做我想做的事情以及其他一些事情的例子.

我现在正在尝试调整我的代码以使其适合这一点.

这是我的问题:一切似乎都正常运行,只是在我的函数中突然弹出一个名为"tk"的新窗口,并且一切都冻结了.

此刻一切都静止了:

 #在链上跟踪文件对于范围内的 i (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,计划,Fils_Chaine + 1,-1])对于范围内的 j(0,计划):对于范围内的 k(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')

在不使用Tkinter的情况下直接运行它时,一切正常,因此我不知道是什么原因造成的.

我也尝试过用无限循环替换这段代码:

i=1当我>0:我=我+1打印(i)

这行得通,没有什么是冻结的.但后来我尝试了这个:

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

所有内容冻结,然后弹出名为"tk"的窗口,并立即冻结.

我在某处读到这可能是beetwen Tkinter和matplotlib后端之间的冲突,但这仅是全部,对我没有多大帮助.

我不知道这是否有帮助,但我使用的是 Python Portble 2.7.2.1

解决方案

我不能写评论,但是要检查的几件事是:

  • 在 python cmdline 上,尝试绘​​制一个非常简单的图形.例如:

     >>>导入pyplot>>>pyplot.plot([1,3,1,3,1])

我的猜测是这会显示一个 TK 窗口,但随后会停止.

  • 查看Tkinter是否真正起作用.尝试例如:

     >>>导入 Tkinter>>>导入_tkinter>>>Tkinter._test()

最后一个命令应该显示一个带按钮的小窗口.

此外,您并没有真正指定冻结"的含义:

  • 您的系统是否完全锁定?

  • 脚本是否锁定?还是可以关闭窗口而脚本只是停止?

  • 是否正在绘制某些东西,或者只是弹出一个空的TK窗口?

另一方面,由于提到了线程,因此您可能会遇到一般的GUI问题:GUI等待用户输入.如果您希望它等待,并且同时进行计算,则后者实际上必须位于单独的线程中.再说一次,如果您想在每次计算新图形时都更新图形,那么就不需要这样做了.见例如http://matplotlib.sourceforge.net/examples/animation/simple_anim_tkagg.html

最后,如果要调试安装程序,则指定操作系统可能会有所帮助.我假设 Python Portble 是便携式 Python.

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

解决方案

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.

这篇关于Tkinter Matplotlib,后端冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆