Python线程-在保持框架打开的同时将控制权返回给终端 [英] Python threading- returning control to the terminal while keeping a frame open

查看:98
本文介绍了Python线程-在保持框架打开的同时将控制权返回给终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:我想在 Python 中打开一个框架,能够使用该框架,并且还能够继续使用该终端.

To sum up: I want to open a frame in Python, be able to work with that frame, and also be able to continue using that terminal.

基本上,我想模仿Python中的某些Matlab行为.在 Matlab 中,您可以:

Basically, I'd like to mimic some Matlab behavior in Python. In Matlab, you can do:

x=0:10;
y=0:10;
plot(x,y)

,将出现一个图,该图是交互式的,并且您也可以访问终端以进行更改和其他工作.

and a plot will come up, be interactive, and you also have access to the terminal to change things and do other work.

我发现在Python中,如果正确地进行线程化,我可以做同样的事情.但是,下面的代码可以控制终端.

I figured that in Python, if I threaded properly, I could do the same thing. However, the code below keeps control of the terminal.

from threading import Thread
from matplotlib import pyplot as plt

class ThreadFrame(Thread):
    def __init__(self):
        Thread.__init__(self)

    def setup(self):
        my_plot = plt.plot(range(0,10), range(0,10))
        fig = plt.gcf()
        ax = plt.gca()

my_thread = ThreadFrame()
my_thread.start()
my_thread.setup()

plt.show()

在线程处理方面我应该做些不同的事情吗?或者还有其他方法可以做到这一点吗?

Is there something I should be doing differently with the threading? Or is there another way to accomplish this?

推荐答案

matplotlib 对使用 plt.show() 感到很有趣.要获得您想要的行为,请查看 有关以交互方式使用 matplotlib 的文档.好消息是,不需要线程.

matplotlib gets funny about using plt.show(). To get the behaviour you want, look at the docs on using matplotlib interactively. The nice thing is, no threading necessary.

(在您的解决方案中,绘图在后台准备好,后台线程终止,您的前台线程留在 plt.show 中)

(In your solution, the plot is prepared in the background, the background thread terminates, and your foreground threads stays in the plt.show)

这篇关于Python线程-在保持框架打开的同时将控制权返回给终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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