Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 在类中创建的模块实例如何从该类继承(Python 3)?_Class_Variables_Inheritance_Module_Parallel Processing - Fatal编程技术网

Class 在类中创建的模块实例如何从该类继承(Python 3)?

Class 在类中创建的模块实例如何从该类继承(Python 3)?,class,variables,inheritance,module,parallel-processing,Class,Variables,Inheritance,Module,Parallel Processing,'我想构建一个包含类AnimationCanvas内部(anim.py)的模块。我想在一个单独的main.py文件中调用这个模块,其中数据(一个变量)正在更改(可能使用GUI)。实例animatedAxes将通过从main.py文件中的变量获取数据自动更新其绘图,同时main.py代码正在运行(一种并行过程)。' '问题:模块中的类AnimationCanvas实例在main.py文件中没有看到类main的变量。 如果类AnimationCanvas和类main在同一个文件中,我知道怎么做。不过

'我想构建一个包含类AnimationCanvas内部(anim.py)的模块。我想在一个单独的main.py文件中调用这个模块,其中数据(一个变量)正在更改(可能使用GUI)。实例animatedAxes将通过从main.py文件中的变量获取数据自动更新其绘图,同时main.py代码正在运行(一种并行过程)。'

'问题:模块中的类AnimationCanvas实例在main.py文件中没有看到类main的变量。 如果类AnimationCanvas类main在同一个文件中,我知道怎么做。不过,我希望有一个动画模块(单独的文件),可以在任何地方使用,只需导入它并编写几行即可

“我可以调用类AnimationCanvas\uuuuuu init\uuuu函数并将变量传递给它,但这是一种一次性效果,如果类main中的变量发生更改,则animatedAxes实例将不会看到此更改。”

'有效的单个文件(Single.py):'

'两个不工作的文件:'

'main.py:'

import matplotlib.pyplot as plt
import numpy as np
from anim import AnimationCanvas

class main():
    def __init__(self):
        self.size=800
        self.data=np.random.rand(self.size)

        # initialize animated graph routine
        self.animatedAxes = AnimationCanvas()  

        # run random data array
        for ii in range(20):
            print(ii)
            self.data=np.random.rand(self.size)
        plt.pause(0.1)

if __name__ == '__main__':
    main()
“anim.py:”

import matplotlib.pyplot as plt
from matplotlib.animation import TimedAnimation
from matplotlib.lines import Line2D
import numpy as np

class AnimationCanvas(TimedAnimation):
    def __init__(self):
        # initialize random data array 
        self.data = np.random.rand(5)
        # Create animation axis and figure
        self.fig = plt.figure(1, figsize=(5, 5)) 
        self.ax = plt.axes([0.1, 0.1, 0.8, 0.8])
        self.line1 = Line2D([], [], color='blue')
        self.ax.add_line(self.line1)

        # start animation with interval of 10 milliseconds
        TimedAnimation.__init__(self, self.fig, interval=10, blit=True)

    def new_frame_seq(self):
        return iter(range(5*5))

    def _step(self, *args):
        try:
            TimedAnimation._step(self, *args)
        except Exception as e:
            TimedAnimation._stop(self)
            pass

    def _draw_frame(self, framedata):
        'update self.data:'
        '????????????????'
        'update plot with self.data'
        self.line1.set_data(np.arange(len(self.data)),self.data)
“我尝试使用super(AnimationCanvas,self)。\uuu init\uuu(),但它不起作用。”

“据我所知,我需要在类main的self类AnimationCanvas的self之间建立直接联系。如有任何建议,我们将不胜感激。谢谢

import matplotlib.pyplot as plt
from matplotlib.animation import TimedAnimation
from matplotlib.lines import Line2D
import numpy as np

class AnimationCanvas(TimedAnimation):
    def __init__(self):
        # initialize random data array 
        self.data = np.random.rand(5)
        # Create animation axis and figure
        self.fig = plt.figure(1, figsize=(5, 5)) 
        self.ax = plt.axes([0.1, 0.1, 0.8, 0.8])
        self.line1 = Line2D([], [], color='blue')
        self.ax.add_line(self.line1)

        # start animation with interval of 10 milliseconds
        TimedAnimation.__init__(self, self.fig, interval=10, blit=True)

    def new_frame_seq(self):
        return iter(range(5*5))

    def _step(self, *args):
        try:
            TimedAnimation._step(self, *args)
        except Exception as e:
            TimedAnimation._stop(self)
            pass

    def _draw_frame(self, framedata):
        'update self.data:'
        '????????????????'
        'update plot with self.data'
        self.line1.set_data(np.arange(len(self.data)),self.data)