TypeError: init () takes exactly 1 argument (2 given)

TypeError: init () takes exactly 1 argument (2 given)

如果有人知道如何解决这个错误,我将不胜感激。我在 运行 以下代码时收到此错误:

!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block 22
# Generated: Tue Jul 23 15:54:16 2019
##################################################




from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
from flask import Flask, render_template, jsonify, request, redirect, url_for
from random import randint


class top_block_22(gr.top_block):

    def __init__(self):
        gr.top_block.__init__(self, "Top Block 22")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.blocks_add_xx = blocks.add_vff(1)
        self.audio_sink = audio.sink(32000, '', True)
        self.analog_sig_source_x_1 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 440, 0.4, 0)
        self.analog_sig_source_x_0 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 350, 0.4, 0)
        self.analog_noise_source_x_0 = analog.noise_source_f(analog.GR_GAUSSIAN, 0.005, -42)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_noise_source_x_0, 0), (self.blocks_add_xx, 2))
        self.connect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx, 0))
        self.connect((self.analog_sig_source_x_1, 0), (self.blocks_add_xx, 1))
        self.connect((self.blocks_add_xx, 0), (self.audio_sink, 0))

app = Flask(__name__)
@app.route('/')
def hex_color():
        return render_template("index.html")
@app.route('/valueofslider')
def slide():
    slide_val = request.args.get('slide_val')
    return top_block_22(slide_val)     


def main(top_block_cls=top_block_22, options=None):

    tb = top_block_cls()
    tb.start()

    try:
        raw_input('Press Enter to quit: ')
    except EOFError:
        pass
    tb.stop()
    tb.wait()

    samp_rate = int(slide_val) + 100
    print(samp_rate)
    return(slide_val) # Still need to return or get TypeError 

if __name__ == '__main__':
    app.run(debug=True)

错误日志:

fit-pc@fitpc-fitlet2:~$ python /home/fit-pc/my_flask_app/virtualenv/Slider/top_block_22.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 269-962-008
127.0.0.1 - - [26/Jul/2019 10:05:46] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Jul/2019 10:05:48] "GET /valueofslider?slide_val=6037 HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/fit-pc/my_flask_app/virtualenv/Slider/top_block_22.py", line 56, in slide
    return top_block_22(slide_val)
TypeError: __init__() takes exactly 1 argument (2 given)

更新:

谢谢大家的回复,但不幸的是 none 到目前为止对我有用。这是我的 index.html 代码 DOCTYPE html html head meta harset="utf-8" title jQuery roundSlider - JS Bin

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/roundSlider/1.3.2/roundslider.min.css" rel="stylesheet" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/roundSlider/1.3.2/roundslider.min.js"></script>
</head>
<body>
  <!-- Only html needed   -->
  <div id="slider"></div>

  <script>
    var val;
    $("#slider").roundSlider({
      radius: 215,
        min: 0,
        max: 40000,

      change: function () {
        var obj1 = $("#slider").data("roundSlider");
        val = obj1.getValue();
        value: 1
        $.getJSON('/valueofslider', {
          slide_val: val
        });
      }
    });
  </script>
</body>

似乎当你调用 gr.top_block.__init__(self, "Top Block 22") 时你不应该将 "Top Block 22" 传递给它。

如错误所述,__init__() takes exactly 1 argument (2 given)

您没有在 def __init__() 中为 top_block22 传递参数 .您在指定行中传递的参数无法识别。

应该如下所示


def __init__(self, slide_value): 
    self.slide_value = slide_value

其实日志说的已经够多了。错误在这里return top_block_22(slide_val)

这很正常,如果你查看 Class 的定义,它不接受任何参数,self 将由 python 提供,但你并没有真正向 Class 添加任何参数定义,后来你创建了一个对象并向它传递了一个参数,这就是你得到 Error

的原因

如果你这样调用对象的创建: return top_block_22() # this would throw no Error